v2.0.11

Dependencies and Refresh

Last updated


Init() loads Mermaid, KaTeX, Chart.js and code-prettify on demand, and Refresh() renders them inside a container on the page; this page covers how the two divide the work and how blocks fall back.

Init


  1. MDParser.Init({ enable: { mermaid: true, latex: true, highlight: true, chartjs: true } })
  2. .then(() => MDParser.Refresh(container))
  3. .catch((err) => console.error(err.message));

enable keyLoadsRenders
mermaidMermaid 11.4.1```mermaid
latexKaTeX 0.16.11 (JS and CSS)```math, $...$
highlightcode-prettifyCode block highlighting
chartjsChart.js 4.5.1```chartjs

The second argument overrides any URL (mermaidjs, katexjs, katexcss, googleprettifyjs, chartjs); the rest fall back to DEFAULT_ENDPOINT. Each URL is inserted once; when one fails to load, the Promise rejects with [Unavailable] <url>: failed to load. In Node.js, Init() only sets options and loads nothing.

Refresh


Refresh(container) processes the container in this order; without an argument it processes the whole document:
ItemBehavior
Copy buttonsBinds clicks, marks the pre with success / error for one second, and passes the result to OnCopyClick
File linksBinds clicks that call fileEndpoint; see File Links
Chart.jsDraws from data-chart, first destroying charts that left the page
KaTeXBlock and inline math
code-prettifyHighlights code blocks
MermaidChecks the syntax, then generates the SVG

The container must already be in the document; otherwise nothing is processed and [Invalid] container: not attached to the document is logged.

Fallbacks


CaseShown
Refresh() not called yet, or dependency not loadedPlain code block
Rendered successfullyThe wrapper gets data-rendered and shows the chart, formula or diagram; the code block hides
Invalid Mermaid syntax, block math or Chart.js configReplaced by a plain code block
Invalid inline mathRestored as $...$ text

  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const md = "~~~math\n\\int_0^1 x\\,dx = \\frac{1}{2}\n~~~\n\nInline $a^2+b^2=c^2$, broken $\\frac{1}{$.";
  4. const out = document.getElementById("out");
  5. out.innerHTML = MDParser.Parse(md).html();
  6. console.log("before:", out.querySelectorAll("[data-rendered]").length);
  7. MDParser.Init({ enable: { latex: true } })
  8. .then(() => MDParser.Refresh(out))
  9. .then(() => console.log("after:", out.querySelectorAll("[data-rendered]").length))
  10. .catch((err) => console.error(err.message));
  11. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文