Parse either a literal markdown string or a markdown file
given a path. Different dialects and features are supported via the flags
argument. See flags_describe()
for possible flags and their usage. parse_md()
defaults parsing using the commonmark spec while parse_gfm()
uses the GitHub
flavored markdown spec.
parse_md(md, flags = "MD_DIALECT_COMMONMARK")
parse_gfm(md, flags = "MD_DIALECT_GITHUB")
Character. Either literal string of markdown or a path to a markdown file.
Character vector. Dialect flags used by the parser.
Both functions return a markdown ast, a list with the md_block_doc
class.
parse_md(system.file("examples/commonmark.md", package = "md4r"))
#> md_block_doc [flags: "MD_DIALECT_COMMONMARK"]
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Try CommonMark"
#> ├── md_block_p
#> │ ├── md_text_normal - "You can try CommonMark here. This dingus is powered by"
#> │ ├── md_text_softbreak
#> │ ├── md_span_a [title: "", href: "https://github.com/commonmark/commonmark.js"]
#> │ │ └── md_text_normal - "commonmark.js"
#> │ ├── md_text_normal - ", the"
#> │ ├── md_text_softbreak
#> │ └── md_text_normal - "JavaScript reference implementation."
#> └── md_block_ol [start: 1, tight: 1, mark_delimiter: "."]
#> ├── md_block_li
#> │ └── md_text_normal - "item one"
#> └── md_block_li
#> ├── md_text_normal - "item two"
#> └── md_block_ul [tight: 1, mark: "-"]
#> ├── md_block_li
#> │ └── md_text_normal - "sublist"
#> └── md_block_li
#> └── md_text_normal - "sublist"
parse_gfm(system.file("examples/github.md", package = "md4r"))
#> md_block_doc [flags: "MD_DIALECT_GITHUB"]
#> ├── md_block_h [level: 1]
#> │ └── md_text_normal - "GitHub Flavored Markdown"
#> ├── md_block_p
#> │ └── md_span_em
#> │ ├── md_text_normal - "View the "
#> │ ├── md_span_a [title: "", href: "http://github.github.com/github-flavored-markdown/sample_content.html"]
#> │ │ └── md_text_normal - "source of this content"
#> │ └── md_text_normal - "."
#> ├── md_block_p
#> │ └── md_text_normal - "Let's get the whole "linebreak" thing out of the way. The next paragraph contains two phrases separated by a single newline character:"
#> ├── md_block_p
#> │ ├── md_text_normal - "Roses are red"
#> │ ├── md_text_softbreak
#> │ └── md_text_normal - "Violets are blue"
#> ├── md_block_p
#> │ └── md_text_normal - "The next paragraph has the same phrases, but now they are separated by two spaces and a newline character:"
#> ├── md_block_p
#> │ ├── md_text_normal - "Roses are red"
#> │ ├── md_text_break - ""
#> │ └── md_text_normal - "Violets are blue"
#> ├── md_block_p
#> │ └── md_text_normal - "Oh, and one thing I cannot stand is the mangling of words with multiple underscores in them like perform_complicated_task or do_this_and_do_that_and_another_thing."
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "A bit of the GitHub spice"
#> ├── md_block_p
#> │ └── md_text_normal - "In addition to the changes in the previous section, certain references are auto-linked:"
#> ├── md_block_ul [tight: 1, mark: "*"]
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "SHA: be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2"
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "User@SHA ref: mojombo@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2"
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "User/Project@SHA: mojombo/god@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2"
#> │ ├── md_block_li
#> │ │ ├── md_text_normal - "#"
#> │ │ └── md_text_normal - "Num: #1"
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "User/#Num: mojombo#1"
#> │ └── md_block_li
#> │ └── md_text_normal - "User/Project#Num: mojombo/god#1"
#> ├── md_block_p
#> │ └── md_text_normal - "These are dangerous goodies though, and we need to make sure email addresses don't get mangled:"
#> ├── md_block_p
#> │ ├── md_text_normal - "My email addy is "
#> │ ├── md_span_a [title: "", href: "mailto:tom@github.com"]
#> │ │ └── md_text_normal - "tom@github.com"
#> │ └── md_text_normal - "."
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Math is hard, let's go shopping"
#> ├── md_block_p
#> │ └── md_text_normal - "In first grade I learned that 5 > 3 and 2 < 7. Maybe some arrows. 1 -> 2 -> 3. 9 <- 8 <- 7."
#> ├── md_block_p
#> │ └── md_text_normal - "Triangles man! a^2 + b^2 = c^2"
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "We all like making lists"
#> ├── md_block_p
#> │ └── md_text_normal - "The above header should be an H2 tag. Now, for a list of fruits:"
#> ├── md_block_ul [tight: 1, mark: "*"]
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "Red Apples"
#> │ ├── md_block_li
#> │ │ └── md_text_normal - "Purple Grapes"
#> │ └── md_block_li
#> │ └── md_text_normal - "Green Kiwifruits"
#> ├── md_block_p
#> │ └── md_text_normal - "Let's get crazy:"
#> ├── md_block_ol [start: 1, tight: 0, mark_delimiter: "."]
#> │ ├── md_block_li
#> │ │ ├── md_block_p
#> │ │ │ ├── md_text_normal - "This is a list item with two paragraphs. Lorem ipsum dolor"
#> │ │ │ ├── md_text_softbreak
#> │ │ │ ├── md_text_normal - "sit amet, consectetuer adipiscing elit. Aliquam hendrerit"
#> │ │ │ ├── md_text_softbreak
#> │ │ │ └── md_text_normal - "mi posuere lectus."
#> │ │ └── md_block_p
#> │ │ ├── md_text_normal - "Vestibulum enim wisi, viverra nec, fringilla in, laoreet"
#> │ │ ├── md_text_softbreak
#> │ │ ├── md_text_normal - "vitae, risus. Donec sit amet nisl. Aliquam semper ipsum"
#> │ │ ├── md_text_softbreak
#> │ │ └── md_text_normal - "sit amet velit."
#> │ └── md_block_li
#> │ └── md_block_p
#> │ └── md_text_normal - "Suspendisse id sem consectetuer libero luctus adipiscing."
#> ├── md_block_p
#> │ ├── md_text_normal - "What about some code "
#> │ ├── md_span_strong
#> │ │ └── md_text_normal - "in"
#> │ └── md_text_normal - " a list? That's insane, right?"
#> ├── md_block_ol [start: 1, tight: 0, mark_delimiter: "."]
#> │ ├── md_block_li
#> │ │ ├── md_block_p
#> │ │ │ └── md_text_normal - "In Ruby you can map like this:"
#> │ │ └── md_block_code [info: "", lang: "", fence_char: ""]
#> │ │ ├── md_text_code - " "
#> │ │ ├── md_text_code - "['a', 'b'].map { |x| x.uppercase }"
#> │ │ └── md_text_code - "\n"
#> │ └── md_block_li
#> │ ├── md_block_p
#> │ │ └── md_text_normal - "In Rails, you can do a shortcut:"
#> │ └── md_block_code [info: "", lang: "", fence_char: ""]
#> │ ├── md_text_code - " "
#> │ ├── md_text_code - "['a', 'b'].map(&:uppercase)"
#> │ └── md_text_code - "\n"
#> ├── md_block_p
#> │ └── md_text_normal - "Some people seem to like definition lists"
#> ├── md_block_html
#> │ ├── md_text_html - "<dl>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<dt>Lower cost</dt>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<dd>The new version of this product costs significantly less than the previous one!</dd>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<dt>Easier to use</dt>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<dd>We've changed the product so that it's much easier to use!</dd>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - "</dl>"
#> │ └── md_text_html - "\n"
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "I am a robot"
#> ├── md_block_p
#> │ ├── md_text_normal - "Maybe you want to print "
#> │ ├── md_span_code
#> │ │ └── md_text_code - "robot"
#> │ └── md_text_normal - " to the console 1000 times. Why not?"
#> ├── md_block_code [info: "", lang: "", fence_char: ""]
#> │ ├── md_text_code - "def robot_invasion"
#> │ ├── md_text_code - "\n"
#> │ ├── md_text_code - " "
#> │ ├── md_text_code - "puts("robot " * 1000)"
#> │ ├── md_text_code - "\n"
#> │ ├── md_text_code - "end"
#> │ └── md_text_code - "\n"
#> ├── md_block_p
#> │ └── md_text_normal - "You see, that was formatted as code because it's been indented by four spaces."
#> ├── md_block_p
#> │ └── md_text_normal - "How about we throw some angle braces and ampersands in there?"
#> ├── md_block_code [info: "", lang: "", fence_char: ""]
#> │ ├── md_text_code - "<div class="footer">"
#> │ ├── md_text_code - "\n"
#> │ ├── md_text_code - " "
#> │ ├── md_text_code - "&copy; 2004 Foo Corporation"
#> │ ├── md_text_code - "\n"
#> │ ├── md_text_code - "</div>"
#> │ └── md_text_code - "\n"
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Set in stone"
#> ├── md_block_p
#> │ └── md_text_normal - "Preformatted blocks are useful for ASCII art:"
#> ├── md_block_html
#> │ ├── md_text_html - "<pre>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - ",-. "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - ", ,-. ,-. "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "/ \ ( )-( ) "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "\ | ,.>-( )-< "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "\|,' ( )-( ) "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "Y ___`-' `-' "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "|/__/ `-' "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "| "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "| "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "| -hrr- "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "___|_____________ "
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - "</pre>"
#> │ └── md_text_html - "\n"
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Playing the blame game"
#> ├── md_block_p
#> │ └── md_text_normal - "If you need to blame someone, the best way to do so is by quoting them:"
#> ├── md_block_quote
#> │ └── md_block_p
#> │ └── md_text_normal - "I, at any rate, am convinced that He does not throw dice."
#> ├── md_block_p
#> │ └── md_text_normal - "Or perhaps someone a little less eloquent:"
#> ├── md_block_quote
#> │ ├── md_block_p
#> │ │ ├── md_text_normal - "I wish you'd have given me this written question ahead of time so I"
#> │ │ ├── md_text_softbreak
#> │ │ ├── md_text_normal - "could plan for it... I'm sure something will pop into my head here in"
#> │ │ ├── md_text_softbreak
#> │ │ ├── md_text_normal - "the midst of this press conference, with all the pressure of trying to"
#> │ │ ├── md_text_softbreak
#> │ │ └── md_text_normal - "come up with answer, but it hadn't yet..."
#> │ └── md_block_p
#> │ ├── md_text_normal - "I don't want to sound like"
#> │ ├── md_text_softbreak
#> │ ├── md_text_normal - "I have made no mistakes. I'm confident I have. I just haven't - you"
#> │ ├── md_text_softbreak
#> │ ├── md_text_normal - "just put me under the spot here, and maybe I'm not as quick on my feet"
#> │ ├── md_text_softbreak
#> │ └── md_text_normal - "as I should be in coming up with one."
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Table for two"
#> ├── md_block_html
#> │ ├── md_text_html - "<table>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<th>ID</th><th>Name</th><th>Rank</th>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "</tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<td>1</td><td>Tom Preston-Werner</td><td>Awesome</td>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "</tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "<td>2</td><td>Albert Einstein</td><td>Nearly as awesome</td>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - " "
#> │ ├── md_text_html - "</tr>"
#> │ ├── md_text_html - "\n"
#> │ ├── md_text_html - "</table>"
#> │ └── md_text_html - "\n"
#> ├── md_block_h [level: 2]
#> │ └── md_text_normal - "Crazy linking action"
#> └── md_block_p
#> ├── md_text_normal - "I get 10 times more traffic from [Google] "
#> ├── md_span_a [title: "Google", href: "http://google.com/"]
#> │ └── md_text_normal - "1"
#> ├── md_text_normal - " than from"
#> ├── md_text_softbreak
#> ├── md_text_normal - "[Yahoo] "
#> ├── md_span_a [title: "Yahoo Search", href: "http://search.yahoo.com/"]
#> │ └── md_text_normal - "2"
#> ├── md_text_normal - " or [MSN] "
#> ├── md_span_a [title: "MSN Search", href: "http://search.msn.com/"]
#> │ └── md_text_normal - "3"
#> └── md_text_normal - "."