diff --git a/NEWS.md b/NEWS.md index f0564caee..9cceae534 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # pkgdown (development version) +* Support for math rendering using katex and mathjax was improved and now includes necessary components using CDN (#2704). +* Articles (i.e., Rmarkdown/Quarto documents in `vignettes/articles`, created by `usethis::use_article()` and available on pkgdown sites but not included in a built package) have improved test cases (thanks to @venpopov and @ethanbass). * pkgdown now uses the same Sass variables as bslib to set the navbar background color: `$navbar-bg` for the background color in light and dark mode, or `$navbar-light-bg` and `$navbar-dark-bg` for individually controlling light and dark mode background colors. `pkgdown-navbar-bg` and `$pkgdown-navbar-bg-dark` both still provide pkgdown-specific overrides (@gadenbuie, #2847). * Code repositories hosted on Codeberg are now supported in the `BugReports` and `URL` fields (@nfrerebeau, #2843). * Articles (i.e., vignettes in `vignettes/articles`, created by `usethis::use_article()` and available on pkgdown sites but not included in a built package) have improved test cases (thanks to @venpopov and @ethanbass). diff --git a/R/external-deps.R b/R/external-deps.R index c084bf7c1..39596f62b 100644 --- a/R/external-deps.R +++ b/R/external-deps.R @@ -70,21 +70,6 @@ math_dependency <- function(pkg, call = caller_env()) { ) ) ) - } else if (math == "katex") { - cached_dependency( - name = "KaTex", - version = "0.16.10", - files = list( - list( - url = "https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.js", - integrity = "sha384-hIoBPJpTUs74ddyc4bFZSM1TVlQDA60VBbJS0oA934VSz82sBx1X7kSx2ATBDIyd" - ), - list( - url = "https://cdn.jsdelivr.net/npm/katex@0.16.10/dist/katex.min.css", - integrity = "sha384-wcIxkf4k558AjM3Yz3BBFQUbk/zgIYC2R0QpeeYb+TwlBVMrlgLqwRjRtGZiK7ww" - ) - ) - ) } else { NULL } @@ -136,7 +121,7 @@ compute_hash <- function(path, size) { con <- file(path, encoding = "UTF-8") openssl::base64_encode(openssl::sha2(con, size)) } - + parse_integrity <- function(x) { size <- as.integer(regmatches(x, regexpr("(?<=^sha)\\d{3}", x, perl = TRUE))) hash <- regmatches(x, regexpr("(?<=^sha\\d{3}-).+", x, perl = TRUE)) diff --git a/R/markdown.R b/R/markdown.R index 8fae5ef21..b5dabba5b 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -109,6 +109,7 @@ convert_markdown_to_html <- function(pkg, in_path, out_path, ...) { cli::cli_abort("Pandoc not available") } } + rmarkdown::pandoc_convert( input = in_path, output = out_path, diff --git a/R/render.R b/R/render.R index f64fcdfbb..ab7a7c972 100644 --- a/R/render.R +++ b/R/render.R @@ -128,6 +128,7 @@ data_template <- function(pkg = ".", depth = 0L) { out$footer <- data_footer(pkg) out$lightswitch <- uses_lightswitch(pkg) out$uses_katex <- config_math_rendering(pkg) == "katex" + out$uses_mathjax <- config_math_rendering(pkg) == "mathjax" print_yaml(out) } diff --git a/R/test.R b/R/test.R index db9928ddd..986f0ee4b 100644 --- a/R/test.R +++ b/R/test.R @@ -285,6 +285,18 @@ NULL #' #' \deqn{y = \alpha + \beta X + \varepsilon} #' +#' Multi-line equation (correctly rendered by katex only): +#' +#' \deqn{\mathit{Minimize} \space l \\ +#' \mathit{subject \space to} \\ +#' \sum_{i = 1}^{I} x_i r_{ij} + y_j \geq t_j \forall j \in J \\ +#' l \geq \frac{y_j}{t_j} \forall j \in J \\ +#' \sum_{i = 1}^{I} x_i c_i \leq B}{ +#' Minimize l subject to +#' sum_i^I (xi * rij) + yj >= tj for all j in J & +#' l >= (yj / tj) for all j in J & +#' sum_i^I (xi * ci) <= B} +#' #' @name test-math-examples #' @keywords internal #' @family tests diff --git a/inst/BS5/templates/head.html b/inst/BS5/templates/head.html index 94f3e394b..6f1131a7f 100644 --- a/inst/BS5/templates/head.html +++ b/inst/BS5/templates/head.html @@ -12,8 +12,26 @@ {{/has_favicons}} +{{#uses_katex}} + + + + + +{{/uses_katex}} + +{{#uses_mathjax}} + + +{{/uses_mathjax}} + {{#lightswitch}}{{/lightswitch}} -{{#uses_katex}}{{/uses_katex}} {{{headdeps}}} {{#includes}}{{{head}}}{{/includes}} diff --git a/man/test-math-examples.Rd b/man/test-math-examples.Rd index de64ccaae..f20ade3c0 100644 --- a/man/test-math-examples.Rd +++ b/man/test-math-examples.Rd @@ -13,6 +13,18 @@ Test case: math rendering in examples Display equation: \deqn{y = \alpha + \beta X + \varepsilon} + +Multi-line equation (correctly rendered by katex only): + +\deqn{\mathit{Minimize} \space l \\ +\mathit{subject \space to} \\ +\sum_{i = 1}^{I} x_i r_{ij} + y_j \geq t_j \forall j \in J \\ +l \geq \frac{y_j}{t_j} \forall j \in J \\ +\sum_{i = 1}^{I} x_i c_i \leq B}{ +Minimize l subject to +sum_i^I (xi * rij) + yj >= tj for all j in J & +l >= (yj / tj) for all j in J & +sum_i^I (xi * ci) <= B} } \seealso{ Other tests: diff --git a/tests/testthat/_snaps/render.md b/tests/testthat/_snaps/render.md index d7c109778..ac5482509 100644 --- a/tests/testthat/_snaps/render.md +++ b/tests/testthat/_snaps/render.md @@ -49,6 +49,7 @@ right:

