v2.0.11

Getting Started

Last updated


This page installs NanoMD and renders a first piece of Markdown to HTML in the browser and in Node.js.

Prerequisites


ItemRequirement
BrowserES2022, RegExp lookbehind and \p{L}: Chrome, Edge, Firefox, Safari 16.4+
Node.js22 or higher (for ESM or require())
NetworkAccess to cdn.jsdelivr.net when Mermaid, KaTeX, Chart.js or code-prettify is enabled; see Dependencies and Refresh

Installation


CDN


  1. <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/nanomd@2.0.6/dist/parser.js"></script>

The script exposes a global MDParser and inserts parser.css from the same directory into <head>.

npm


  1. npm i @pardnchiu/nanomd

  1. import { MDParser } from "@pardnchiu/nanomd";

require("@pardnchiu/nanomd") also works in Node.js 22. If a bundler keeps the styles from loading automatically, add import "@pardnchiu/nanomd/parser.css";.

First Render


Parse() returns an AST and html() turns it into HTML; put the output in a class="nanomd" container to apply the styles.
  1. <div id="preview" class="nanomd"></div>
  2. <script>
  3. const md = "# Hello\n\nThis is **NanoMD**.\n\n- [x] Parse\n- [ ] Render";
  4. document.getElementById("preview").innerHTML = MDParser.Parse(md).html();
  5. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Output HTML in Node.js


Parse() and html() need no DOM, so they work for SSR and build pipelines.
  1. import { MDParser } from "@pardnchiu/nanomd";
  2. const html = MDParser.Parse("## Title\n\n- [x] Done").html();
  3. console.log(html);

Enable Diagrams, Math and Highlighting


Init() loads the enabled dependencies and Refresh() renders inside a container that is already in the document. Until the dependencies load, those blocks show as plain code blocks.
  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const md = "~~~math\nE = mc^2\n~~~\n\n~~~mermaid\nflowchart LR\n A[Markdown] --> B[AST] --> C[HTML]\n~~~";
  4. const out = document.getElementById("out");
  5. out.innerHTML = MDParser.Parse(md).html();
  6. MDParser.Init({ enable: { mermaid: true, latex: true } })
  7. .then(() => MDParser.Refresh(out))
  8. .then(() => console.log("rendered:", out.querySelectorAll("[data-rendered]").length))
  9. .catch((err) => console.error(err.message));
  10. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Next Steps


GoalPage
Learn the supported syntaxBlocks, Inline Formatting
Render the AST yourselfAST Nodes
Upgrade from v1v1 Deprecation
中文