An opinionated Markdown parser and formatter built on unified and remark.
remark-parser combines CommonMark parsing with frontmatter, GitHub Flavored Markdown, directives, readable reference links, optional local-link validation, Markdown stringification, and Prettier formatting.
| Feature | Description |
|---|---|
parseMarkdown() |
Asynchronous parsing into a transformed mdast tree. |
parseMarkdownSync() |
Synchronous parsing and transforms. |
processMarkdown() |
Returns the processed VFile with diagnostics in file.messages. |
formatMarkdown() |
Parses, transforms, stringifies, and formats Markdown. |
| Custom plugins | Supports unified plugins and [plugin, ...params] tuples. |
| VFile support | Accepts strings, file-like objects, and VFiles; preserves paths. |
| Validation | Optional relative-link checks run before reference-link conversion. |
| TypeScript | Expanded declarations and tsd type tests. |
| CI | Node.js 20, 22, and 24. |
Syntax and transform coverage:
- CommonMark parsing into an mdast syntax tree.
- YAML and TOML frontmatter.
- GitHub Flavored Markdown: tables, task lists, footnotes, autolinks, and strikethrough.
- Generic directives for custom Markdown syntax.
- Convert inline links and images into reference-style links.
- Renumber and order generated reference definitions.
- Optional validation of relative links and heading fragments.
- Markdown serialization with Prettier formatting.
- ESM-first package with TypeScript declarations.
npm install remark-parserNode.js 20 or newer is required.
import { parseMarkdown } from 'remark-parser'
const tree = await parseMarkdown(`
---
title: Hello
---
# Hello, world!
A table:
| Feature | Status |
| --- | --- |
| GFM | enabled |
`)
console.dir(tree, { depth: null })import { formatMarkdown } from 'remark-parser'
const markdown = await formatMarkdown(`
# A title
Read [the remark documentation](https://remark.js.org).
`)
console.log(markdown)Formatting converts inline links into numbered references and orders their definitions. Prettier is enabled by default for formatMarkdown and createProcessor.
import { createParser } from 'remark-parser'
const parser = createParser({
frontmatter: ['yaml'],
gfm: true,
directives: true,
references: false,
})
const tree = parser.parse('# Custom pipeline')
const transformedTree = await parser.run(tree)
// `createParser()` is a normal unified processor; add plugins before freezing it.
const custom = createParser().use(() => (tree) => {
console.log(`Found ${tree.children.length} top-level nodes`)
})The simplest way to extend the preset is to use the exported remarkParser plugin:
import { unified, remarkParser } from 'remark-parser'
const processor = unified()
.use(remarkParser, { references: false })
.use(() => (tree) => {
// Add your own mdast transform here.
return tree
})The default export and named export. A unified preset plugin that installs the configured parser, syntax extensions, transformations, and optional output stages.
Returns a unified processor configured to parse Markdown and run mdast transforms. It does not install a Markdown compiler, so use parse() followed by run() or call parseMarkdown().
Returns a complete processor with remark-stringify. Prettier formatting is enabled by default. Set prettier: false to disable it.
Parses Markdown and runs enabled mdast transforms. Returns the transformed mdast root.
Synchronous variant of parseMarkdown. Returns the transformed mdast root directly. Throws if a configured transform is asynchronous — the built-in plugins are all synchronous.
Runs the full parse/transform/stringify pipeline and resolves to the processed VFile. The compiled Markdown is available via String(file), and diagnostics (for example, relative-link warnings) are in file.messages. This is the best entry point when you need file metadata or messages rather than a plain string.
Parses, transforms, stringifies, and formats Markdown. Returns a string.
| Option | Default | Description |
|---|---|---|
frontmatter |
['yaml', 'toml'] |
Frontmatter formats to recognize, or false to disable. |
gfm |
true |
Enable GFM. Pass GFM options or false. |
directives |
true |
Enable generic directives. |
references |
true |
Convert inline links and images into references. |
orderReferences |
true |
Renumber and order reference definitions. |
validateRelativeLinks |
false |
Validate relative files and heading fragments. |
stringify |
true in remarkParser |
Install remark-stringify. |
stringifyOptions |
— | Options passed to remark-stringify. |
prettier |
false in remarkParser; true in createProcessor |
Enable unified-prettier, or pass Prettier options. |
plugins |
[] |
Extra unified plugins applied after built-in transforms. Pass a plugin or a [plugin, ...params] tuple. |
Use the plugins option to run your own unified plugins after the built-in transforms. Entries are either a plugin or a [plugin, ...params] tuple:
import { processMarkdown } from 'remark-parser'
const appendSignature =
(signature = '— generated') =>
(tree) => {
tree.children.push({
type: 'paragraph',
children: [{ type: 'text', value: signature }],
})
}
const file = await processMarkdown('# Hello', {
plugins: [[appendSignature, '— docs-bot']],
})
console.log(String(file))Note that unified identifies plugins by function reference: registering the same plugin twice via use() reconfigures it rather than adding a second instance.
Relative-link validation is deliberately opt-in. It checks local file paths and heading fragments, so it needs a file-backed VFile with a meaningful path and a working directory. It is not appropriate for arbitrary in-memory Markdown strings. Validation runs before inline links are converted to reference-style definitions, so it sees the original link targets. The plugin takes no options; enable it with validateRelativeLinks: true.
import { readFile } from 'node:fs/promises'
import { createProcessor } from 'remark-parser'
const path = 'docs/guide.md'
const value = await readFile(path, 'utf8')
const processor = createProcessor({
validateRelativeLinks: true,
prettier: false,
})
const file = await processor.process({ path, value })
for (const message of file.messages) {
console.error(message)
}The validation plugin reports warnings through file.messages; applications can decide whether warnings should fail their build.
| Plugin | Purpose |
|---|---|
remark-frontmatter |
YAML, TOML, and other frontmatter syntax. |
remark-parse |
Markdown parsing into mdast. |
unified-prettier |
Format serialized output with Prettier. |
remark-gfm |
GitHub Flavored Markdown extensions. |
remark-reference-links |
Convert inline links and images to reference definitions. |
remark-order-reference-links |
Order and number reference definitions. |
remark-directive |
Generic text, leaf, and container directives. |
remark-validate-relative-links |
Validate local links and heading fragments. |
remark-stringify |
Serialize mdast back to Markdown. |
remark-stringify is included as the matching output compiler for the parser. It is not a syntax extension, but it is required for formatMarkdown() to produce Markdown text.
npm install
npm test
npm run check
npm run format:checkRun the examples:
node examples/basic.js
node examples/validate-links.js README.mdAll plugins in this package run in the unified pipeline. Markdown syntax plugins transform or extend mdast; they do not sanitize HTML output. If you later convert mdast to HTML, apply an appropriate HTML sanitization policy before rendering untrusted content. Relative-link validation accesses the local filesystem and should only be enabled for trusted repository content.
MIT