Markdown AST parser for mruby – built with md4c.
The library produces an AST that can then be rendered for the browser or other environments like the terminal but that is left up to the user to do from the AST alone.
The focus on the AST helps keep the core library simple, understandable, and maintainable. If you build your own renderer that uses the AST, consider opening a pull request with a link to your repository.
markdown = Markdown.new("# Hello **world**")
ast = markdown.ast
puts ast[:type] # => :document
puts ast[:children][0][:type] # => :heading
puts ast[:children][0][:level] # => 1The root node is always:
{ type: :document, children: [...] }Common node fields:
:typefor the symbolic node type:kindfor the internal numeric node kind:childrenfor nested nodes:textfor text-node content
Example:
{
kind: 7,
type: :heading,
level: 1,
children: [
{
kind: 27,
type: :text,
text_kind: 0,
text_type: :text,
text: "Hello world"
}
]
}Tables, links, lists, emphasis, code, and other markdown structures are represented as nested hashes and arrays in the same style.
Pass parser flags as the optional second argument to Markdown.new:
flags = Markdown::TABLES | Markdown::STRIKETHROUGH
markdown = Markdown.new(text, flags)Useful constants:
Markdown::DEFAULT_FLAGSMarkdown::TABLESMarkdown::STRIKETHROUGHMarkdown::UNDERLINEMarkdown::COLLAPSE_WHITESPACEMarkdown::NO_HTML_BLOCKSMarkdown::NO_HTML_SPANSMarkdown::TASK_LISTSMarkdown::LATEX_MATHMarkdown::WIKI_LINKS
src/markdown.cdefines theMarkdownclass and public APIsrc/ast.cbuilds the Ruby AST from md4c callbackssrc/md4c/contains the bundled md4c sources
MIT. Bundled md4c is under its own MIT license.