Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use before_navbar to fix the double search box #604

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
71 changes: 44 additions & 27 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,33 +87,42 @@ 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)
stopifnot(index_current < index_options)
version_switcher_factory <- function(versions, root_url) {
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) {
c(
navbar_code[1:(index_current - 1)],
wrap_label(version$label),
navbar_code[(index_current + 1):(index_options - 1)],
purrr::map_chr(
versions,
function(ver) {
sprintf(
'<li><a class="dropdown-item" href="%s">%s</a></li>',
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)
),
navbar_code[(index_options + 1):length(navbar_code)]
)
htmltools::tags$ul(
class = "dropdown-menu",
version_list
)
) |> as.character()
}
}
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.

Loading