Automerge backed Markdown editor for the Atmosphere
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import { markdownLanguage } from '@codemirror/lang-markdown'import { NodeProp } from '@lezer/common'import { Element, MarkdownParser, type InlineContext } from '@lezer/markdown'import { Language } from '@codemirror/language'
/** The token-class property exposed by Obsidian's CodeMirror bundle. */export const tokenClassNodeProp = new NodeProp<string>()
/** * Exposes code-span content as `InlineCode` and its delimiters as adjacent * `CodeMark` nodes. This matches the delimiter-free `inline-code` token * Dataview's live-preview extension consumes. */function parseObsidianInlineCode(cx: InlineContext, next: number, start: number): number { if (next !== 96 || (start && cx.char(start - 1) === 96)) return -1
let size = 0 while (cx.char(start + size) === 96) size++
for (let pos = start + size; pos < cx.end; pos++) { if (cx.char(pos) !== 96) continue
let end = pos while (cx.char(end) === 96) end++ if (end - pos !== size) { pos = end - 1 continue }
const nodeType = (name: string) => cx.parser.nodeSet.types.find(type => type.name === name)!.id // `Element` intentionally omits its public constructor from the package // declarations, though the parser extension API requires emitting one. const element = (type: number, from: number, to: number) => new (Element as unknown as new (type: number, from: number, to: number) => Element)(type, from, to) const codeMark = nodeType('CodeMark') cx.addElement(element(codeMark, start, start + size)) cx.addElement(element(nodeType('InlineCode'), start + size, pos)) return cx.addElement(element(codeMark, pos, end)) }
return -1}
/** Markdown parser used by Reams editors, with Obsidian token classes. */const parser = (markdownLanguage.parser as MarkdownParser).configure({ parseInline: [{ name: 'InlineCode', parse: parseObsidianInlineCode }], props: [tokenClassNodeProp.add({ InlineCode: 'inline-code', CodeMark: 'formatting', StrongEmphasis: 'strong', Emphasis: 'em', Strikethrough: 'strikethrough', Highlight: 'highlight', Comment: 'comment', })],})
export const reamsMarkdownLanguage = new Language(markdownLanguage.data, parser, [], 'markdown')