Skip to content

Commit

Permalink
Add support for disabling sidebar automatic numbering
Browse files Browse the repository at this point in the history
  • Loading branch information
froggleston committed Nov 22, 2024
1 parent d174084 commit 5aaa2c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
25 changes: 21 additions & 4 deletions R/utils-sidebar.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,39 @@ 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
#' @seealso [create_sidebar_item()] for creation of individual sidebar items,
#' [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 = "<a href='https://carpentries.org'/>") {
create_sidebar <- function(
chapters,
name = "",
html = "<a href='https://carpentries.org'/>",
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(
"<a href='", info$href, "'>",
i - 1, ". ", parse_title(info$pagetitle),
numbering_prefix,
parse_title(info$pagetitle),
"</a>"
)
res[i] <- create_sidebar_item(html, page_link, position)
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 20 additions & 2 deletions R/utils-varnish.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5aaa2c1

Please sign in to comment.