///|
test "basic parsing" {
let input =
#|# Hello World
#|
#|This is a paragraph.
let doc = @cmark.Doc::from_string(input)
guard doc.block is Blocks(blocks) else { fail("expected multiple blocks") }
guard blocks.v.to_array()
is [Heading(heading), BlankLine(_), Paragraph(paragraph)] else {
fail("expected a heading followed by a paragraph")
}
@test.assert_eq(heading.v.level, 1)
guard heading.v.inline is Text(title) else { fail("expected heading text") }
guard paragraph.v.inline is Text(text) else {
fail("expected paragraph text")
}
@test.assert_eq(title.v, "Hello World")
@test.assert_eq(text.v, "This is a paragraph.")
}///|
test "emphasis and strong emphasis" {
let doc = @cmark.Doc::from_string("_Emphasis_ and **strong emphasis**")
guard doc.block is Paragraph(paragraph) else { fail("expected a paragraph") }
guard paragraph.v.inline is Inlines(inlines) else {
fail("expected inline elements")
}
guard inlines.v.to_array()
is [Emphasis(emphasis), Text(separator), StrongEmphasis(strong)] else {
fail("expected emphasis and strong emphasis")
}
guard emphasis.v.inline is Text(text) else { fail("expected emphasis text") }
guard strong.v.inline is Text(strong_text) else {
fail("expected strong emphasis text")
}
@test.assert_eq((emphasis.v.delim, text.v), ('_', "Emphasis"))
@test.assert_eq(separator.v, " and ")
@test.assert_eq((strong.v.delim, strong_text.v), ('*', "strong emphasis"))
}///|
test "inline code and math" {
let input =
$|$E = mc^2$ in Python: `E = m * (c ** 2)`
let doc = @cmark.Doc::from_string(input, strict=false)
guard doc.block is Paragraph(paragraph) else { fail("expected a paragraph") }
guard paragraph.v.inline is Inlines(inlines) else {
fail("expected inline elements")
}
guard inlines.v.to_array()
is [ExtMathSpan(math), Text(separator), CodeSpan(code)] else {
fail("expected inline math and code")
}
@test.assert_eq((math.v.display, math.v.tex()), (false, "E = mc^2"))
@test.assert_eq(separator.v, " in Python: ")
@test.assert_eq(code.v.code(), "E = m * (c ** 2)")
}///|
test "headings" {
let doc = @cmark.Doc::from_string("# Level 1")
guard doc.block is Heading(heading) else { fail("expected a heading") }
guard heading.v.inline is Text(text) else { fail("expected heading text") }
@test.assert_eq((heading.v.level, text.v), (1, "Level 1"))
}///|
test "lists" {
let input =
#|- First item
#|- Second item
#| 1. Nested item
let doc = @cmark.Doc::from_string(input)
guard doc.block is List(list) else { fail("expected a list") }
guard list.v.ty is Unordered('-') else { fail("expected an unordered list") }
guard list.v.items.to_array() is [first, second] else {
fail("expected two items")
}
guard first.v.block is Paragraph({ v: { inline: Text(text), .. }, .. }) else {
fail("expected the first item's paragraph")
}
@test.assert_eq(text.v, "First item")
guard second.v.block is Blocks(blocks) else { fail("expected a nested list") }
guard blocks.v.to_array()
is [Paragraph({ v: { inline: Text(text), .. }, .. }), List(nested)] else {
fail("expected a paragraph followed by a nested list")
}
@test.assert_eq(text.v, "Second item")
guard nested.v.ty is Ordered(1, '.') else { fail("expected an ordered list") }
guard nested.v.items.to_array()
is [
{ v: { block: Paragraph({ v: { inline: Text(text), .. }, .. }), .. }, .. },
] else {
fail("expected the nested item's paragraph")
}
@test.assert_eq(text.v, "Nested item")
}///|
test "code blocks" {
let tick3 = "`".repeat(3)
let input =
$|\{tick3}moonbit
$|fn main {
$| println("Hello")
$|}
$|\{tick3}
let doc = @cmark.Doc::from_string(input)
guard doc.block is CodeBlock(code) else { fail("expected a code block") }
guard code.v.layout is Fenced(_) else { fail("expected fenced code") }
@test.assert_eq(code.v.info_string.map(fn(info) { info.v }), Some("moonbit"))
@test.assert_eq(code.v.code.to_array().map(fn(line) { line.v }), [
"fn main {", " println(\"Hello\")", "}",
])
}///|
test "tables" {
let input =
#|| Header 1 | Header 2 |
#||----------|----------|
#|| Cell 1 | Cell 2 |
let doc = @cmark.Doc::from_string(input, strict=false)
guard doc.block is ExtTable(table) else { fail("expected a table") }
@test.assert_eq(table.v.col_count, 2)
guard table.v.rows.to_array() is [(header, _), (separator, _), (data, _)] else {
fail("expected a header, separator and data row")
}
guard header.v is Header(cells) else { fail("expected a header row") }
guard cells.to_array() is [(Text(first), _), (Text(second), _)] else {
fail("expected two header cells")
}
@test.assert_eq((first.v, second.v), ("Header 1", "Header 2"))
guard separator.v is Sep(columns) else { fail("expected a separator row") }
guard columns.to_array()
is [{ v: TableSep((None, 10)), .. }, { v: TableSep((None, 10)), .. }] else {
fail("expected two unaligned separator cells with ten dashes")
}
guard data.v is Data(cells) else { fail("expected a data row") }
guard cells.to_array() is [(Text(first), _), (Text(second), _)] else {
fail("expected two data cells")
}
@test.assert_eq((first.v, second.v), ("Cell 1", "Cell 2"))
}///|
test "footnotes" {
let input =
#|Text[^1]
#|
#|[^1]: Footnote content
let doc = @cmark.Doc::from_string(input, strict=false)
guard doc.block is Blocks(blocks) else { fail("expected multiple blocks") }
guard blocks.v.to_array()
is [Paragraph(paragraph), BlankLine(_), ExtFootnoteDefinition(footnote)] else {
fail("expected a footnote reference and definition")
}
guard paragraph.v.inline is Inlines(inlines) else {
fail("expected a footnote reference")
}
guard inlines.v.to_array() is [Text(text), Link(reference)] else {
fail("expected text followed by a footnote reference")
}
@test.assert_eq(text.v, "Text")
@test.assert_eq(
reference.v.referenced_label().map(fn(label) { label.key }),
Some("^1"),
)
@test.assert_eq(footnote.v.label.key, "^1")
@test.assert_eq(doc.defs.length(), 1)
guard doc.defs.get("^1") is Some(FootnoteDef(definition)) else {
fail("expected a registered footnote definition")
}
guard definition.v.block is Paragraph({ v: { inline: Text(text), .. }, .. }) else {
fail("expected footnote text")
}
@test.assert_eq(text.v, "Footnote content")
}type BlockBlankLine = Stringtype InlineText = Stringtest {
let map = { 3: "three", 8: "eight", 1: "one" }
@test.assert_eq(map.get(2), None)
@test.assert_eq(map.get(3), Some("three"))
map.set(3, "updated")
@test.assert_eq(map.get(3), Some("updated"))
}type LabelKey = Stringpub(all) enum Block {
BlankLine(Node[String])
BlockQuote(Node[BlockQuote])
Blocks(Node[Seq[Block]])
CodeBlock(Node[CodeBlock])
Heading(Node[BlockHeading])
HtmlBlock(Node[HtmlBlock])
LinkRefDefinition(Node[LinkDefinition])
List(Node[BlockList])
Paragraph(Node[BlockParagraph])
ThematicBreak(Node[BlockThematicBreak])
ExtMathBlock(Node[CodeBlock])
ExtTable(Node[Table])
ExtFootnoteDefinition(Node[Footnote])
} derive(Debug)pub(all) struct BlockHeading {
layout : BlockHeadingLayout
level : Int
inline : Inline
id : BlockHeadingId?
} derive(Debug)fn BlockHeading::new(id? : BlockHeadingId?, layout? : BlockHeadingLayout, level~ : Int, inline : Inline) -> BlockHeadingpub(all) enum BlockHeadingLayout {
Atx(BlockHeadingAtxLayout)
Setext(BlockHeadingSetextLayout)
} derive(Debug)fn BlockParagraph::new(leading_indent? : Int, trailing_blanks? : String, inline : Inline) -> BlockParagraphpub(all) struct CodeBlock {
layout : CodeBlockLayout
info_string : Node[String]?
code : Seq[Node[String]]
} derive(Debug)fn Doc::from_string(defs? : Map[String, LabelDef], resolver? : LabelResolverFn, nested_links? : Bool, heading_auto_ids? : Bool, layout? : Bool, locs? : Bool, file? : String, strict? : Bool, math_delimiters? : MathDelimiters, s : String) -> Docimpl Default for FolderResult[A]pub(all) enum Inline {
Autolink(Node[InlineAutolink])
Break(Node[InlineBreak])
CodeSpan(Node[InlineCodeSpan])
Emphasis(Node[InlineEmphasis])
Image(Node[InlineLink])
Inlines(Node[Seq[Inline]])
Link(Node[InlineLink])
RawHtml(Node[InlineRawHtml])
StrongEmphasis(Node[InlineEmphasis])
Text(Node[String])
ExtStrikethrough(Node[InlineStrikethrough])
ExtMathSpan(Node[InlineMathSpan])
} derive(Eq, Debug)pub(all) struct InlineBreak {
layout_before : Node[String]
ty : InlineBreakType
layout_after : Node[String]
} derive(Eq, Debug)fn InlineBreak::new(layout_before? : Node[String], layout_after? : Node[String], ty : InlineBreakType) -> InlineBreakfn LinkDefinition::new(layout? : LinkDefinitionLayout, label? : Label?, defined_label? : Label?, dest? : Node[String]?, title? : Seq[Tight]?) -> LinkDefinitionpub(all) enum ListTaskStatus {
Cancelled
Checked
Unchecked
Other(Char)
}impl Default for MapperResult[A]pub(all) enum MathDelimiters {
Dollar
Latex
Both
}pub(all) enum ReferenceKind {
Inline(Node[LinkDefinition])
Ref(ReferenceLayout, Label, Label)
} derive(Eq, Debug)pub(all) enum TableRow {
Header(Seq[(Inline, TableCellLayout)])
Sep(Seq[Node[TableSep]])
Data(Seq[(Inline, TableCellLayout)])
} derive(Debug)fn is_punctuation(c : Char) -> Boolfn is_whitespace(c : Char) -> Bool