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 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..b579f330 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,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( - '
  • %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) ), - navbar_code[(index_options + 1):length(navbar_code)] - ) + htmltools::tags$ul( + class = "dropdown-menu", + version_list + ) + ) |> as.character() } } 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}}