diff --git a/NEWS.md b/NEWS.md index 4bec7b613..9bbda7769 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * New translation for "Search site", the label applied to the search box for screenreaders. This was previously incorrectly labelled as "Toggle navigation" (#2320). * You can now choose where the search box is placed with the "search" navbar component. This has been documented for a very long time, but as far as I can tell, never worked (#2320). If you have made your own template with a custom `navbar`, you will need to remove the `
` with `role="search"` to avoid getting two search boxes. * The mobile version of pkgdown sites no longer has a scrollburglar (a small amount of horizontal scroll) (#2179, @netique). +* The `template.bslib` item now also accepts a `bootswatch` key (@gadenbuie, #2483). # pkgdown 2.0.9 diff --git a/R/theme.R b/R/theme.R index 67fc1743b..d3f188584 100644 --- a/R/theme.R +++ b/R/theme.R @@ -36,7 +36,9 @@ bs_theme <- function(pkg = ".") { bs_theme_args <- pkg$meta$template$bslib %||% list() bs_theme_args[["version"]] <- pkg$bs_version + # In bslib >= 0.5.1, bs_theme() takes bootstrap preset theme via `preset` bs_theme_args[["preset"]] <- get_bslib_theme(pkg) + bs_theme_args[["bootswatch"]] <- NULL bs_theme <- exec(bslib::bs_theme, !!!bs_theme_args) @@ -91,31 +93,35 @@ highlight_styles <- function() { } get_bslib_theme <- function(pkg) { - preset <- pkg$meta[["template"]]$bslib$preset + themes <- list( + "template.bslib.preset" = pkg$meta[["template"]]$bslib$preset, + "template.bslib.bootswatch" = pkg$meta[["template"]]$bslib$bootswatch, + "template.bootswatch" = pkg$meta[["template"]]$bootswatch, + # Historically (< 0.2.0), bootswatch wasn't a top-level template field + "template.params.bootswatch" = pkg$meta[["template"]]$params$bootswatch + ) - if (!is.null(preset)) { - check_bslib_theme(preset, pkg, c("template", "bslib", "preset")) - return(preset) - } + is_present <- !purrr::map_lgl(themes, is.null) + n_present <- sum(is_present) + n_unique <- length(unique(themes[is_present])) - bootswatch <- - pkg$meta[["template"]]$bootswatch %||% - # Historically (< 0.2.0), bootswatch wasn't a top-level template field - pkg$meta[["template"]]$params$bootswatch + if (n_present == 0) { + return("default") + } - if (!is.null(bootswatch)) { - check_bslib_theme(bootswatch, pkg, c("template", "bootswatch")) - return(bootswatch) + if (n_present > 1 && n_unique > 1) { + cli::cli_warn(c( + "Multiple Bootstrap preset themes were set. Using {.val {themes[is_present][[1]]}} from {.field {names(themes)[is_present][1]}}.", + x = "Found {.and {.field {names(themes)[is_present]}}}.", + i = "Remove extraneous theme declarations to avoid this warning." + )) } - "default" + field <- names(themes)[which(is_present)[1]] + check_bslib_theme(themes[[field]], pkg, strsplit(field, ".")[[1]]) } check_bslib_theme <- function(theme, pkg, field = c("template", "bootswatch"), bs_version = pkg$bs_version) { - if (theme %in% c("_default", "default")) { - return("default") - } - bslib_themes <- c( bslib::bootswatch_themes(bs_version), bslib::builtin_themes(bs_version), diff --git a/tests/testthat/_snaps/templates.md b/tests/testthat/_snaps/templates.md index 0a744668b..2b3f3ecdf 100644 --- a/tests/testthat/_snaps/templates.md +++ b/tests/testthat/_snaps/templates.md @@ -16,3 +16,9 @@ Error in `as_pkgdown()`: ! Both template.bootstrap and template.bslib.version are set. +# Warns when Bootstrap theme is specified in multiple locations + + Multiple Bootstrap preset themes were set. Using "flatly" from template.bslib.preset. + x Found template.bslib.preset, template.bslib.bootswatch, template.bootswatch, and template.params.bootswatch. + i Remove extraneous theme declarations to avoid this warning. + diff --git a/tests/testthat/test-render.R b/tests/testthat/test-render.R index c566b16bd..55b1ac327 100644 --- a/tests/testthat/test-render.R +++ b/tests/testthat/test-render.R @@ -1,6 +1,6 @@ test_that("check_bslib_theme() works", { pkg <- as_pkgdown(test_path("assets/reference")) - expect_equal(check_bslib_theme("_default", pkg, bs_version = 4), "default") + expect_equal(check_bslib_theme("default", pkg, bs_version = 4), "default") expect_equal(check_bslib_theme("lux", pkg, bs_version = 4), "lux") expect_snapshot_error(check_bslib_theme("paper", pkg, bs_version = 4)) expect_snapshot_error(check_bslib_theme("paper", pkg, bs_version = 4, field = c("template", "preset"))) diff --git a/tests/testthat/test-templates.R b/tests/testthat/test-templates.R index a79d7aad0..2b0c4faa3 100644 --- a/tests/testthat/test-templates.R +++ b/tests/testthat/test-templates.R @@ -146,3 +146,67 @@ test_that("Valid local Bootstrap version masks invalid template package", { ))) ) }) + +# Bootstrap theme resolution ---------------------------------------------- +test_that("Finds Bootstrap theme in all the places", { + pkg_sketchy <- local_pkgdown_site(meta = ' + template: + bslib: + preset: sketchy + version: 5 + ') + pkg_superhero <- local_pkgdown_site(meta = ' + template: + bslib: + preset: superhero + version: 5 + ') + pkg_cosmo <- local_pkgdown_site(meta = ' + template: + bootstrap: 5 + bootswatch: cosmo + ') + + pkg_yeti <- local_pkgdown_site(meta = ' + template: + bootstrap: 5 + params: + bootswatch: yeti + ') + + expect_equal(get_bslib_theme(pkg_sketchy), "sketchy") + expect_equal(get_bslib_theme(pkg_superhero), "superhero") + expect_equal(get_bslib_theme(pkg_cosmo), "cosmo") + expect_equal(get_bslib_theme(pkg_yeti), "yeti") +}) + +test_that("Warns when Bootstrap theme is specified in multiple locations", { + pkg <- local_pkgdown_site(meta = ' + template: + bootstrap: 5 + bootswatch: cerulean + bslib: + preset: flatly + bootswatch: lux + params: + bootswatch: darkly + ') + + expect_snapshot_warning( + get_bslib_theme(pkg) + ) +}) + +test_that("Doesn't warn when the same Bootstrap theme is specified in multiple locations", { + pkg <- local_pkgdown_site(meta = ' + template: + bootswatch: cerulean + bslib: + preset: cerulean + ') + + expect_equal( + expect_silent(get_bslib_theme(pkg)), + "cerulean" + ) +})