Skip to content

Commit

Permalink
fix: avoid double search box on Rhino home page
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kamilzyla committed Jul 31, 2024
1 parent b95565c commit c979e33
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 87 deletions.
2 changes: 1 addition & 1 deletion pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ template:
gtag('js', new Date());
gtag('config', 'G-FQQZL5V93G');
</script>
before_navbar: |
before_title: |
<a href="https://rhinoverse.dev" class="home"><i class="fa fa-lg fa-chevron-left"></i></a>
url: https://appsilon.github.io/rhino/
Expand Down
56 changes: 41 additions & 15 deletions pkgdown/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
Expand All @@ -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(
'<div id="version-switcher" class="dropdown">',
' <a',
' href="#"',
' class="nav-link dropdown-toggle"',
' data-bs-toggle="dropdown"',
' role="button"',
' aria-expanded="false"',
' aria-haspopup="true"',
' >',
' ___CURRENT_PLACEHOLDER___',
' </a>',
' <ul class="dropdown-menu">',
' ___OPTIONS_PLACEHOLDER___',
' </ul>',
'</div>'
)

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)) {
Expand All @@ -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) {
Expand All @@ -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")
}
}
5 changes: 5 additions & 0 deletions pkgdown/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 0 additions & 71 deletions pkgdown/navbar_template.html

This file was deleted.

0 comments on commit c979e33

Please sign in to comment.