API overview
The parser ships as two npm packages, which always share one version.
@mnaoumov/obsidian-markdown-parseris everything: the editor’s classes, the three syntax adapters, the remark engine and the metadata. It depends on CodeMirror 6,@lezer/*,codemirror@5, micromark and mdast, markdown-it,yamland three entity tables.@mnaoumov/obsidian-markdown-parser-metadata(packages/metadata/) is the metadata projection and the remark engine it is built from, and nothing else. It depends onyaml,character-entities-legacy,character-reference-invalidanddecode-named-character-referenceonly.
The metadata package exists for a consumer that needs only CachedMetadata, such as a mock of Obsidian’s metadata cache, and must not pull in an editor. Its source is the same src/remark/ and src/metadata/ the root package builds, compiled by packages/metadata/tsconfig.build.json. src/entries/metadata-package.test.ts pins that their import closure reaches no other module and no package beyond those four, at the root package’s versions.
Installing
Section titled “Installing”npm install @mnaoumov/obsidian-markdown-parser # everythingnpm install @mnaoumov/obsidian-markdown-parser-metadata # the metadata projection aloneBoth packages are ES modules with TypeScript declarations, for Node 22 or later and for bundlers. To try an unreleased change, npm run build in a clone, then npm pack and npm pack --workspace packages/metadata give the tarballs, which npm install <path to the tarball> installs.
Entry points
Section titled “Entry points”Every entry point of the root package is a subpath of it, and the root (@mnaoumov/obsidian-markdown-parser) exports all of them at once.
@mnaoumov/obsidian-markdown-parser/editor
Section titled “@mnaoumov/obsidian-markdown-parser/editor”highlight,highlightLinesandLineState: the classes Obsidian’s editor puts on every character and line, resumable from any line outside a fenced block or a script or style element (isLineStateResumable, phase-1.md).editorClassLanguageandcreateEditorClassLanguage: that tokenizer as a CodeMirror 6Language, with the editor’s tree shape and incremental re-parsing (cm6-host.md).- Each of
highlight,highlightLinesandcreateEditorClassLanguagetakes anobsidianVersion, the release whose classes to give: 1.15.0 and later mark the end of emphasis, strikethrough and code spans (phase-1.md). Without one they give 1.14’s classes. A state passed to highlightLines keeps the version it was started for.parseMetadataandworkerFailureOftake anobsidianVersiontoo: 1.15.1 and later postv: 1and read a front matter keyhasOwnProperty(metadata.md). Without one they give 1.15.0’s metadata. editorClassesandeditorClassDecorations: its classes rendered as the editor renders them (cm6-host.md).
@mnaoumov/obsidian-markdown-parser/lezer
Section titled “@mnaoumov/obsidian-markdown-parser/lezer”parse,obsidianParserandcreateObsidianParser: the@lezer/markdowntree with the Obsidian syntax (syntax.md).obsidianMarkdown,createObsidianMarkdownandobsidianSyntax: that syntax as@lezer/markdownextensions (syntax.md).renderLezerHtmlandcommonMarkFixes: a Lezer tree rendered as CommonMark’s reference implementation does, for the spec suites (spec-suites.md).
@mnaoumov/obsidian-markdown-parser/mdast
Section titled “@mnaoumov/obsidian-markdown-parser/mdast”parseMdast: the mdast tree with the Obsidian syntax (syntax.md).micromarkObsidianMarkdown,createMicromarkObsidianMarkdownandmdastObsidianMarkdown: that syntax as micromark andmdast-util-from-markdownextensions.micromarkQuirksHtml: micromark’s events rendered with Obsidian’s quirks (spec-suites.md).
@mnaoumov/obsidian-markdown-parser/markdown-it
Section titled “@mnaoumov/obsidian-markdown-parser/markdown-it”parseMarkdownIt,markdownItObsidianSyntaxandcreateObsidianMarkdownIt: the syntax as a markdown-it plugin, with tokens named afterObsidianNode(syntax.md).
@mnaoumov/obsidian-markdown-parser/syntax
Section titled “@mnaoumov/obsidian-markdown-parser/syntax”What the lezer, mdast and markdown-it adapters share:
ObsidianNode: the names of the syntax nodes.CONFORMANCE_CASES: the corpus every adapter is checked against.- The options: a preset (
commonmark,gfmorobsidian, the default) with any of its flags turned on or off,resolveObsidianSyntaxandresolveObsidianQuirksto read them, andObsidianQuirkFlagsfor where Obsidian reads valid CommonMark differently, all off in every preset (spec-suites.md).
@mnaoumov/obsidian-markdown-parser/remark
Section titled “@mnaoumov/obsidian-markdown-parser/remark”parseRemark: the mdast tree Obsidian’s reading view and metadata worker parse, from a port ofremark-parse8 with Obsidian’s edits (remark.md).renderRemarkHtml: that tree rendered asremark-htmldoes.
@mnaoumov/obsidian-markdown-parser/metadata
Section titled “@mnaoumov/obsidian-markdown-parser/metadata”parseMetadata: theCachedMetadatathe metadata worker posts for a document, equal to it as JSON, ornullwhere the worker throws (metadata.md).
The metadata package has two entry points: its root (@mnaoumov/obsidian-markdown-parser-metadata), which is ./metadata above, and @mnaoumov/obsidian-markdown-parser-metadata/remark, which is ./remark above.
Example
Section titled “Example”import { parseMetadata } from '@mnaoumov/obsidian-markdown-parser-metadata';
const metadata = parseMetadata('---\ntags: [a]\n---\n# Title\n\nSee [[Note]] #tag ^id\n');// metadata?.links?.[0]?.link === 'Note', metadata?.blocks?.['id'], metadata?.frontmatter?.['tags'] ...import { LanguageSupport } from '@codemirror/language';import { EditorState } from '@codemirror/state';import { EditorView } from '@codemirror/view';import { editorClasses, editorClassLanguage} from '@mnaoumov/obsidian-markdown-parser/editor';
const state = EditorState.create({ doc: '# Title', extensions: [new LanguageSupport(editorClassLanguage), editorClasses] });new EditorView({ parent: document.body, state });Checked by
Section titled “Checked by”npm run build compiles both packages, and src/entries/metadata-package.test.ts checks that each package’s exports names exactly the entry files its build compiles. A consumer installing both tarballs, type-checking an import of every entry point under "moduleResolution": "NodeNext" with skipLibCheck off, and running them in Node, was verified when the packages were added; the metadata consumer’s node_modules held only the four dependencies above and character-entities, which decode-named-character-reference depends on.