From c979e338393c998faaf45a0251f2b5d519d583e6 Mon Sep 17 00:00:00 2001 From: Kamil Zyla Date: Tue, 30 Jul 2024 15:35:39 +0200 Subject: [PATCH 1/3] fix: avoid double search box on Rhino home page The `pkgdown/build.R` script used to replace the `navbar.html` template in order to add a documentation version switcher to the navbar. However, this solution broke down when the template was updated in {pkgdown}. Fix: use `template.includes.before_navbar` to inject the version switcher instead of overwriting the navbar template. This solution has a chance of being more robust against future updates to the template. --- pkgdown/_pkgdown.yml | 2 +- pkgdown/build.R | 56 ++++++++++++++++++++-------- pkgdown/extra.css | 5 +++ pkgdown/navbar_template.html | 71 ------------------------------------ 4 files changed, 47 insertions(+), 87 deletions(-) delete mode 100644 pkgdown/navbar_template.html diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 21c6bdf9..1623bb05 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -22,7 +22,7 @@ template: gtag('js', new Date()); gtag('config', 'G-FQQZL5V93G'); - before_navbar: | + before_title: | url: https://appsilon.github.io/rhino/ diff --git a/pkgdown/build.R b/pkgdown/build.R index 3edc34c9..895b4eb5 100644 --- a/pkgdown/build.R +++ b/pkgdown/build.R @@ -39,31 +39,39 @@ validate_versions <- function(versions) { } build_version_factory <- function(repo, versions, root_url, destination) { - navbar_template <- navbar_template_factory(versions, root_url) + version_switcher <- version_switcher_factory(versions, root_url) destination <- fs::path_abs(destination) extra_css_path <- fs::path_join(c(repo, "pkgdown", "extra.css")) function(version) { # Prepare a worktree for building build_dir <- fs::file_temp("versioned-build-worktree-") - on.exit(system2("git", c("-C", repo, "worktree", "remove", "--force", build_dir))) # NOTE: --force because we add the navbar file + on.exit(system2("git", c("-C", repo, "worktree", "remove", "--force", build_dir))) # NOTE: --force because we overwrite extra.css status <- system2("git", c("-C", repo, "worktree", "add", build_dir, version$git_ref)) if (status != 0) { stop("Failed to create a worktree for ref ", version$git_ref) } - # Write the navbar template and extra.css - template_dir <- fs::path_join(c(build_dir, "pkgdown", "templates")) - fs::dir_create(template_dir) - writeLines(navbar_template(version), fs::path_join(c(template_dir, "navbar.html"))) + # Write extra.css fs::file_copy(extra_css_path, fs::path_join(c(build_dir, "pkgdown", "extra.css")), overwrite = TRUE) # NOTE: providing an absolute path to build_site won't work: https://github.com/r-lib/pkgdown/issues/2172 withr::with_dir(build_dir, { + config <- yaml::read_yaml("pkgdown/_pkgdown.yml") pkgdown::build_site_github_pages( override = list( url = sub("/$", "", url_join(root_url, version$url)), - navbar = list(type = "light") + navbar = list(type = "light"), + template = list( + includes = list( + # Prepend the version switcher to before_navbar instead of overwriting it. + before_navbar = paste( + version_switcher(version), + config$template$includes$before_navbar, + sep = "\n" + ) + ) + ) ), dest_dir = fs::path_join(c(destination, version$url)) ) @@ -79,10 +87,27 @@ url_join <- function(url, path) { ) } -navbar_template_factory <- function(versions, root_url) { - navbar_code <- readLines("pkgdown/navbar_template.html") - index_current <- grep("___CURRENT_PLACEHOLDER___", navbar_code) - index_options <- grep("___OPTIONS_PLACEHOLDER___", navbar_code) +version_switcher_code <- c( + '' +) + +version_switcher_factory <- function(versions, root_url) { + index_current <- grep("___CURRENT_PLACEHOLDER___", version_switcher_code) + index_options <- grep("___OPTIONS_PLACEHOLDER___", version_switcher_code) stopifnot(index_current < index_options) wrap_label <- function(label) { if (isTRUE(label)) { @@ -91,10 +116,10 @@ navbar_template_factory <- function(versions, root_url) { label } function(version) { - c( - navbar_code[1:(index_current - 1)], + lines <- c( + version_switcher_code[1:(index_current - 1)], wrap_label(version$label), - navbar_code[(index_current + 1):(index_options - 1)], + version_switcher_code[(index_current + 1):(index_options - 1)], purrr::map_chr( versions, function(ver) { @@ -105,7 +130,8 @@ navbar_template_factory <- function(versions, root_url) { ) } ), - navbar_code[(index_options + 1):length(navbar_code)] + version_switcher_code[(index_options + 1):length(version_switcher_code)] ) + paste0(lines, collapse = "\n") } } diff --git a/pkgdown/extra.css b/pkgdown/extra.css index 71a00065..c9de13d9 100644 --- a/pkgdown/extra.css +++ b/pkgdown/extra.css @@ -65,6 +65,11 @@ button.btn.btn-primary.btn-copy-ex { } } +/* Hide the version number - it's in the version switcher injected by pkgdown/build.R. */ +.navbar .nav-text { + display: none; +} + #version-switcher { margin-inline-start: 0.5rem; margin-inline-end: auto; diff --git a/pkgdown/navbar_template.html b/pkgdown/navbar_template.html deleted file mode 100644 index e48bae15..00000000 --- a/pkgdown/navbar_template.html +++ /dev/null @@ -1,71 +0,0 @@ -{{#navbar}} - -{{/navbar}} From 25ac6e1c9712905f43e1f33220fe05062d77d762 Mon Sep 17 00:00:00 2001 From: Kamil Zyla Date: Wed, 31 Jul 2024 08:46:06 +0200 Subject: [PATCH 2/3] refactor: use {htmltools} to build version switcher --- pkgdown/build.R | 65 +++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/pkgdown/build.R b/pkgdown/build.R index 895b4eb5..b579f330 100644 --- a/pkgdown/build.R +++ b/pkgdown/build.R @@ -87,51 +87,42 @@ url_join <- function(url, path) { ) } -version_switcher_code <- c( - '' -) - version_switcher_factory <- function(versions, root_url) { - index_current <- grep("___CURRENT_PLACEHOLDER___", version_switcher_code) - index_options <- grep("___OPTIONS_PLACEHOLDER___", version_switcher_code) - stopifnot(index_current < index_options) wrap_label <- function(label) { if (isTRUE(label)) { label <- paste(desc::desc_get_version(), "(dev)") } label } + version_list <- purrr::map( + versions, + function(ver) { + htmltools::tags$li( + htmltools::a( + class = "dropdown-item", + href = url_join(root_url, ver$url), + wrap_label(ver$label) + ) + ) + } + ) function(version) { - lines <- c( - version_switcher_code[1:(index_current - 1)], - wrap_label(version$label), - version_switcher_code[(index_current + 1):(index_options - 1)], - purrr::map_chr( - versions, - function(ver) { - sprintf( - '
  • %s
  • ', - url_join(root_url, ver$url), - wrap_label(ver$label) - ) - } + htmltools::div( + id = "version-switcher", + class = "dropdown", + htmltools::a( + href = "#", + class = "nav-link dropdown-toggle", + role = "button", + `data-bs-toggle` = "dropdown", + `aria-expanded` = "false", + `aria-haspopup` = "true", + wrap_label(version$label) ), - version_switcher_code[(index_options + 1):length(version_switcher_code)] - ) - paste0(lines, collapse = "\n") + htmltools::tags$ul( + class = "dropdown-menu", + version_list + ) + ) |> as.character() } } From 3f8d6bfea19a9791cc16d981737be632931f3f30 Mon Sep 17 00:00:00 2001 From: Kamil Zyla Date: Thu, 1 Aug 2024 13:26:49 +0200 Subject: [PATCH 3/3] ci: use the latest R version in ci.yml --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d64f377..045b002d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,6 @@ jobs: - name: Install R uses: r-lib/actions/setup-r@v2 with: - r-version: '4.2.2' use-public-rspm: true - name: Install R package dependencies