v2.0.16

AST 節點

最後更新


Parse() 回傳的 AST 是 BlockNode 陣列(繼承 Array),本頁列出每種節點的欄位與對應語法。

區塊節點(BlockNode)


type欄位對應語法
textchildren: InlineNode[]一般段落;相鄰行以值為 "\n" 的 text 節點連接
headinglevel、children#~######(移除結尾的 # 序列)、Setext
blockquotetag?、children: BlockNode[]>;tag 為 note/tip/important/warning/caution
listordered、start?、children: ListItem[]-、*、1.;有序清單不從 1 開始時才有 start
tablealign、children: TableRow[]GFM 表格;align 每欄為 left/center/right/null
codeBlocklang?、value```/~~~ 圍欄;未閉合時沒有 lang
htmlvalue以 HTML 標籤開頭的行(原文,輸出時才清洗)
hr—水平線
break—空行
footnoteschildren: Footnote[]文末註腳區

行內節點(InlineNode)


type欄位對應語法
textvalue純文字
b、em、del、mark、sub、supchildren粗體、斜體、刪除線、標記、下標、上標
codevalue行內程式碼;可位於樣式節點的 children 內
mathvalue$...$
linkhref、tag?、title?、width?、height?、children連結(http(s)、mailto:、tel:、相對路徑)、圖片、影片、裸網址、<https://...> 自動連結、檔案路徑、#tag;tag 為 image/video/youtube/vimeo/file/hashtag(href 為不含 # 的標籤)
footnoteRefid、index[^id],index 為編號
br—段落或表格儲存格內的 <br>

子節點


型別欄位
ListItemtype: "listItem"、checked?(任務清單)、children: BlockNode[]
TableRowtype: "tableRow"、header、children: TableCell[]
TableCelltype: "tableCell"、children: InlineNode[]
Footnotetype: "footnote"、id、index、children: BlockNode[]

json() 回傳節點的深拷貝,為一般的 BlockNode[]:沒有 html()、undefined 欄位會被移除,修改它也不影響原本的 AST,可直接儲存或交給其他平台的渲染器。型別定義隨套件發佈於 dist/types/types.d.ts。
  1. <script>
  2. const ast = MDParser.Parse("## Hi\n\n- [x] done\n\nE = $mc^2$ [link](https://nanomd.pardn.io)");
  3. console.log(JSON.stringify(ast.json(), null, 2));
  4. </script>
即時預覽 · @pardnchiu/nanomd@2.0.16



Important給 AI agent: 產生或修改使用 NanoMD 的程式碼或 Markdown 前,先閱讀 NanoMD 生成指南(Markdown),內含支援的語法、必要的呼叫順序與會讓渲染失敗的常見錯誤。