This page explains how parsing works and how to work with the AST that comes back from the parser.
Parser.parse(String) returns an AST rooted at org.dominokit.markdown.node.Node.
The root is usually a Document, and its children represent the block structure of the input. Those block nodes may themselves contain inline children.
Parser parser = Parser.builder().build();
Node document = parser.parse("Hello **world**");
If you are only rendering, you may not need to inspect the tree yourself. If you want to understand or transform the content, the AST is the right place to work.
The node tree behaves like a linked structure:
getFirstChild()getNext()getParent()getLastChild()Example:
Parser parser = Parser.builder().build();
Node document = parser.parse("""
# Title
First paragraph.
""");
Node firstChild = document.getFirstChild();
In this example, the first child is a heading node.
The public node model supports the visitor pattern.
import org.dominokit.markdown.node.AbstractVisitor;
import org.dominokit.markdown.node.Heading;
import org.dominokit.markdown.node.Node;
import org.dominokit.markdown.node.Paragraph;
import org.dominokit.markdown.node.Text;
class MyVisitor extends AbstractVisitor {
@Override
public void visit(Heading heading) {
System.out.println("heading level: " + heading.getLevel());
}
@Override
public void visit(Paragraph paragraph) {
System.out.println("paragraph");
}
@Override
public void visit(Text text) {
System.out.println(text.getLiteral());
}
}
Node document = Parser.builder().build().parse("Hello");
document.accept(new MyVisitor());
Block nodes such as paragraphs and headings can contain inline nodes.
Parser parser = Parser.builder().build();
Node document = parser.parse("This is **bold** text.");
Node paragraph = document.getFirstChild();
Node inline = paragraph.getFirstChild();
Inline children may be:
TextCodeEmphasisStrongEmphasisLinkImageHtmlInlineSource spans are optional. Enable them when you need line and column information for diagnostics, editor integrations, or source mapping.
import org.dominokit.markdown.parser.IncludeSourceSpans;
Parser parser = Parser.builder()
.includeSourceSpans(IncludeSourceSpans.ALL)
.build();
Node document = parser.parse("Hello");
When source spans are enabled, nodes can expose origin information through getSourceSpans().
Parser parser = Parser.builder().build();
Node document = parser.parse("# Title");
Heading heading = (Heading) document.getFirstChild();
int level = heading.getLevel();
Parser parser = Parser.builder().build();
Node document = parser.parse("[OpenAI](https://openai.com)");
Link link = (Link) document.getFirstChild().getFirstChild();
String destination = link.getDestination();
String title = link.getTitle();
Parser parser = Parser.builder().build();
Node document = parser.parse("");
Image image = (Image) document.getFirstChild().getFirstChild();
Parser parser = Parser.builder().build();
Node document = parser.parse("""
- One
- Two
""");
ListBlock list = (ListBlock) document.getFirstChild();
The AST is mutable. That means you can:
This is useful for post-processing, custom transforms, or application-specific rewriting before rendering.
Example:
Parser parser = Parser.builder().build();
Node document = parser.parse("Hello");
Paragraph paragraph = (Paragraph) document.getFirstChild();
paragraph.appendChild(new Text(" world"));
Block parsing decides where paragraphs, headings, lists, quotes, and code blocks begin and end.
Inline parsing then resolves emphasis, links, code spans, and related syntax inside block containers.
Some extensions, such as autolink and task lists, modify the tree after the inline parser has finished.
instanceof chains when walking larger trees.