v2.0.11

API Reference

Last updated


Every export of MDParser; the IIFE build exposes a global MDParser, and the ESM build provides MDParser plus named exports of the same names.

Exports


NameSignatureDescription
Parse(content: string) => ASTParses Markdown into an AST
AST.html() => stringRenders the AST to HTML with escaping and raw HTML sanitization
AST.json() => BlockNode[]Returns a deep copy of the nodes as plain JSON data, without html()
Init(options?: InitOptions, endpoint?: DependencyEndpoint) => Promise<void[]>Sets options and loads the enabled dependencies
Refresh(container?: ParentNode) => Promise<void>Renders dependencies and binds copy buttons and file links inside a container already in the document
OnCopyClick(handler: (success: boolean, pre: HTMLElement) => void) => voidRegisters a callback that runs after the built-in success / error feedback; only the last one is kept
DEFAULT_ENDPOINTRequired<DependencyEndpoint>Default dependency URLs
PARSE_OPTIONS{ indent: number; fileEndpoint: string; hashtagEndpoint: string; parseHeading: boolean }The options currently in effect
ParseLines(lines: string[], nested?: boolean) => BlockNode[]The block parser itself; does not resolve footnotes or reference links

InitOptions


FieldTypeDefaultDescription
indentnumber4Column width of a tab
enable{ mermaid?, latex?, highlight?, chartjs? }All falseDependencies to load
fileEndpointstring""URL for file links, must contain {filepath}; see File Links
hashtagEndpointstring""URL for #tag links, must contain {tag}; see Hashtag Links
parseHeadingbooleanfalseRender headings as <h1> to <h6> instead of <span>; only the boolean true enables it

Every Init() call resets the options from that call; omitted fields return to their defaults.

Heading Elements


With parseHeading: true, headings render as real heading elements that keep the same md-h1 to md-h6 classes and look the same; calling Init() without it switches back to <span>.
  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const out = document.getElementById("out");
  4. const md = "# Title\n## Section";
  5. MDParser.Init({ parseHeading: true });
  6. out.innerHTML = MDParser.Parse(md).html();
  7. console.log(out.innerHTML);
  8. MDParser.Init();
  9. console.log(MDParser.Parse(md).html());
  10. </script>
Live preview · @pardnchiu/nanomd@2.0.11

DependencyEndpoint


KeyDefault
mermaidjshttps://cdn.jsdelivr.net/npm/mermaid@11.4.1/dist/mermaid.min.js
katexjshttps://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js
katexcsshttps://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css
googleprettifyjshttps://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js
chartjshttps://cdn.jsdelivr.net/npm/chart.js@4.5.1/dist/chart.umd.min.js

Copy Callback


  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const out = document.getElementById("out");
  4. out.innerHTML = MDParser.Parse("~~~js\nconst a = 1;\n~~~").html();
  5. MDParser.OnCopyClick((success, pre) => {
  6. console.log("copied:", success, pre.getAttribute("name"));
  7. });
  8. MDParser.Refresh(out);
  9. console.log(Object.keys(MDParser).join(", "));
  10. </script>
Live preview · @pardnchiu/nanomd@2.0.11

See AST Nodes for the node types.
中文