Site built with pkgdown {version}.

lightswitch: no uses_katex: no + uses_mathjax: no # check_opengraph validates inputs diff --git a/tests/testthat/test-build-article.R b/tests/testthat/test-build-article.R index 5429d9251..9155ca9d7 100644 --- a/tests/testthat/test-build-article.R +++ b/tests/testthat/test-build-article.R @@ -141,7 +141,7 @@ test_that("can control math mode", { expect_equal(xpath_length(html, ".//span[contains(@class, 'math')]"), 1) expect_contains( path_file(xpath_attr(html, ".//script", "src")), - c("katex-auto.js", "katex.min.js") + c("katex-auto.js", "auto-render.min.js", "katex.min.js") ) }) diff --git a/vignettes/customise.Rmd b/vignettes/customise.Rmd index 1d5cb9951..eef3d9837 100644 --- a/vignettes/customise.Rmd +++ b/vignettes/customise.Rmd @@ -226,7 +226,7 @@ template: ### Math rendering -By default, pkgdown will render math using mathml. mathml is the official standard for rendering math on the web, and requires no additional javascript or css dependencies. However, browser support for complex math is not always that good, so if you are including complex equations in your documentation, you may want to switch to either [`katex`](https://katex.org) or [`mathjax`](https://www.mathjax.org) by using the `template.math-rendering` field: +By default, pkgdown will render math using [mathml](https://w3c.github.io/mathml/). mathml is the official standard for rendering math on the web, and requires no additional javascript or css dependencies. However, browser support for complex math is not always that good, so if you are including complex equations in your documentation, you may want to switch to either [`katex`](https://katex.org), which we recommend for display of complex math expressions, or [`mathjax`](https://www.mathjax.org) by using the `template.math-rendering` field: ```yaml template: