| 1 |
#' Convert to GitHub Flavored Markdown |
| 2 |
#' |
| 3 |
#' Format for converting from R Markdown to GitHub Flavored Markdown. |
| 4 |
#' |
| 5 |
#' See the [online |
| 6 |
#' documentation](https://rmarkdown.rstudio.com/github_document_format.html) for additional details on using the `github_document()` |
| 7 |
#' format. |
| 8 |
#' @inheritParams output_format |
| 9 |
#' @inheritParams html_document |
| 10 |
#' @inheritParams md_document |
| 11 |
#' @param math_method `"default"` means that [native Github |
| 12 |
#' support](https://github.blog/changelog/2022-05-19-render-mathematical-expressions-in-markdown/) |
| 13 |
#' for math notations using Mathjax syntax will be used. Other possible value |
| 14 |
#' is `"webtex"` where equation will be rendered to an image in the resulting |
| 15 |
#' Markdown. See [html_document()] for option to change webtex URL. Set `math_method`to |
| 16 |
#' `NULL` to opt-out any math treatment. |
| 17 |
#' @param hard_line_breaks `TRUE` to generate markdown that uses a simple |
| 18 |
#' newline to represent a line break (as opposed to two-spaces and a newline). |
| 19 |
#' @param html_preview `TRUE` to also generate an HTML file for the purpose of |
| 20 |
#' locally previewing what the document will look like on GitHub. |
| 21 |
#' @param keep_html `TRUE` to keep the preview HTML file in the working |
| 22 |
#' directory. Default is `FALSE`. |
| 23 |
#' |
| 24 |
#' @details # About Math support |
| 25 |
#' |
| 26 |
#' Default behavior is to keep any inline equation using `$` and any block |
| 27 |
#' equation using `$$` in the resulting markdown as Github will process those |
| 28 |
#' using Mathjax. **This feature is only available with Pandoc 2.10.1 and above** |
| 29 |
#' |
| 30 |
#' When using `"webtex"`, PNG images with a white background are used by default so |
| 31 |
#' that it shows correctly on Github on both light and dark theme. You can |
| 32 |
#' choose to only output SVG for better quality by changing the URL used: |
| 33 |
#' |
| 34 |
#' ```yaml |
| 35 |
#' output: |
| 36 |
#' github_document: |
| 37 |
#' math_method: |
| 38 |
#' engine: webtex |
| 39 |
#' url: https://latex.codecogs.com/svg.image? |
| 40 |
#' ``` |
| 41 |
#' |
| 42 |
#' Background or fonts color cannot be changed for now and your equation may not be visible on dark theme. |
| 43 |
#' |
| 44 |
#' Using `"webtex"` will be the default with Pandoc 2.0.4 until Pandoc 2.10. Before 2.0.4, Github document output does not support math. |
| 45 |
#' |
| 46 |
#' @return R Markdown output format to pass to [render()] |
| 47 |
#' @export |
| 48 |
#' @md |
| 49 |
github_document <- function(toc = FALSE, |
| 50 |
toc_depth = 3, |
| 51 |
number_sections = FALSE, |
| 52 |
math_method = "default", |
| 53 |
preserve_yaml = FALSE, |
| 54 |
fig_width = 7, |
| 55 |
fig_height = 5, |
| 56 |
dev = 'png', |
| 57 |
df_print = "default", |
| 58 |
includes = NULL, |
| 59 |
md_extensions = NULL, |
| 60 |
hard_line_breaks = TRUE, |
| 61 |
pandoc_args = NULL, |
| 62 |
html_preview = TRUE, |
| 63 |
keep_html = FALSE) { |
| 64 |
|
| 65 |
# add special markdown rendering template to ensure we include the title fields |
| 66 |
# and add an optional feature to number sections |
| 67 |
pandoc_args <- c( |
| 68 |
pandoc_args, "--template", |
| 69 |
pkg_file_arg("rmarkdown/templates/github_document/resources/default.md") |
| 70 |
) |
| 71 |
|
| 72 |
pandoc2 <- pandoc2.0() |
| 73 |
# use md_document as base |
| 74 |
if (pandoc2) { |
| 75 |
variant <- "gfm" |
| 76 |
} else { |
| 77 |
variant <- "markdown_github" |
| 78 |
} |
| 79 |
if (!hard_line_breaks) variant <- paste0(variant, "-hard_line_breaks") |
| 80 |
|
| 81 |
# atx headers are the default in pandoc 2.11.2 and the flag has been deprecated |
| 82 |
# to be replaced by `--markdown-headings=atx|setx` |
| 83 |
if (!pandoc_available("2.11.2")) pandoc_args <- c( |
| 84 |
"--atx-headers", pandoc_args |
| 85 |
) |
| 86 |
|
| 87 |
if ((toc || number_sections) && |
| 88 |
!isTRUE(grepl("gfm_auto_identifiers", md_extensions)) && |
| 89 |
pandoc2) { |
| 90 |
md_extensions <- c(md_extensions, "+gfm_auto_identifiers") |
| 91 |
} |
| 92 |
|
| 93 |
# math support |
| 94 |
if (!is.null(math_method) && pandoc_available("2.0.4")) { |
| 95 |
math <- check_math_argument(math_method) |
| 96 |
preview_math <- NULL |
| 97 |
if (!math$engine %in% c("default", "webtex")) { |
| 98 |
stop("Markdown output format only support 'default' for native Github Math support or 'webtex' for using a Webtex online service to render math.") |
| 99 |
} |
| 100 |
if (math$engine == "default") { |
| 101 |
if (pandoc_available("2.10.1")) { |
| 102 |
# don't activate math in Pandoc and pass it as is |
| 103 |
# https://github.blog/changelog/2022-05-19-render-mathematical-expressions-in-markdown/ |
| 104 |
math <- NULL |
| 105 |
# Pandoc 3.4 uses +tex_math_gfm by default to write special gfm syntax. We choose to keep $$ and $ which are still supported by Github |
| 106 |
variant <- if (pandoc_available("3.4")) paste0(variant, "-tex_math_gfm") else paste0(variant, "+tex_math_dollars") |
| 107 |
preview_math <- check_math_argument("mathjax") |
| 108 |
} else { |
| 109 |
# fallback to webtex |
| 110 |
math <- check_math_argument("webtex") |
| 111 |
} |
| 112 |
} |
| 113 |
if (!is.null(math) && math$engine == "webtex") { |
| 114 |
if (is.null(math$url)) { |
| 115 |
# default to png and white background for webtex |
| 116 |
math$url <- "https://latex.codecogs.com/png.image?%5Cdpi%7B110%7D&space;%5Cbg_white&space;" |
| 117 |
} |
| 118 |
preview_math <- check_math_argument("webtex") |
| 119 |
} |
| 120 |
math <- add_math_support(math, NULL, NULL, NULL) |
| 121 |
preview_math <- add_math_support(preview_math, NULL, NULL, NULL) |
| 122 |
pandoc_args <- c(pandoc_args, math$args) |
| 123 |
} |
| 124 |
|
| 125 |
format <- md_document( |
| 126 |
variant = variant, toc = toc, toc_depth = toc_depth, |
| 127 |
number_sections = number_sections, preserve_yaml = preserve_yaml, |
| 128 |
fig_width = fig_width, fig_height = fig_height, |
| 129 |
dev = dev, df_print = df_print, includes = includes, |
| 130 |
md_extensions = md_extensions, pandoc_args = pandoc_args |
| 131 |
) |
| 132 |
|
| 133 |
# add a post processor for generating a preview if requested |
| 134 |
post <- format$post_processor |
| 135 |
|
| 136 |
format$post_processor <- function(metadata, input_file, output_file, clean, verbose) { |
| 137 |
|
| 138 |
if (is.function(post)) output_file <- post(metadata, input_file, output_file, clean, verbose) |
| 139 |
|
| 140 |
if (html_preview) { |
| 141 |
css <- pkg_file_arg( |
| 142 |
"rmarkdown/templates/github_document/resources/github.css") |
| 143 |
# provide a preview that looks like github |
| 144 |
args <- c( |
| 145 |
self_contained_args(), pandoc_highlight_args("pygments"), |
| 146 |
"--template", pkg_file_arg( |
| 147 |
"rmarkdown/templates/github_document/resources/preview.html"), |
| 148 |
"--variable", paste0("github-markdown-css:", css), |
| 149 |
if (pandoc2) c("--metadata", "pagetitle=PREVIEW"), # HTML5 requirement |
| 150 |
if (!is.null(preview_math)) preview_math$args |
| 151 |
) |
| 152 |
|
| 153 |
# run pandoc |
| 154 |
preview_file <- file_with_ext(output_file, "html") |
| 155 |
pandoc_convert( |
| 156 |
input = output_file, to = "html", from = variant, output = preview_file, |
| 157 |
options = args, verbose = verbose |
| 158 |
) |
| 159 |
|
| 160 |
# move the preview to the preview_dir if specified |
| 161 |
if (!keep_html) { |
| 162 |
preview_dir <- Sys.getenv("RMARKDOWN_PREVIEW_DIR", unset = NA) |
| 163 |
if (!is.na(preview_dir)) { |
| 164 |
relocated_preview_file <- tempfile("preview-", preview_dir, ".html") |
| 165 |
file.copy(preview_file, relocated_preview_file) |
| 166 |
file.remove(preview_file) |
| 167 |
preview_file <- relocated_preview_file |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (verbose) message("\nPreview created: ", preview_file) |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
output_file |
| 176 |
} |
| 177 |
|
| 178 |
format # return format |
| 179 |
} |
| 180 |
|
| 181 |
# explicit definition of gfm_format -- not currently used b/c it didn't |
| 182 |
# yield different about that 'gfm' w/ pandoc 2.11. keeping in the source |
| 183 |
# code for now in case we need to use it for another workaround. |
| 184 |
gfm_format <- function() { |
| 185 |
paste0("markdown_strict", |
| 186 |
# commonmark |
| 187 |
"+raw_html", |
| 188 |
"+all_symbols_escapable", |
| 189 |
"+backtick_code_blocks", |
| 190 |
"+fenced_code_blocks", |
| 191 |
"+space_in_atx_header", |
| 192 |
"+intraword_underscores", |
| 193 |
"+lists_without_preceding_blankline", |
| 194 |
"+shortcut_reference_links", |
| 195 |
# gfm extensions |
| 196 |
"+auto_identifiers", |
| 197 |
"+autolink_bare_uris", |
| 198 |
"+emoji", |
| 199 |
"+gfm_auto_identifiers", |
| 200 |
"+pipe_tables", |
| 201 |
"+strikeout", |
| 202 |
"+task_lists" |
| 203 |
) |
| 204 |
} |