v2.0.1

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">
Blank lines<br>
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">; task items are <li class="md-task">
Tables<div class="md-table-wrap"><table class="md-table">
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">; images and videos are md-img, YouTube/Vimeo add md-embed
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 FlutterJSON.stringify(ast); 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.1
中文