v2.0.6

pardnchiu/NanoMD

Last updated

NoteThis README was generated by SKILL, get the ZH version from here.



FAST MARKDOWN TO AST AND HTML IN ONE PARSER!


npmDownloadsLicense




A TypeScript Markdown parser with a JSON AST, on-demand Mermaid/KaTeX/Chart.js rendering, and sanitized HTML output

Table of Contents



Features


npm i @pardnchiu/nanomd · Documentation · v1 Deprecation · Docs Site

  • Single-Pass Line Parser — Each line is scanned once to build the whole tree; a 4 MB document renders in about 150 ms, at least 7 times faster than v1 and more than 230 times faster at 1 MB.
  • Renderer-Agnostic AST — Parse() returns a node tree and json() copies it out as plain data; the web and SSR use html() directly, and native renderers for iOS or Flutter can be built on the same output.
  • Extended Syntax Beyond GFM — Footnotes, reference links, GitHub alerts, task lists, inline and block math, image sizing, video embeds and #tag links are built in.
  • On-Demand Dependencies with Fallbacks — Only the enabled Mermaid, KaTeX, Chart.js and prettify load, and invalid blocks fall back to plain code blocks.
  • Safe by Default — Text and attributes are escaped at output, and raw HTML loses dangerous tags, event attributes and javascript: URLs.

v1 vs v2


The same sample document, repeated to each size, rendered to HTML in headless Chrome; runs over 15 seconds are recorded as timeouts, and their ratios use 15 seconds as a lower bound.
Sizev1.11.6 MDParser().parse()v2 Parse().html()Speedup
4.4 KB2.6 ms0.4 ms7×
44.3 KB29.6 ms1.8 ms16×
443.4 KB1,790 ms17.7 ms101×
1.08 MB11,037 ms47.4 ms233×
2.16 MBTimeout82.3 ms182× or more
4.33 MBTimeout156.4 ms96× or more

Architecture


Full Architecture

graph LR
    Input[Markdown text] --> Block[Block parsing ParseLines]
    Block --> Inline[Inline parsing ParseInline]
    Inline --> Post[Footnote and link-ref post-processing]
    Post --> AST[AST]
    AST --> HTML["AST.html()"]
    AST --> JSON["AST.json()"]
    AST --> Native["Native rendering iOS/Flutter (build your own)"]
    HTML --> Refresh["Refresh() dependency rendering"]
    Init["Init() loads dependencies"] --> Refresh
  1. graph LR
  2. Input[Markdown text] --> Block[Block parsing ParseLines]
  3. Block --> Inline[Inline parsing ParseInline]
  4. Inline --> Post[Footnote and link-ref post-processing]
  5. Post --> AST[AST]
  6. AST --> HTML["AST.html()"]
  7. AST --> JSON["AST.json()"]
  8. AST --> Native["Native rendering iOS/Flutter (build your own)"]
  9. HTML --> Refresh["Refresh() dependency rendering"]
  10. Init["Init() loads dependencies"] --> Refresh

License


This project is licensed under the MIT LICENSE.

Author


Just open an issue to share an idea.
NanoMD contributors


©️ 2024 邱敬幃 Pardn Chiu
中文