Defaults
Frontmatter
Built-in plugin that parses a leading YAML frontmatter block into tree.frontmatter.
The comark/plugins/frontmatter plugin extracts a leading YAML frontmatter block (---) into tree.frontmatter and removes it from the markdown body before tokenization.
The plugin is enabled by default via registerDefaultPlugins. No installation or registration required.
Usage
---
title: Hello World
description: A sample document
---
# Hello Worldimport { parseMarkdown } from 'comark'
const result = await parseMarkdown(`---
title: Hello World
---
# Hello World
`)
console.log(result.frontmatter)
// → { title: 'Hello World' }
console.log(result.nodes)
// → [ ['h1', { id: 'hello-world' }, 'Hello World'] ]Explicit registration
When default plugins are off, opt in with the plugin directly:
import { parseMarkdown } from 'comark'
import frontmatter from 'comark/plugins/frontmatter'
const result = await parseMarkdown(content, {
registerDefaultPlugins: false,
plugins: [frontmatter()],
})Disable frontmatter parsing
Turn off all defaults (including frontmatter) so the --- block is treated as regular markdown:
const result = await parseMarkdown(content, { registerDefaultPlugins: false })
// result.frontmatter → {}How it works
In the pre hook (before markdown-exit tokenization), the plugin:
- Detects a leading
---…---block viaparseFrontmatter - Parses the YAML body into
state.frontmatter - Strips the frontmatter from
state.markdown - Adjusts
state.parsedLinesso later nodes get correct line numbers
The core parser then copies state.frontmatter onto tree.frontmatter.