v2.0.11

Code and Diagrams

Last updated


This page covers the fence rules for code blocks and the three blocks that get extra rendering: mermaid, math and chartjs.

Fences


RuleDescription
Opening3 or more ` or ~, optionally followed by a language tag
ClosingThe same character, at least as many as the opening
NestingUse a longer or different fence outside; an inner ``` does not close it early
Language tagLowercased; common languages get a language-xxx class for highlighting and a label in the top-left corner
IndentationAligned to the first line; the same width is removed from every line
UnclosedRuns to the end of the document, without a language tag

Every code block has a copy button that shows copied for a second after a click, and OnCopyClick receives the result; see API Reference.

Blocks with Extra Rendering


The language tag must equal one of these exactly (mermaid-js, latex and the like are plain code blocks):
TagRendererContent
mermaidMermaidFlowcharts, sequence diagrams and other Mermaid syntax
mathKaTeXBlock math
chartjsChart.jsJSON with a string type and an object data

These blocks show only a plain code block until Refresh() renders them; with invalid syntax, an incomplete chartjs config or a missing dependency, they keep showing the code block.

Examples


Fences


Backtick and tilde fences, nested fences, longer closing fences and a fence right after a list.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```js
  4. const a = "<b>not converted</b>";
  5. ```
  6. ~~~js
  7. const b = "~~~ is also a code block";
  8. ~~~
  9. ~~~md
  10. Inside ~~~ you can write ``` as is; only ~~~ closes it
  11. ```js
  12. const c = 1;
  13. ```
  14. ~~~
  15. ````md
  16. An outer fence of 4 backticks is not closed early by ```
  17. ```chartjs
  18. { "type": "bar" }
  19. ```
  20. ````
  21. ```
  22. A closing fence can be longer
  23. `````
  24. - List item
  25. ~~~
  26. ~~~ right after a list is not merged into the item
  27. ~~~
  28. </script>
  29. <script>
  30. const out = document.getElementById("out");
  31. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  32. MDParser.Init({ enable: { highlight: true } })
  33. .then(() => MDParser.Refresh(out))
  34. .catch((err) => console.error(err.message));
  35. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Mermaid


A flowchart and a sequence diagram.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```mermaid
  4. flowchart LR
  5. A[Markdown input] --> B{Block?}
  6. B -->|Yes| C[ParseLines]
  7. B -->|No| D[ParseInline]
  8. C --> E["html()"]
  9. D --> E
  10. ```
  11. ```mermaid
  12. sequenceDiagram
  13. User->>Editor: type text
  14. Editor->>Parser: Parse()
  15. Parser-->>Editor: AST
  16. ```
  17. </script>
  18. <script>
  19. const out = document.getElementById("out");
  20. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  21. MDParser.Init({ enable: { mermaid: true } })
  22. .then(() => MDParser.Refresh(out))
  23. .catch((err) => console.error(err.message));
  24. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Block Math


A KaTeX display formula.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```math
  4. \int_0^\infty e^{-x^2} \, dx = \frac{\sqrt{\pi}}{2}
  5. ```
  6. </script>
  7. <script>
  8. const out = document.getElementById("out");
  9. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  10. MDParser.Init({ enable: { latex: true } })
  11. .then(() => MDParser.Refresh(out))
  12. .catch((err) => console.error(err.message));
  13. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Chart.js


Bar, pie and bubble charts from JSON.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```chartjs
  4. {
  5. "type": "bar",
  6. "data": {
  7. "labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
  8. "datasets": [{ "label": "# test", "data": [12, 19, 3, 5, 2, 3] }]
  9. }
  10. }
  11. ```
  12. ```chartjs
  13. {
  14. "type": "pie",
  15. "data": {
  16. "labels": ["A", "B", "C"],
  17. "datasets": [{ "data": [30, 50, 20] }]
  18. }
  19. }
  20. ```
  21. ```chartjs
  22. {
  23. "type": "bubble",
  24. "data": {
  25. "datasets": [
  26. { "label": "A", "data": [{ "x": 10, "y": 20, "r": 8 }, { "x": 25, "y": 35, "r": 15 }, { "x": 40, "y": 10, "r": 5 }] },
  27. { "label": "B", "data": [{ "x": 15, "y": 30, "r": 10 }, { "x": 30, "y": 15, "r": 12 }, { "x": 45, "y": 40, "r": 6 }] }
  28. ]
  29. }
  30. }
  31. ```
  32. </script>
  33. <script>
  34. const out = document.getElementById("out");
  35. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  36. MDParser.Init({ enable: { chartjs: true } })
  37. .then(() => MDParser.Refresh(out))
  38. .catch((err) => console.error(err.message));
  39. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Exact Language Tags


Only mermaid, math and chartjs get extra rendering; these stay plain code blocks.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```latex
  4. E = mc^2
  5. ```
  6. ```mathjax
  7. E = mc^2
  8. ```
  9. ```mermaid-js
  10. flowchart LR
  11. A --> B
  12. ```
  13. </script>
  14. <script>
  15. const out = document.getElementById("out");
  16. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  17. MDParser.Init({ enable: { mermaid: true, latex: true, highlight: true } })
  18. .then(() => MDParser.Refresh(out))
  19. .catch((err) => console.error(err.message));
  20. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Fallbacks


Invalid Mermaid, invalid math, a chart without data and an unregistered chart type all fall back to code blocks.
  1. <div id="out" class="nanomd"></div>
  2. <script type="text/markdown" id="md">
  3. ```mermaid
  4. This is not Mermaid syntax
  5. ```
  6. ```math
  7. \frac{1}{
  8. ```
  9. ```chartjs
  10. { "type": "bar" }
  11. ```
  12. ```chartjs
  13. { "type": "unknown", "data": { "datasets": [{ "data": [1, 2, 3] }] } }
  14. ```
  15. </script>
  16. <script>
  17. const out = document.getElementById("out");
  18. out.innerHTML = MDParser.Parse(document.getElementById("md").textContent).html();
  19. MDParser.Init({ enable: { mermaid: true, latex: true, chartjs: true } })
  20. .then(() => MDParser.Refresh(out))
  21. .then(() => { console.log("rendered:", out.querySelectorAll("[data-rendered]").length, "code blocks:", out.querySelectorAll(".nanomd > pre.prettyprint").length); })
  22. .catch((err) => console.error(err.message));
  23. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文