From 5aaa2c1bd5fb4913407c18f72ffc8670bd18e2d5 Mon Sep 17 00:00:00 2001 From: froggleston Date: Fri, 22 Nov 2024 13:59:35 +0000 Subject: [PATCH] Add support for disabling sidebar automatic numbering --- R/utils-sidebar.R | 25 +++++++++++++++++++++---- R/utils-varnish.R | 22 ++++++++++++++++++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/R/utils-sidebar.R b/R/utils-sidebar.R index 6a5d93f3..3454a04d 100644 --- a/R/utils-sidebar.R +++ b/R/utils-sidebar.R @@ -144,6 +144,9 @@ create_sidebar_headings <- function(nodes) { #' @param name the name of the current chapter #' @param html the html of the current chapter. defaults to a link that will #' produce a sidebar with no links to headings. +#' @param disable_numbering a boolean indicating if the sidebar should not automatically +#' number the chapters. Defaults to `FALSE`. If `TRUE`, developers should consider +#' adding their own custom numbering to the chapter titles in the frontmatter. #' @return a character vector of HTML divs that can be appended to display the #' sidebar. #' @keywords internal @@ -151,15 +154,29 @@ create_sidebar_headings <- function(nodes) { #' [set_globals()] for where `create_sidebar()` is called and #' [build_html()] for where `update_sidebar()` is called. #' @rdname create_sidebar -create_sidebar <- function(chapters, name = "", html = "") { +create_sidebar <- function( + chapters, + name = "", + html = "", + disable_numbering = FALSE) { res <- character(length(chapters)) + for (i in seq(chapters)) { position <- if (name == chapters[i]) "current" else i info <- get_navbar_info(chapters[i]) - # We use zero index to count the index page (which is removed later) + + numbering_prefix = paste0(i - 1, ". ") + # if numbering is disabled, remove list numbering prefix + if (disable_numbering) { + numbering_prefix = "" + } + + # We use zero index to count the index page + # (which is removed later if automated numbering is enabled) page_link <- paste0( "", - i - 1, ". ", parse_title(info$pagetitle), + numbering_prefix, + parse_title(info$pagetitle), "" ) res[i] <- create_sidebar_item(html, page_link, position) @@ -253,7 +270,7 @@ update_sidebar <- function( #' #' # Add an anchor to the links #' snd$fix_sidebar_href(my_links, scheme = "https", fragment = "anchor") -#' +#' #' # NOTE: this will _always_ return a character vector, even if the input is #' # incorrect #' snd$fix_sidebar_href(list(), server = "example.com") diff --git a/R/utils-varnish.R b/R/utils-varnish.R index be080f34..eaa2f39f 100644 --- a/R/utils-varnish.R +++ b/R/utils-varnish.R @@ -78,13 +78,31 @@ set_globals <- function(path) { # that is different is the name of the index node. idx <- these_resources[["."]] idx <- idx[as_html(idx) == "index.html"] - instructor_sidebar <- create_sidebar(c(idx, these_resources[["episodes"]])) + + # get sidebar numbering disable option from config, if null set FALSE + disable_numbering <- this_metadata$get()[["disable_sidebar_numbering"]] %||% FALSE + + instructor_sidebar <- create_sidebar( + c(idx, these_resources[["episodes"]]), + disable_numbering = disable_numbering + ) # check if we have a title in the index sidebar and replace with # "summary and schedule" if it does not exist. idx_item <- xml2::read_html(instructor_sidebar[[1]]) idx_link <- xml2::xml_find_first(idx_item, ".//a") idx_text <- xml2::xml_contents(idx_link) - no_index_title <- length(idx_text) == 1 && xml2::xml_text(idx_text) == "0. " + href <- xml2::xml_attr(idx_link, "href") + + no_index_title <- ( + length(idx_text) == 1 && + xml2::xml_text(idx_text) == "0. " + ) || + ( + disable_numbering && + length(idx_text) == 0 && + href == "index.html" + ) + if (no_index_title) { xml2::xml_set_text(idx_link, tr_computed("SummaryAndSchedule")) } else {