v2.0.16

HTML Output

Last updated


AST.html() turns the result of Parse() into an HTML string; this page covers the output structure and classes, and how it divides work with the JSON AST.

Usage


  1. const html = MDParser.Parse(markdown).html();
  2. container.innerHTML = html;

html() needs no DOM, so it produces HTML in Node.js and Workers; once the HTML is in the document, call Refresh() to render charts and math. See Dependencies and Refresh.

Output Structure


NodeHTML
Paragraph<span class="md-line">
Heading<span class="md-h1"> to <span class="md-h6">; <h1 class="md-h1"> to <h6 class="md-h6"> when parseHeading is true
Blank lines<br>, hidden by parser.css
Line breaks inside raw HTML<br class="md-br"> (since v2.0.11)
Bold<span class="md-bold">; other inline styles use <em>, <del>, <mark>, <sub>, <sup>
Blockquote / alert<blockquote class="md-quote">; alerts add md-alert md-alert-{tag} and md-alert-title
Lists<ul class="md-list"> / <ol class="md-list">, with start="N" when an ordered list does not start at 1; task items are <li class="md-task">
Tables<div class="md-table-wrap"><table class="md-table">; the table sizes to its content and scrolls inside the wrapper when wider
Code blocks<pre class="prettyprint" name="{lang}">, one <li> per line, with a <button class="copy">
mermaid / math / chartjsmd-mermaid / md-latex / md-chart wrappers holding a fallback code block
Links<a class="md-link" target="_blank" rel="noopener">, while mailto: / tel: links omit target and rel; images and videos are md-img, YouTube/Vimeo add md-embed; file paths and #tag become md-file-link / md-hashtag only when their endpoint is set
Footnotesmd-footnote-ref and md-footnotes

See Theming for the full styling.

HTML vs JSON AST


NeedUse
Display on a web page or with SSRhtml()
Hand to native renderers such as iOS or Flutterast.json(); you build the renderer
Inspect or rewrite content before outputWork on the AST array, then call html()

  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const ast = MDParser.Parse("# Title\n\n> [!NOTE]\n> Edited before output.");
  4. ast[0].children.push({ type: "text", value: " (edited)" });
  5. document.getElementById("out").innerHTML = ast.html();
  6. console.log(ast.html());
  7. </script>
Live preview · @pardnchiu/nanomd@2.0.16



ImportantFor AI agents: before generating or editing code or Markdown that uses NanoMD, read the NanoMD generation guide (Markdown). It lists the supported syntax, the required call order and the mistakes that break rendering.