Markdown parsing and rendering
Extension point for adding/changing attributes on HTML tags for a node.
A node that uses delimiters in the source form, e.g.
Custom delimiter processor for additional delimiters besides _ and *.
Parser for a type of inline content.
A factory for extending inline content parsing.
Parser for inline content (text, links, emphasized text, etc.)
InlineParserState
A parsed link/image.
A mixin to decide how links/images are handled
Mixin for parser/renderer extensions.
A renderer for a set of node types
Block parsing state.
PostProcessors are run as the last step of parsing and provide an opportunity to inspect/modify the parsed AST before rendering.
Renders a tree of nodes
Sanitizes uris for img and a elements by whitelisting protocols.
Node visitor
A block node.
Resulting object for continuing parsing of a block.
A block parser is able to parse a specific block node.
Parser factory for a block node for determining when a block starts.
Block quote block
Resulting object for starting parsing of a block.
Bullet list block
Code
Custom Block
Custom node
A map that can be used to store and lookup reference definitions by a label.
Delimiter (emphasis, strong emphasis, or custom emphasis)
Document is the root node of the AST
Emphasis
Fenced code block
Hard line break
Heading block
HTML block
Context for rendering nodes to HTML
HTML inline
Renders a tree of nodes to HTML
Builder for configuring an HtmlRenderer .
HTML writer for markdown rendering
Image node
ImgAttrs
Extension for adding attributes to image nodes.
Indented code block
InlineParserContext
A link with a destination and an optional title; the link text is in child nodes
A link reference definition
LinkResult
Abstract base class for list blocks
List item
Context for rendering nodes to Markdown
Renders nodes to Markdown (CommonMark syntax).
Builder for configuring a MarkdownRenderer
Writer for Markdown (CommonMark) text.
Open block parser that was last matched during the continue phase.
Base class for all CommonMark AST nodes.
Ordered list block
Paragraph
ParsedInline
Parse input text into a tree of nodes.
Builder for customizing the behavior of the common mark parser
Scanner is a utility class for parsing lines
Soft line break
A line or portion of a line from the markdown source text
A set of lines (SourceLine ) from the input source.
References a snippet of text from the source input.
A list of source spans that can be added to.
Strong emphasis
Extension for GFM tables using "|" pipes.
Text
Context for rendering nodes to plain text
Renders a tree of nodes to plain text
Builder for configuring a TextRenderer
Text writer for markdown rendering
Thematic break block
Enum for configuring whether to include SourceSpans or not while parsing.
LineBreakRendering
Fantom library for parsing and rendering Markdown text according to the CommonMark specification.
using markdown
parser := Parser()
doc := parser.parse("This is *Markdown*")
renderer := HtmlRenderer()
html := renderer.render(doc) // => "<p>This is <em>Markdown</em></p>\n"
This uses the parser and renderer with default options. Both have builders for configuring their behavior. See ParserBuilder and HtmlRendererBuilder for more details.
Use a visitor to process parsed nodes
After the source text has been parsed, the result is a tree of nodes. That tree can be modified before rendering, or just inspected without rendering. See Visitor and the HTML rendering example below.
You can take complete control over how HTML is rendered.
In this example, we're changing the rendering of indented code blocks to only wrap them in pre instead of pre and code:
parser := Parser()
renderer := HtmlRenderer.builder
.nodeRendererFactory(|HtmlContext cx->NodeRenderer| { IndentedCodeBlockRendere(cx) })
.build
html := renderer.render(parser.parse("Example:\n\n code"))
// "<p>Example:</p><pre>code</pre>\n"
class IndentedCodeBlockRenderer : NodeRenderer, Visitor
{
new make(HtmlContext cx) { this.html = cx.writer }
private HtmlWriter html
** We only want to override rendering of the Code node type
override const Type nodeTypes := [Code#]
override Void render(Node node) { node.walk(this) }
override Void visitIndentedCode(IndentedCode code)
{
html.line
.tag("pre")
.text(code.literal)
.tag("/pre")
.line
}
}
Add your own node types
In case you want to store additional data in the document, or have custom elements in the resulting HTML, you can create your own subclass of CustomNode or CustomBlock and add instances as child nodes to existing nodes.
To define the HTML rendering for them, you can use a NodeRenderer as explained above.
You can also render markdown to plaintext with most formatting removed. The builder for the TextRenderer supports various methods or rendering line breaks depending on your use case.
// Generate plaintext using the default "compact" rendering
doc := Parser().parse("Here, the **emphasis** is ignored")
str := TextRenderer().render(doc) // => Here the emphasis is ignored
Customize parsing
There are a few ways to extend parsing or even override built-in parsing, all of them via methods on Parser.builder . See Section 3 (Blocks and Inlines) of the spec for an overview of blocks/inlines.
withEnabledBlockTypescustomBlockParserFactorycustomInlineContentParserFactorylinkProcessor and linkMarkerThread-safety
Both the Parser and the HtmlRenderer are designed so that you can configure them once and then use them multiple times/from multiple Actors.
Extensions are used to extend the parser, the HTML renderer, or both. Extensions are configured on the various builders using the extensions() method. The default parser and HTML renderer conform to the CommonMark specification. This library includes some built-in extensions that you can optionally enable.
For example, the following code enables the ImgAttrsExt which allows you to specify attributes for images.
using markdown exts := [ImgAttrsExt()] parser := Parser.builder.extensions(exts).build renderer := HtmlRenderer.builder.extensions(exts).build
Image Attributes
Adds support for specifying attributes (specifically height and width) for images. Use ImgAttrsExt to enable this extension.
The attribute elements are given as key=value pairs inside curly braces { } after the image node to which they apply. For example:
{width=640 height=480}
will be rendered as
<img src="/url.png" alt="text" width="640" height="480" />
Tables
Enables tables using pipes as in Github Flavored Markdown. See TablesExt .
| Header1 | Header2 | | ------- | ------- | | foo | bar |
markdown 1.0.82∙26-Jun-2025 Thu 13:18:40 EDT