Operate on the Abstract Syntax Tree (AST)
masterThe markdown function allows you to manipulate the resulting Abstract Syntax Tree (AST) instead of just receiving a rendered string.
- Initialize a custom root: Create a
Document()object and pass it to themarkdownfunction using theroot=argument. - Modify the tree: Once
markdown(input, root=root)is called, therootobject is populated with the parsed nodes. You can manually append new nodes (likeParagraph,Em, orText) to the tree usingappendChild(). - Render the modified tree: Use the
render(root)function to convert your modified AST into an HTML string.
let root = Document()
discard markdown("# Hello World\nTest.", root=root)
# Manually add nodes to the AST
let p = Paragraph(loose: true)
let em = Em()
let text = Text(doc: "emphasis text.")
em.appendChild(text)
p.appendChild(em)
root.appendChild(p)
# Render the modified tree
echo render(root)