High Performance CommonMark and Github Markdown Rendering in R

[This article was first published on rOpenSci Blog - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This week the folks at Github have open sourced their fork of libcmark (based on the extensive PR by Mathieu Duponchelle), which they use to render markdown text within documents, issues, comments and anything else on the Github website. The new release of the commonmark R package incorporates this library so that we can take advantage of Github quality markdown rendering in R.

The most exciting change is that the library has gained an extension mechanism to provide optional rendering features which are missing from the commonmark spec. Most notably, Github has added extentions for rendering GFM style tables and autolinks, both very useful features for R users.


What is CommonMark

CommonMark is an initiative by Professor John MacFarlane (author of Pandoc) to formally define markdown syntax specification and provide high quality reference implementations. His high performance cmark C library converts markdown text into xml, html, latex or groff man in the blink of an eye.

The commonmark R package wraps libcmark and has been on CRAN for a while. It should replace the old markdown package which is based on the deprecated Sundown library (the author of Sundown now works for Github and is one of the contributors to libcmark).

CommonMark Extensions

Commonmark is extremely reliable and optimized, but the disadvantage of the formal specification is that it can take a long time for new feature proposals to be adopted. For example a proposal for markdown tables has been open for over two years, but ironing out the syntax details has proven to be difficult.

For this reason many commonmark implementations include 'extensions' to enable features which are not (yet) part of the official specification. The current version of the commonmark R package offers 4 such extensions:

  • table support rendering of tables
  • strikethough via ~sometext~ syntax
  • autolink automatically turn URLs into hyperlinks
  • tagfilter blacklist html tags: title textarea style xmp iframe noembed noframes script plaintext.

These extensions were added by Github to support GitHub Flavored Markdown.


Note that extensions are not enabled by default. For example to render tables, you need to set the extensions parameter to TRUE or "table".

Markdown and Roxygen

We also use commonmark in the upcoming version 6.0 of roxygen2 to support markdown within roxygen, which is super awesome.


To give this a try you need the development version of roxygen2:


To enable markdown roxygen for a given package, all you need to do is add a line with Roxygen: list(markdown = TRUE) to the package DESCRIPTION file. For more details check out the vignette. There is also package roxygen2md which will automatically convert existing roxygen code to markdown.

To leave a comment for the author, please follow the link and comment on their blog: rOpenSci Blog - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)