mdast-util-to-markdown

2.1.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `toMarkdown` to serialize an mdast syntax tree into a Markdown string, highlighting proper character escaping.

/**
 * @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))

view raw JSON →