commonmark/ast
This module defines the Markdown AST used.
This AST can be used to manipulate markdown documents or render them with your own algorithm.
CommonMark defines two major types of elements which have a hierarchical relationship:
- A Document has many blocks.
- A block contains either other blocks or a list of inline elements
- Inline elements define the textual contents of the document.
Types
Block nodes are used to define the overall structure of a document.
pub type BlockNode {
HorizontalBreak
Heading(level: Int, contents: List(InlineNode))
CodeBlock(
info: Option(String),
full_info: Option(String),
contents: String,
)
HtmlBlock(html: String)
Paragraph(contents: List(InlineNode))
BlockQuote(contents: List(BlockNode))
OrderedList(
contents: List(ListItem),
start: Int,
marker: OrderedListMarker,
)
UnorderedList(
contents: List(ListItem),
marker: UnorderedListMarker,
)
}
Constructors
-
HorizontalBreak -
Heading(level: Int, contents: List(InlineNode)) -
CodeBlock( info: Option(String), full_info: Option(String), contents: String, ) -
HtmlBlock(html: String) -
Paragraph(contents: List(InlineNode)) -
BlockQuote(contents: List(BlockNode)) -
OrderedList( contents: List(ListItem), start: Int, marker: OrderedListMarker, ) -
UnorderedList( contents: List(ListItem), marker: UnorderedListMarker, )
Documents contain all the information necessary to render a document, both structural and metadata.
pub type Document {
Document(blocks: List(BlockNode), references: ReferenceList)
}
Constructors
-
Document(blocks: List(BlockNode), references: ReferenceList)
The emphasis marker used to generate an emphasis inline element.
pub type EmphasisMarker {
AsteriskEmphasisMarker
UnderscoreEmphasisMarker
}
Constructors
-
AsteriskEmphasisMarker -
UnderscoreEmphasisMarker
Inline nodes are used to define the formatting and individual elements that appear in a document.
pub type InlineNode {
CodeSpan(contents: String)
Emphasis(contents: List(InlineNode), marker: EmphasisMarker)
StrongEmphasis(
contents: List(InlineNode),
marker: EmphasisMarker,
)
StrikeThrough(contents: List(InlineNode))
Link(
contents: List(InlineNode),
title: Option(String),
href: String,
)
ReferenceLink(contents: List(InlineNode), ref: String)
Image(alt: String, title: Option(String), href: String)
ReferenceImage(alt: String, ref: String)
UriAutolink(href: String)
EmailAutolink(href: String)
HtmlInline(html: String)
PlainText(contents: String)
HardLineBreak
SoftLineBreak
}
Constructors
-
CodeSpan(contents: String) -
Emphasis(contents: List(InlineNode), marker: EmphasisMarker) -
StrongEmphasis( contents: List(InlineNode), marker: EmphasisMarker, ) -
StrikeThrough(contents: List(InlineNode)) -
Link( contents: List(InlineNode), title: Option(String), href: String, ) -
ReferenceLink(contents: List(InlineNode), ref: String) -
Image(alt: String, title: Option(String), href: String) -
ReferenceImage(alt: String, ref: String) -
UriAutolink(href: String) -
EmailAutolink(href: String) -
HtmlInline(html: String) -
PlainText(contents: String)Literal text content that should be rendered as-is. In particular, this means that the content is not HTML-safe, nor is any other precaution taken for specific renderers. Renderers should take appropriate precautions to make sure that this text is displayed as presented.
Text content shouldn’t normally contain line breaks. See
HardLineBreakandSoftLineBreakfor the canonical representation of line breaks that renderers can make decisions about. The exception isCodeBlock(Text(""))which may contain line breaks (standardised to"\n") as the full block of text inside theCodeBlockis returned as a single pre-formatted text blob. -
HardLineBreak -
SoftLineBreak
Represents a single item in a list.
Standardised markdown rendering should not output paragraph tags inside a tight list item. The intention here is to try to mirror the visual style of the original document, where tight items are grouped together tightly.
pub type ListItem {
ListItem(contents: List(BlockNode))
TightListItem(contents: List(BlockNode))
}
Constructors
-
ListItem(contents: List(BlockNode))A “loose” list item.
-
TightListItem(contents: List(BlockNode))A “tight” list item.
The list marker used to define an ordered list.
pub type OrderedListMarker {
PeriodListMarker
BracketListMarker
}
Constructors
-
PeriodListMarkerThe list used a
.as the marker for the ordered list. -
BracketListMarkerThe list used a
)as the marker for the ordered list.
A reference used with ReferenceLink nodes.
pub type Reference {
Reference(href: String, title: Option(String))
}
Constructors
-
Reference(href: String, title: Option(String))
A dictionary of references keyed by the name of the reference.
pub type ReferenceList =
Dict(String, Reference)
Errors that can occur while rendering.
pub type RenderError {
MissingReference(reference: String)
}
Constructors
-
MissingReference(reference: String)There was a link or image that points to a reference which doesn’t exist.
The list marker used to define an unordered list.
pub type UnorderedListMarker {
DashListMarker
PlusListMarker
AsteriskListMarker
}
Constructors
-
DashListMarkerThe list used a
-as the marker for the unordered list. -
PlusListMarkerThe list used a
+as the marker for the unordered list. -
AsteriskListMarkerThe list used a
*as the marker for the unordered list.