Skip to content

Commit

Permalink
Merge pull request #49 from gaelj/initial-development
Browse files Browse the repository at this point in the history
Initial development
  • Loading branch information
gaelj authored Jan 7, 2024
2 parents 6ad28f2 + 4533445 commit 23cf002
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 8 additions & 11 deletions CodeMirror6/NodeLib/src/CmMarkdownLink.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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)
}
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -92,7 +89,7 @@ function createMarkdownLinkDecorationPlugin(): Extension {

// Combine the extension with the mention plugin
export const markdownLinkExtension = (stylingEnabled: boolean) => [
createMarkdownLinkExtension(),
stylingEnabled ? createMarkdownLinkExtension() : [],
stylingEnabled ? createMarkdownLinkDecorationPlugin() : [],
]

Expand Down
8 changes: 7 additions & 1 deletion CodeMirror6/NodeLib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]),
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 }
})
Expand Down

0 comments on commit 23cf002

Please sign in to comment.