Hast-util-to-mdast
lastmod 2026-11-13created 2026-11-134 edits
Utility that converts a hast (HTML) syntax tree into an mdast (Markdown) syntax tree
About
- A syntax-tree utility from the unified collective, used to convert HTML to Markdown
- The input is hast (HTML syntax tree) and the output is mdast (Markdown syntax tree). The reverse conversion is handled by
mdast-util-to-hast; for higher-level use, there isrehype-remark, a rehype plugin that wraps this utility - The usual flow is to parse HTML with
hast-util-from-html, convert it withtoMdast, and then produce a string withmdast-util-to-markdown-
<h2>Hello <strong>world!</strong></h2>→## Hello **world!**
-
xmp and base. Particularly strong at handling forms, media, and implicit and explicit paragraphs <table> and <del>/<s>/<strike> become GFM table and delete nodes. Does not produce definitions/references, footnotes, frontmatter, or math <svg> are dropped and <video> is downgraded to a link. The handlers (per tag) and nodeHandlers (per node type) options let you change the default handling or keep parts of the HTML as-is data-mdast="ignore" attribute are excluded from conversion newlines (keep line breaks when collapsing whitespace), checked/unchecked (checkbox markers, default [x]/[ ]), quotes (quote characters for each <q> nesting level), document (whether to treat the input as a complete document and wrap content in paragraphs) hast-util-to-mdast@^10 is compatible with Node.js 16 and later. npm install hast-util-to-mdast See also
- Markdown
- HTML
- Abstract syntax tree
- Defuddle - Open-source HTML-to-Markdown alternative to Readability