Fast HTML-to-Markdown converter for Deno. Single function call, no config
required, handles the stuff you'd expect (tables, code blocks, nested lists,
reference links, etc.). Bare <pre> tags (without a <code> child) are fenced
just like <pre><code>, with language hints extracted from the class
attribute.
import { htmlToMd } from "jsr:@codybrom/html2md";Or in your deno.json:
{
"imports": {
"@codybrom/html2md": "jsr:@codybrom/html2md"
}
}import { htmlToMd } from "@codybrom/html2md";
htmlToMd("<h1>Hello</h1><p>World</p>");
// # Hello
//
// WorldThat's the whole API for most use cases. Pass HTML in, get markdown out.
If you've got multiple files, batchHtmlToMd takes a record and returns one:
import { batchHtmlToMd } from "@codybrom/html2md";
const results = batchHtmlToMd({
"page1.html": "<b>hello</b>",
"page2.html": "<em>goodbye</em>",
});
// { "page1.html": "**hello**", "page2.html": "_goodbye_" }For repeated conversions with the same config, MdEngine freezes options once
and reuses them:
import { MdEngine } from "@codybrom/html2md";
const engine = new MdEngine({ bulletMarker: "-" });
engine.convert("<ul><li>one</li><li>two</li></ul>");Everything's optional. Defaults are sensible.
htmlToMd(html, {
codeFence: "```", // code block delimiter
bulletMarker: "*", // unordered list marker (* - +)
codeBlockStyle: "fenced", // "fenced" or "indented"
emphasisMark: "_", // italic delimiter
strongMark: "**", // bold delimiter
strikeMark: "~~", // strikethrough delimiter
indent: " ", // list nesting indent
maxBlankLines: 3, // collapse runs of blank lines
keepDataImages: false, // include data: URI images
refLinks: false, // [text][1] style instead of inline
autolinks: true, // <url> when link text matches href
skipTags: [], // tags to ignore completely
extraBlockTags: [], // treat these as block-level
charEscape: [/[\\`*_~\[\]]/gm, "\\$&"],
lineLeadEscape: [/pattern/gm, "replacement"],
textReplacements: [], // [[/find/g, "replace"], ...]
});MIT