Markdown for MoonBit: CommonMark and GFM to a syntax tree, HTML or Markdown, whole or as it streams in.
moon add moonbitstack/moonmdlet document = @moonmd.parse("# Title\n\nA *short* paragraph with a [link](/a).\n")
let html = document.html(safe=false)
let markdown = document.markdown()<h1>Title</h1>
<p>A <em>short</em> paragraph with a <a href="/a">link</a>.</p>pub(all) enum Block {
Heading(Heading) // level, content
Paragraph(Array[Inline]) // a paragraph of inlines
Code(Code) // info, text, fenced
Quote(Array[Block]) // a block quote
List(ListBlock) // kind, tight, items
Table(Table) // align, head, body
Html(String) // a block of raw HTML
Break // a thematic break
}pub(all) enum Inline {
Text(String)
Code(String)
Emphasis(Array[Inline])
Strong(Array[Inline])
Link(Link)
Image(Link)
Html(String)
Strike(Array[Inline])
Break(Break) // Soft or Hard
}let counts = [0, 0]
@moonmd.walk(document, fn(node) {
match node {
@moonmd.Block(@moonmd.Heading(_)) => counts[0] = counts[0] + 1
@moonmd.Inline(@moonmd.Link(_)) => counts[1] = counts[1] + 1
_ => ()
}
})pub(all) struct Flavor {
tables : Bool // pipe tables
strikethrough : Bool // ~~struck out~~
autolinks : Bool // www.example.com, https://example.com, a@b.example
task_lists : Bool // - [ ] and - [x]
tag_filter : Bool // keep <script> and friends out of a page
}let document = @moonmd.parse(text, flavor=@moonmd.commonmark)let stream = @moonmd.Stream::new()
for chunk in chunks {
for block in stream.feed(chunk) {
render(block) // final already
}
}
for block in stream.finish() {
render(block)
}| Corpus | Examples | Passing |
|---|---|---|
| CommonMark 0.31.2 spec.json | 652 | 652 |
| GitHub Flavored Markdown extensions | 29 | 26 |
pub fn parse(text : StringView, flavor~ : Flavor = flavor) -> Document
pub fn Document::html(Self, safe~ : Bool = true) -> String
pub fn Document::markdown(Self) -> String
pub fn Document::blocks(Self) -> Array[Block]
pub fn Document::flavor(Self) -> Flavor
pub fn Document::of_blocks(blocks : Array[Block], flavor~ : Flavor = flavor) -> Document
pub fn walk(Document, visit : (Node) -> Unit) -> Unit
pub fn Stream::new(flavor~ : Flavor = flavor, defer_links~ : Bool = true) -> Stream
pub fn Stream::feed(Self, chunk : StringView) -> Array[Block]
pub fn Stream::finish(Self) -> Array[Block]pub(all) enum Align {
Default
Left
Center
Right
}pub(all) struct Code {
info : String
text : String
fenced : Bool
}pub struct Document {
// private fields
}pub(all) struct Flavor {
tables : Bool
strikethrough : Bool
autolinks : Bool
task_lists : Bool
tag_filter : Bool
}pub(all) enum ListKind {
Bullet(Char)
Ordered(Int, Char)
}pub struct Stream {
// private fields
}pub(all) enum Task {
Plain
Unchecked
Checked
}test {
let document = @moonmd.parse("# Title")
assert_eq(document.html(safe=false), "<h1>Title</h1>\n")
}Install
Download zipMarkdown for MoonBit: CommonMark and GFM to a syntax tree, HTML or Markdown, whole or as it streams in.