v2.0.1

HTML 輸出

最後更新


AST.html() 把 Parse() 的結果轉成 HTML 字串;本頁說明輸出的結構與 class,以及它與 JSON AST 的分工。

用法


  1. const html = MDParser.Parse(markdown).html();
  2. container.innerHTML = html;

html() 不需要 DOM,可在 Node.js、Worker 產生 HTML;放進頁面後再呼叫 Refresh() 渲染圖表與數學式,見 依賴與 Refresh。

輸出結構


節點HTML
段落<span class="md-line">
標題<span class="md-h1">~<span class="md-h6">
空行<br>
粗體<span class="md-bold">;其餘行內樣式使用 <em>、<del>、<mark>、<sub>、<sup>
引言/提示框<blockquote class="md-quote">;提示框加上 md-alert md-alert-{tag} 與 md-alert-title
清單<ul class="md-list">/<ol class="md-list">,任務項目為 <li class="md-task">
表格<div class="md-table-wrap"><table class="md-table">
程式碼區塊<pre class="prettyprint" name="{lang}">,每行一個 <li>,附 <button class="copy">
mermaid/math/chartjsmd-mermaid/md-latex/md-chart 外殼,內含備用的程式碼區塊
連結<a class="md-link" target="_blank" rel="noopener">;圖片與影片為 md-img,YouTube/Vimeo 附 md-embed
註腳md-footnote-ref 與 md-footnotes

完整樣式見 樣式主題。

與 JSON AST 的分工


需求使用
在網頁或 SSR 顯示html()
交給 iOS、Flutter 等原生渲染器JSON.stringify(ast),渲染器需自行開發
檢查或改寫內容後再輸出直接操作 AST 陣列,再呼叫 html()

  1. <div id="out" class="nanomd"></div>
  2. <script>
  3. const ast = MDParser.Parse("# Title\n\n> [!NOTE]\n> Edited before output.");
  4. ast[0].children.push({ type: "text", value: " (edited)" });
  5. document.getElementById("out").innerHTML = ast.html();
  6. console.log(ast.html());
  7. </script>
即時預覽 · @pardnchiu/nanomd@2.0.1
EN