Skip to content
ironmark

HTML to Markdown

htmlToMarkdown() converts HTML back to Markdown syntax.

import { htmlToMarkdown } from "ironmark";
 
htmlToMarkdown("<h1>Hello</h1><p><strong>Bold</strong> text</p>");
// "# Hello\n\n**Bold** text\n"

Unknown tags

By default, tags without a Markdown equivalent are dropped and their text is kept. Pass preserveUnknownAsHtml: true to keep them as raw HTML:

htmlToMarkdown("<p>H<sub>2</sub>O</p>", { preserveUnknownAsHtml: true });
// "H<sub>2</sub>O"

AST

htmlToAst() returns the same AST as markdownToAst(), so you can inspect or edit it before writing Markdown:

import { htmlToAst, astToMarkdown } from "ironmark";
 
const ast = htmlToAst("<h1>Hello</h1><p>World</p>");
const md = astToMarkdown(ast);

As a document

ironmark.fromHtml(html) returns a document, so every output works on HTML input too:

import ironmark from "ironmark";
 
const doc = ironmark.fromHtml("<h1>Hi</h1><p><b>bold</b></p>");
doc.markdown(); // "# Hi\n\n**bold**\n"
doc.text(); // "Hi\n\nbold"