Skip to content

Commit

Permalink
Re-activate article navbar after building Rmd article (#2683)
Browse files Browse the repository at this point in the history
Fixes #2678
  • Loading branch information
hadley authored Jul 3, 2024
1 parent c2346de commit 47203ec
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 4 additions & 0 deletions R/build-article.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ build_rmarkdown_article <- function(pkg,
input_path = path_dir(input_path),
pkg = pkg
)
# Need re-active navbar now that we now the target path
update_html(path, function(html) {
activate_navbar(html, path_rel(path, pkg$dst_path), pkg)
})
}
if (digest != file_digest(output_path)) {
writing_file(path_rel(output_path, pkg$dst_path), output_file)
Expand Down
4 changes: 1 addition & 3 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ render_page <- function(pkg = ".", name, data, path, depth = NULL, quiet = FALSE
html <- render_page_html(pkg, name = name, data = data, depth = depth)

tweak_page(html, name, pkg = pkg)
if (pkg$bs_version > 3) {
activate_navbar(html, data$output_file %||% path, pkg)
}
activate_navbar(html, data$output_file %||% path, pkg)

rendered <- as.character(html, options = character())
write_if_different(pkg, rendered, path, quiet = quiet)
Expand Down
16 changes: 9 additions & 7 deletions R/tweak-navbar.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
activate_navbar <- function(html, path, pkg = list()) {
activate_navbar <- function(html, path, pkg = list(bs_version = 5)) {
if (pkg$bs_version <= 3) {
return()
}

path <- remove_useless_parts(path, pkg = pkg)

# Get nav items, their links, their similarity to the current path
navbar_haystack <- navbar_links_haystack(html, pkg, path)

# Nothing similar
if (nrow(navbar_haystack) == 0) {
return()
}

# Pick the most similar link, activate the corresponding nav item
tweak_class_prepend(
navbar_haystack$nav_item[which.max(navbar_haystack$similar)][[1]],
"active"
)
best_match <- navbar_haystack[which.max(navbar_haystack$similar), ]
tweak_class_prepend(best_match$nav_item[[1]], "active")

invisible()
}

navbar_links_haystack <- function(html, pkg, path) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-build-article.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ test_that("BS5 article laid out correctly with and without TOC", {
expect_equal(xpath_length(toc_false, ".//aside"), 0)
})

test_that("BS5 article gets correctly activated navbar", {
pkg <- local_pkgdown_site()
pkg <- pkg_add_file(pkg, "vignettes/article.Rmd", pkg_vignette())
suppressMessages(article_path <- build_article("article", pkg))

html <- xml2::read_html(article_path)
navbar <- xml2::xml_find_first(html, ".//div[contains(@class, 'navbar')]")

expect_equal(
xpath_text(navbar,".//li[contains(@class, 'active')]//button"),
"Articles"
)
})

test_that("titles are escaped when needed", {
pkg <- local_pkgdown_site()
pkg <- pkg_add_file(pkg, "vignettes/test.Rmd", pkg_vignette(title = "a <-> b"))
Expand Down

0 comments on commit 47203ec

Please sign in to comment.