mdast-util-to-markdown
mdast-util-to-markdown is a low-level utility for serializing an mdast (Markdown Abstract Syntax Tree) syntax tree back into a Markdown string. It is currently at version 2.1.2. The package maintains an active release cadence, frequently publishing patch and minor versions for bug fixes and new features, with major versions reserved for significant breaking changes, such as Node.js version requirement updates or fundamental API shifts. This utility differentiates itself by providing direct, granular control over the Markdown serialization process, contrasting with higher-level solutions like `remark-stringify` which abstract away these internals. It is a core component within the unified `syntax-tree` ecosystem and is designed to be highly extensible through integration with other `mdast-util` packages, enabling support for various Markdown extensions like GFM, MDX, and frontmatter. Developers typically use this package when manual syntax tree manipulation is required, rather than relying on a full-fledged Markdown processor.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to use `require()` to import `mdast-util-to-markdown` in a CommonJS context.fixConvert your module to ES Modules or use dynamic `import()` if you cannot switch entirely. For example, replace `const toMarkdown = require('mdast-util-to-markdown')` with `import { toMarkdown } from 'mdast-util-to-markdown'`. -
TypeError: Cannot read properties of undefined (reading 'type')
cause The `tree` argument passed to `toMarkdown` is `undefined`, `null`, or an invalid object that lacks a `type` property.fixEnsure that the `tree` variable you are passing to `toMarkdown` is a valid mdast node object, typically a `Root` or other compatible node, as documented by the mdast specification. -
Error: Cannot find module 'mdast-util-to-markdown'
cause The package `mdast-util-to-markdown` has not been installed or the import path is incorrect.fixRun `npm install mdast-util-to-markdown` to install the package. Verify that the import statement `import { toMarkdown } from 'mdast-util-to-markdown'` uses the correct package name.
Warnings
- breaking Version 2.0.0 changed the minimum required Node.js version to 16. Older Node.js environments are no longer supported.
- breaking Version 2.0.0 switched to using `exports` maps. Direct access to internal or private APIs via non-explicit import paths may break.
- breaking The `bulletOrderedOther` option was removed in v2.0.0 as its functionality became the default. Passing this option will have no effect or may cause errors if not handled gracefully by your code.
- breaking The default value for the `fences` option changed to `true` in v2.0.0. If you relied on the previous default of `false` for fenced code blocks, your output may change.
- gotcha Earlier versions (pre-2.1.1) had issues with round-tripping 'attention' (strong/emphasis) by incorrectly encoding surrounding characters, potentially leading to malformed Markdown output.
Install
-
npm install mdast-util-to-markdown -
yarn add mdast-util-to-markdown -
pnpm add mdast-util-to-markdown
Imports
- toMarkdown
const toMarkdown = require('mdast-util-to-markdown')import { toMarkdown } from 'mdast-util-to-markdown' - defaultHandlers
const { defaultHandlers } = require('mdast-util-to-markdown')import { defaultHandlers } from 'mdast-util-to-markdown' - Options
import { Options } from 'mdast-util-to-markdown'import type { Options } from 'mdast-util-to-markdown'
Quickstart
/**
* @import {Root} from 'mdast'
*/
import {toMarkdown} from 'mdast-util-to-markdown'
/** @type {Root} */
const tree = {
type: 'root',
children: [
{
type: 'blockquote',
children: [
{type: 'thematicBreak'},
{
type: 'paragraph',
children: [
{type: 'text', value: '- a\nb !'},
{
type: 'link',
url: 'example.com',
children: [{type: 'text', value: 'd'}]
}
]
}
]
}
]
}
console.log(toMarkdown(tree))