app/models/markdown.rb
This example demonstrates how you could parse a Commonmarker markdown document into an abstract syntax tree to customize the output. Each node
has a type
to switch on behavior and an each
method for iterating over its direct children.
ruby
require "commonmarker"
def visit(node)
return if node.nil?
case node.type
in :document
visit_children(node)
in :heading
header(node.header_level) { visit_children(node) }
in :paragraph
p { visit_children(node) }
# ...
end
end
def visit_children(node)
node.each { |c| visit(c) }
end
visit(Commonmarker.parse("# Hello World\n\nHappy Friday!"))