Skip to content

Commit

Permalink
feat: prep for pkgdown new version (#110)
Browse files Browse the repository at this point in the history
* feat: prep for pkgdown light switch

* fix dark background

* refactor: use pkgdown's math rendering option instead of custom head.html

* ci: use dev pkgdown @jeroen
  • Loading branch information
maelle authored Jul 5, 2024
1 parent fdbec4d commit d484500
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 125 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ Imports:
curl,
jsonlite,
memoise,
pkgdown (>= 2.0.1),
pkgdown (>= 2.0.9.9000),
purrr,
servr,
yaml,
xml2,
commonmark
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Remotes:
r-lib/pkgdown
41 changes: 29 additions & 12 deletions R/build-site.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ build_ropensci_docs <- function(path = ".", destination = NULL, install = FALSE,

#NB: pkgdown uses utils::modifyList() to merge _pkgdown.yml values with overrides.
#This will recursively merge lists, and delete values that are 'NULL' in overrides.


override <- list(
template = list(
package = "rotemplate",
bootstrap = 5,
params = list(mathjax = need_mathjax(path), bootswatch = NULL),
params = list(bootswatch = NULL),
path = NULL,
bootswatch = NULL
),
Expand All @@ -66,6 +68,11 @@ build_ropensci_docs <- function(path = ".", destination = NULL, install = FALSE,
destination = destination
)

math_config <- get_math_rendering(path)
if (!is.null(math_config)) {
override$template$`math-rendering` <- math_config
}

find_and_fix_readme(path, pkgname)

# Prevent favicon building
Expand Down Expand Up @@ -98,18 +105,28 @@ build_ropensci_docs <- function(path = ".", destination = NULL, install = FALSE,
invisible(pkg$dst_path)
}

need_mathjax <- function(path){
# we need this to keep supporting the old syntax we had set up
# TODO: PR to packages using it so we can remove those lines!
get_math_rendering <- function(path){

pkgdown_yml <- pkgdown_config_path(path = path)
isTRUE(try({
if(!is.null(pkgdown_yml)){
pkgdown_config <- yaml::read_yaml(pkgdown_yml)
if(isTRUE(pkgdown_config$mathjax) || isTRUE(pkgdown_config$template$params$mathjax)){
message("Site needs mathjax library")
return(TRUE)
}
}
message("Site does not need mathjax")
}))
pkgdown_config <- yaml::read_yaml(pkgdown_yml)

if (is.null(pkgdown_config)) {
return(NULL)
}

math_rendering <- pkgdown_config$template$`math-rendering`
if (!is.null(math_rendering)) {
return(math_rendering)
}

if(isTRUE(pkgdown_config$mathjax) || isTRUE(pkgdown_config$template$params$mathjax)){
return("mathjax")
}

return(NULL)

}

find_and_fix_readme <- function(path, pkg){
Expand Down
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@ If your website is not deploying or you run into another problem, please
open an issue in the [ropensci/docs](https://github.com/ropensci/docs)
repository.

### Mathjax
### Math rendering

if you want to use Mathjax you’ll need to specify it in the `pkgdown`
config file like so:
Please refer to [pkgdown documentation](https://pkgdown.r-lib.org/dev/articles/customise.html#math-rendering).

``` yaml
template:
params:
mathjax: true
```

## Example sites

Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ url: https://docs.ropensci.org/rotemplate

template:
package: rotemplate
light-switch: true

destination: docs

Expand Down
10 changes: 8 additions & 2 deletions inst/pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
template:
includes:
in_header: |
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css" />
<script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js" data-cfasync="false"></script>
<script src="https://ropensci.org/scripts/matomo.js"></script>
<noscript><p><img src="https://ropensci.matomo.cloud/matomo.php?idsite=1&amp;rec=1" style="border:0;" alt="" /></p></noscript>
before_title: '<a href="https://ropensci.org"><img src="https://ropensci.org/img/icon_short_white.svg" id="hexlogo" alt="rOpenSci"></a>'
bootstrap: 5
bslib:
bg: "white"
body-bg-dark: "#212529"
fg: "#283140"
primary: "#1b6ada"
dropdown-link-hover-bg: "#64a4ef"
dropdown-link-hover-color: "white"
dropdown-link-active-color: "white"
headings-color: "#5c677e"
headings-color-light: "#484f5e"
navbar-brand-font-size: "2rem"
navbar-light-color: "white"
navbar-light-brand-color: "white"
Expand All @@ -23,7 +29,7 @@ template:
navbar:
structure:
left:
right: [search, intro, reference, articles, tutorials, news, github]
right: [search, intro, reference, articles, tutorials, news, github, lightswitch]


authors:
Expand Down
1 change: 0 additions & 1 deletion inst/pkgdown/extra.scss
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ main h1 img {
/* Because ProximaNova is very thin by default */
h1, h2, h3, h4, h5, h6 {
font-weight: 700;
color: #484f5e;
code {
font-weight: 500;
}
Expand Down
99 changes: 0 additions & 99 deletions inst/pkgdown/templates/head.html

This file was deleted.

4 changes: 2 additions & 2 deletions tests/testthat/test-mathjax.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_that("adding mathjax works", {
)
})
homepage <- xml2::read_html(file.path(docs, "index.html"), encoding = "UTF-8")
script <- xml2::xml_find_first(homepage, ".//head/script[@id='MathJax-script']")
script <- xml2::xml_find_first(homepage, ".//script[contains(@src,'MathJax')]")
# If not present the class is xml_missing
testthat::expect_s3_class(script, "xml_node")
})
Expand All @@ -46,6 +46,6 @@ test_that("NOT adding mathjax works", {
)
})
homepage <- xml2::read_html(file.path(docs, "index.html"), encoding = "UTF-8")
script <- xml2::xml_find_first(homepage, ".//head/script[@id='MathJax-script']")
script <- xml2::xml_find_first(homepage, ".//script[contains(@src,'MathJax')]")
testthat::expect_s3_class(script, "xml_missing")
})

0 comments on commit d484500

Please sign in to comment.