Skip to content

Commit

Permalink
exclude headings containing the class name "unlisted" from TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Oct 2, 2024
1 parent 4257bd8 commit 66159bd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: litedown
Type: Package
Title: A Lightweight Version of R Markdown
Version: 0.2.6
Version: 0.2.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person()
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Added helper functions `pkg_desc()`, `pkg_news()`, `pkg_citation()`, and `pkg_manual()` to get various package information for building the full package documentation as a single-file book (thanks, @jangorecki @llrs #24, @TimTaylor #22).

- Section headings containing the class name "unlisted" will be excluded in the table of contents.

- Added back/forward/refresh/print buttons to the toolbar in the `litedown::roam()` preview interface.

- Set `options(bitmapType = 'cairo')` in `fuse()` if `capabilities('cairo')` is TRUE, which will generate smaller bitmap plot files (e.g., `.png`) than using `quartz` or `Xlib`, and is also a safer option for `fuse()` to be executed in parallel (rstudio/rmarkdown#2561).
Expand Down
11 changes: 7 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ build_toc = function(html, n = 3) {
if (n > 6) n = 6
r = sprintf('<(h[1-%d])( id="[^"]+")?[^>]*>(.+?)</\\1>', n)
items = unlist(match_full(html, r))
# ignore headings with class="unlisted"
items = items[!has_class(items, 'unlisted')]
if (length(items) == 0) return()
x = gsub(r, '<toc\\2>\\3</toc>', items) # use a tag <toc> to protect heading text
h = as.integer(gsub('^h', '', gsub(r, '\\1', items))) # heading level
Expand Down Expand Up @@ -698,6 +700,11 @@ unique_id = function(x, empty) {
x
}

# test if a class name exists in attributes
has_class = function(x, class) {
grepl(sprintf(' class="([^"]+ )?%s( [^"]+)?"', class), x)
}

# number sections in HTML output
number_sections = function(x) {
h = sub('</h([1-6])>', '\\1', unlist(match_full(x, '</h[1-6]>')))
Expand All @@ -706,10 +713,6 @@ number_sections = function(x) {
r = '<h([1-6])([^>]*)>(?!<span class="section-number)'
n = rep(0, 6) # counters for all levels of headings
k0 = 6 # level of last unnumbered heading
# test if a class name exists in attributes
has_class = function(x, class) {
grepl(sprintf(' class="([^"]+ )?%s( [^"]+)?"', class), x)
}
match_replace(x, r, function(z) {
z1 = as.integer(sub(r, '\\1', z, perl = TRUE))
z2 = sub(r, '\\2', z, perl = TRUE)
Expand Down

0 comments on commit 66159bd

Please sign in to comment.