| Title: | Markdown Parser Implemented using the 'MD4C' Library |
|---|---|
| Description: | Provides an R wrapper for the 'MD4C' (Markdown for 'C') library. Functions exist for parsing markdown ('CommonMark' compliant) along with support for other common markdown extensions (e.g. 'GitHub' flavored markdown, 'LaTeX' equation support, etc.). The package also provides a number of higher level functions for exploring and manipulating markdown abstract syntax trees as well as translating and displaying the documents. |
| Authors: | Colin Rundel [aut, cre], Martin Mitáš [cph] (md4c author: md4c.c, md4c.h, specs/md4c/), RStudio, PBC [cph] (httpuv_url_tools.cpp), John MacFarlane [cph] (specs/gfm/spec.txt, specs/md4c/spec.txt) |
| Maintainer: | Colin Rundel <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.3.0 |
| Built: | 2026-06-05 06:12:44 UTC |
| Source: | https://github.com/rundel/md4r |
The md4c library supports a number of markdown
variants / options. The parsing of these is controlled by flags passed to the
parser. The following functions provide commonly used utilities for these flags.
flags_available() flags_describe() flags_used(md)flags_available() flags_describe() flags_used(md)
md |
Markdown ast object |
flags_available() returns a character vector of available flags accepted by parse_md().
flags_describe() returns a tibble with columns flag and description describing each flag.
flags_used() returns a character vector of flags used in a parsed markdown document.
flags_available() flags_describe() md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) flags_used(md)flags_available() flags_describe() md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) flags_used(md)
These functions are used to create block nodes. Blocks nodes are used to represent block level elements in markdown.
md_block_doc(..., flags = "MD_DIALECT_COMMONMARK") md_block_quote(...) md_block_html(...) md_block_p(...) md_block_hr(...) md_block_code(..., info = "", lang = "", fence_char = "`") md_block_ul(..., tight = 1, mark = "*") md_block_ol(..., tight = 1L, start = 1, mark_delimiter = ".") md_block_li(..., is_task = 0, task_mark = " ") md_block_h(..., level) md_block_table(..., col_count, body_row_count, head_row_count = 1) md_block_thead(...) md_block_tbody(...) md_block_tr(...) md_block_th(..., align = c("default", "left", "center", "right")) md_block_td(..., align = c("default", "left", "center", "right"))md_block_doc(..., flags = "MD_DIALECT_COMMONMARK") md_block_quote(...) md_block_html(...) md_block_p(...) md_block_hr(...) md_block_code(..., info = "", lang = "", fence_char = "`") md_block_ul(..., tight = 1, mark = "*") md_block_ol(..., tight = 1L, start = 1, mark_delimiter = ".") md_block_li(..., is_task = 0, task_mark = " ") md_block_h(..., level) md_block_table(..., col_count, body_row_count, head_row_count = 1) md_block_thead(...) md_block_tbody(...) md_block_tr(...) md_block_th(..., align = c("default", "left", "center", "right")) md_block_td(..., align = c("default", "left", "center", "right"))
... |
Child nodes that will be contained within the block - all must inherit from |
flags |
Used by |
info |
Used by |
lang |
Used by |
fence_char |
Used by |
tight |
Used by |
mark |
Used by |
start |
Used by |
mark_delimiter |
Used by |
is_task |
Used by |
task_mark |
Used by |
level |
Used by |
col_count |
Used by |
body_row_count |
Used by |
head_row_count |
Used by |
align |
Used by |
Returns a list with a class of specified type along with md_span and md_node.
md_node objectsOverrides the $ operator so the content and attributes of
md_node objects can be read and updated by name. Use node$content to
access or replace the children of a block or span node (or the text of a
text node), and node$<attr> to read or update an existing node attribute
(e.g. level, href, mark).
Only attributes already present on the node may be replaced, and the new
value is validated against the same rules used by the corresponding
md_block_* / md_span_* constructor. The class and names attributes
cannot be set this way.
## S3 method for class 'md_node' x$name ## S3 replacement method for class 'md_node' x$name <- value ## S3 replacement method for class 'md_text' x$name <- value## S3 method for class 'md_node' x$name ## S3 replacement method for class 'md_node' x$name <- value ## S3 replacement method for class 'md_text' x$name <- value
x |
An object that inherits from |
name |
A character string giving the attribute to access, or
|
value |
Replacement value for the content or attribute. |
A modified md_node object.
md = md_text_normal("Hello World") md$content = "Nice to meet you" md md = md_block_ul( md_block_li(md_text_normal("bullet 1")), md_block_li(md_text_normal("bullet 2")) ) md$mark = "-" md$content[[1]]$content[[1]]$content = "first" to_md(md) |> cat(sep = "\n")md = md_text_normal("Hello World") md$content = "Nice to meet you" md md = md_block_ul( md_block_li(md_text_normal("bullet 1")), md_block_li(md_text_normal("bullet 2")) ) md$mark = "-" md$content[[1]]$content[[1]]$content = "first" to_md(md) |> cat(sep = "\n")
These functions are used to create span nodes. Span nodes are used to represent inline elements in markdown and include things like links, images, code, emphasized text, strong text, and more.
md_span_em(...) md_span_strong(...) md_span_a(..., href, title) md_span_img(..., src, title) md_span_code(...) md_span_del(...) md_span_latexmath(...) md_span_latexmath_display(...) md_span_wikilink(..., target) md_span_u(...)md_span_em(...) md_span_strong(...) md_span_a(..., href, title) md_span_img(..., src, title) md_span_code(...) md_span_del(...) md_span_latexmath(...) md_span_latexmath_display(...) md_span_wikilink(..., target) md_span_u(...)
... |
Child nodes that will be contained within the span - all must inherit from |
href |
Used by |
title |
Used by |
src |
Used by |
target |
Used by |
Returns a list with a class of specified type along with md_span and md_node.
These functions are used to create text nodes. Text nodes are used to represent textual elements in markdown.
md_text_normal(x) md_text_nullchar() md_text_break() md_text_softbreak() md_text_entity(x) md_text_code(x) md_text_html(x) md_text_latexmath(x)md_text_normal(x) md_text_nullchar() md_text_break() md_text_softbreak() md_text_entity(x) md_text_code(x) md_text_html(x) md_text_latexmath(x)
x |
Text content of the node. |
Returns a character vector with a class of specified type along with md_text and md_node.
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")parse_md(md, flags = "MD_DIALECT_COMMONMARK") parse_gfm(md, flags = "MD_DIALECT_GITHUB")
md |
Character. Either literal string of markdown or a path to a markdown file. |
flags |
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")) parse_gfm(system.file("examples/github.md", package = "md4r"))parse_md(system.file("examples/commonmark.md", package = "md4r")) parse_gfm(system.file("examples/github.md", package = "md4r"))
Coverts a markdown object (full ast or node) to HTML.
to_html(md, ...)to_html(md, ...)
md |
Markdown object |
... |
Unused, for extensibility. |
Returns a character vector of HTML lines representing the markdown object.
md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) cat(to_html(md), sep="\n")md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) cat(to_html(md), sep="\n")
Coverts a markdown object (full ast or node) to markdown text.
to_md(md, ...)to_md(md, ...)
md |
Markdown object |
... |
Unused, for extensibility. |
Returns a character vector of markdown lines representing the markdown object.
md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) cat(to_md(md), sep="\n")md_file = system.file("examples/commonmark.md", package = "md4r") md = parse_md(md_file) cat(to_md(md), sep="\n")