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..be43cca4 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), ]), @@ -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 } })