Markdown with YAML frontmatter and traversal APIs for Moonbase
Dependencies
let doc = @mdc.Document::Document("---\ntitle: Goal\n---\n# Goal\n")
// doc.frontmatter() contains the raw YAML and a parsed Yaml mapping.
// doc.body() is exactly the Markdown after the closing delimiter line.let headings = doc.fold(0, @mdc.Folder::Folder((count, node) => {
match node {
Block(view) => match view.kind() {
Heading(_) => Continue(count + 1)
_ => Continue(count)
}
_ => Continue(count)
}
}))let deepest = doc.fold(0, @mdc.Folder::with_cursor((deepest, cursor) => {
Continue(if cursor.depth() > deepest { cursor.depth() } else { deepest })
}))let nodes = doc.reduce((_, children : ReadOnlyArray[Int]) => {
1 + children.fold(init=0, (total, count) => total + count)
})| Trait | Required method | Entry point |
|---|---|---|
| Fold | enter(Self, Node) -> FoldAction[Self] | fold_with(root, initial) or doc.fold_with(initial) |
| CursorFold | enter(Self, Cursor) -> FoldAction[Self] | fold_cursor_with(root, initial) or doc.fold_cursor_with(initial) |
| Reducer | reduce(Node, ReadOnlyArray[Self]) -> Self | reduce_with(root) or doc.reduce_with() |
| CursorReducer | reduce(Cursor, ReadOnlyArray[Self]) -> Self | reduce_cursor_with(root) or doc.reduce_cursor_with() |
struct Tracked[A] {
value : A
count : Int
}
impl[A : @mdc.Reducer] @mdc.Reducer for Tracked[A] with fn reduce(node, children) {
{
value: @mdc.Reducer::reduce(node, children.map(child => child.value)),
count: 1 + children.fold(init=0, (sum, child) => sum + child.count),
}
}pub(open) trait Visitor {
fn visit_document(Self, Document) -> Control = _
fn visit_frontmatter(Self, Frontmatter) -> Control = _
fn visit_block(Self, BlockView) -> Control = _
fn visit_inline(Self, InlineView) -> Control = _
fn leave(Self, Node) -> Unit = _
}pub(all) suberror ParseError {
UnterminatedFrontmatter
InvalidFrontmatter(line~ : Int, column~ : Int, message~ : String)
} derive(Debug)type Cursorfn Document::Document(source : String, strict? : Bool, layout? : Bool, locs? : Bool, file? : String) -> Document raise ParseErrortype Folder[A]pub(all) enum Node {
Document(Document)
Frontmatter(Frontmatter)
Block(BlockView)
Inline(InlineView)
} derive(Debug)fn parse(source : String, strict? : Bool, layout? : Bool, locs? : Bool, file? : String) -> Document raise ParseErrorInstall
Download zipMarkdown with YAML frontmatter and traversal APIs for Moonbase
Dependencies