Skip to content

Commit 5aaa2c1

Browse files
committed
Add support for disabling sidebar automatic numbering
1 parent d174084 commit 5aaa2c1

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

R/utils-sidebar.R

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,39 @@ create_sidebar_headings <- function(nodes) {
144144
#' @param name the name of the current chapter
145145
#' @param html the html of the current chapter. defaults to a link that will
146146
#' produce a sidebar with no links to headings.
147+
#' @param disable_numbering a boolean indicating if the sidebar should not automatically
148+
#' number the chapters. Defaults to `FALSE`. If `TRUE`, developers should consider
149+
#' adding their own custom numbering to the chapter titles in the frontmatter.
147150
#' @return a character vector of HTML divs that can be appended to display the
148151
#' sidebar.
149152
#' @keywords internal
150153
#' @seealso [create_sidebar_item()] for creation of individual sidebar items,
151154
#' [set_globals()] for where `create_sidebar()` is called and
152155
#' [build_html()] for where `update_sidebar()` is called.
153156
#' @rdname create_sidebar
154-
create_sidebar <- function(chapters, name = "", html = "<a href='https://carpentries.org'/>") {
157+
create_sidebar <- function(
158+
chapters,
159+
name = "",
160+
html = "<a href='https://carpentries.org'/>",
161+
disable_numbering = FALSE) {
155162
res <- character(length(chapters))
163+
156164
for (i in seq(chapters)) {
157165
position <- if (name == chapters[i]) "current" else i
158166
info <- get_navbar_info(chapters[i])
159-
# We use zero index to count the index page (which is removed later)
167+
168+
numbering_prefix = paste0(i - 1, ". ")
169+
# if numbering is disabled, remove list numbering prefix
170+
if (disable_numbering) {
171+
numbering_prefix = ""
172+
}
173+
174+
# We use zero index to count the index page
175+
# (which is removed later if automated numbering is enabled)
160176
page_link <- paste0(
161177
"<a href='", info$href, "'>",
162-
i - 1, ". ", parse_title(info$pagetitle),
178+
numbering_prefix,
179+
parse_title(info$pagetitle),
163180
"</a>"
164181
)
165182
res[i] <- create_sidebar_item(html, page_link, position)
@@ -253,7 +270,7 @@ update_sidebar <- function(
253270
#'
254271
#' # Add an anchor to the links
255272
#' snd$fix_sidebar_href(my_links, scheme = "https", fragment = "anchor")
256-
#'
273+
#'
257274
#' # NOTE: this will _always_ return a character vector, even if the input is
258275
#' # incorrect
259276
#' snd$fix_sidebar_href(list(), server = "example.com")

R/utils-varnish.R

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,31 @@ set_globals <- function(path) {
7878
# that is different is the name of the index node.
7979
idx <- these_resources[["."]]
8080
idx <- idx[as_html(idx) == "index.html"]
81-
instructor_sidebar <- create_sidebar(c(idx, these_resources[["episodes"]]))
81+
82+
# get sidebar numbering disable option from config, if null set FALSE
83+
disable_numbering <- this_metadata$get()[["disable_sidebar_numbering"]] %||% FALSE
84+
85+
instructor_sidebar <- create_sidebar(
86+
c(idx, these_resources[["episodes"]]),
87+
disable_numbering = disable_numbering
88+
)
8289
# check if we have a title in the index sidebar and replace with
8390
# "summary and schedule" if it does not exist.
8491
idx_item <- xml2::read_html(instructor_sidebar[[1]])
8592
idx_link <- xml2::xml_find_first(idx_item, ".//a")
8693
idx_text <- xml2::xml_contents(idx_link)
87-
no_index_title <- length(idx_text) == 1 && xml2::xml_text(idx_text) == "0. "
94+
href <- xml2::xml_attr(idx_link, "href")
95+
96+
no_index_title <- (
97+
length(idx_text) == 1 &&
98+
xml2::xml_text(idx_text) == "0. "
99+
) ||
100+
(
101+
disable_numbering &&
102+
length(idx_text) == 0 &&
103+
href == "index.html"
104+
)
105+
88106
if (no_index_title) {
89107
xml2::xml_set_text(idx_link, tr_computed("SummaryAndSchedule"))
90108
} else {

0 commit comments

Comments
 (0)