The goal of downlit is to provide syntax highlighting and automatic linking of R code in a way that is easily used from RMarkdown packages like pkgdown, bookdown, and hugodown.


Install downlit from CRAN with:

{r, eval = FALSE} install.packages("downlit")


downlit has two slightly different highlighting/linking engines:

  • highlight() works with multiline code blocks and does syntax highlighting, function linking, and comment styling.
  • autolink() works with inline code and only does linking.

Multiline code blocks have:

  • Code syntax highlighted using R’s parser.
  • Function calls automatically linked to their corresponding documentation.
  • Comments styled by transforming ANSI escapes sequences to their HTML equivalents (thanks fansi package).

The following forms of inline code are recognized and automatically linked:

  • fun(), pkg::fun().
  • ?fun, pkg::fun, type?topic.
  • help("fun"), help("fun", package = "package"), help(package = "package").
  • vignette("name"), vignette("name", package = "package").
  • library(package), require(package), requireNamespace("package").
  • {package} gets linked (if possible) and formatted as plain text.


downlit is designed to be used by other packages, and I expect most uses of downlit will use it via another package (e.g. hugodown). If you want to use it in your own package, you’ll typically want to apply it as part of some bigger transformation process. You can get some sense of how this might work by reading the source code of downlit_html() and downlit_md(), which transform HTML and markdown documents respectively.