From a2f379e2520b84252bb16f9833044a8a57874bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20James?= Date: Sun, 7 Jan 2024 12:33:44 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Format=20image=20and=20empty=20?= =?UTF-8?q?links?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeMirror6/NodeLib/src/CmMarkdownLink.ts | 19 ++++++++----------- CodeMirror6/NodeLib/src/index.ts | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CodeMirror6/NodeLib/src/CmMarkdownLink.ts b/CodeMirror6/NodeLib/src/CmMarkdownLink.ts index d73c0aea..c998cadd 100644 --- a/CodeMirror6/NodeLib/src/CmMarkdownLink.ts +++ b/CodeMirror6/NodeLib/src/CmMarkdownLink.ts @@ -1,11 +1,10 @@ -import { Extension, Range, RangeSetBuilder } from "@codemirror/state" -import { EditorView, Decoration, DecorationSet, ViewPlugin, ViewUpdate } from "@codemirror/view" -import { syntaxTree } from "@codemirror/language" +import { Extension, RangeSetBuilder } from "@codemirror/state" +import { EditorView, Decoration, ViewPlugin } from "@codemirror/view" import { isCursorInRange, isInCodeBlock } from "./CmHelpers" import { buildWidget } from "./lib/codemirror-kit" import { markdownLanguage } from "@codemirror/lang-markdown" -const linkRegex = /\[([^\]]+)\]\([^\)]+\)/g +const linkRegex = /\!?\[([^\]]*)\]\([^\)]+\)/g export function createMarkdownLinkExtension(): Extension { return ViewPlugin.define( @@ -23,11 +22,8 @@ export function createMarkdownLinkExtension(): Extension { const isCode = isInCodeBlock(view.state, start) if (!isCode) { const linkName = match[1] - if (!linkName || linkName === "") continue - if (linkName) { - const widget = createMarkdownLinkWidget(match[0], linkName) - builder.add(start, end, widget) - } + const widget = createMarkdownLinkWidget(match[0], linkName) + builder.add(start, end, widget) } } } @@ -49,7 +45,8 @@ function createMarkdownLinkWidget(rawLinkText: string, linkName: string) { toDOM: () => { const span = document.createElement("span") span.textContent = linkName - span.className = "cm-md-link-detail" + if (linkName) + span.className = "cm-md-link-detail" return span }, ignoreEvent: () => false, @@ -92,7 +89,7 @@ function createMarkdownLinkDecorationPlugin(): Extension { // Combine the extension with the mention plugin export const markdownLinkExtension = (stylingEnabled: boolean) => [ - createMarkdownLinkExtension(), + stylingEnabled ? createMarkdownLinkExtension() : [], stylingEnabled ? createMarkdownLinkDecorationPlugin() : [], ] diff --git a/CodeMirror6/NodeLib/src/index.ts b/CodeMirror6/NodeLib/src/index.ts index 77278756..228fc2fd 100644 --- a/CodeMirror6/NodeLib/src/index.ts +++ b/CodeMirror6/NodeLib/src/index.ts @@ -88,7 +88,7 @@ export async function initCodeMirror( listsExtension(initialConfig.autoFormatMarkdown), blockquote(), viewEmojiExtension(initialConfig.autoFormatMarkdown), - markdownLinkExtension(true), + markdownLinkExtension(initialConfig.autoFormatMarkdown), hyperLink, hyperLinkStyle, htmlViewPlugin(initialConfig.autoFormatMarkdown), ]), From 4533445fc5ee8d58f2e4772fad5f7df5fbd8b810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20James?= Date: Sun, 7 Jan 2024 12:37:58 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Initial=20force=20redraw,=20?= =?UTF-8?q?which=20now=20requests=20measure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeMirror6/NodeLib/src/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CodeMirror6/NodeLib/src/index.ts b/CodeMirror6/NodeLib/src/index.ts index 228fc2fd..be43cca4 100644 --- a/CodeMirror6/NodeLib/src/index.ts +++ b/CodeMirror6/NodeLib/src/index.ts @@ -201,6 +201,8 @@ export async function initCodeMirror( // add a class to allow resizing of the editor setResize(id, initialConfig.resize) + forceRedraw(id) + } catch (error) { console.error(`Error in initializing CodeMirror`, error) } @@ -294,6 +296,10 @@ export function setMentionCompletions(id: string, mentionCompletions: Completion export function forceRedraw(id: string) { const view = CMInstances[id].view + + view.requestMeasure() + view.update([]) + const changes = view.state.changeByRange((range: SelectionRange) => { return { range } })