Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tabsets in ToC (closes #1336) #2007

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/html_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ html_dependency_tocify <- function() {
name = "tocify",
version = "1.9.1",
src = pkg_file("rmd/h/tocify"),
script = "jquery.tocify.js",
script = c("jquery.tocify.js", "tabset.tocify.js"),
stylesheet = "jquery.tocify.css")
}

Expand Down
7 changes: 6 additions & 1 deletion R/html_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ html_document <- function(toc = FALSE,

# build pandoc args
args <- c("--standalone")
lua_filters <- character(0L)

# use section divs
if (section_divs)
Expand Down Expand Up @@ -267,6 +268,9 @@ html_document <- function(toc = FALSE,
# flag for template
args <- c(args, pandoc_variable_arg("toc_float", "1"))

# Lua filter
lua_filters <- c(lua_filters, pkg_file_lua("tocify-tabset.lua"))

# selectors
selectors <- paste0("h", seq(1, toc_depth), collapse = ",")
args <- c(args, pandoc_variable_arg("toc_selectors", selectors))
Expand Down Expand Up @@ -465,7 +469,8 @@ html_document <- function(toc = FALSE,
knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md, dev),
pandoc = pandoc_options(to = "html",
from = from_rmarkdown(fig_caption, md_extensions),
args = args),
args = args,
lua_filters = lua_filters),
keep_md = keep_md,
clean_supporting = self_contained,
df_print = df_print,
Expand Down
38 changes: 38 additions & 0 deletions inst/rmarkdown/lua/tocify-tabset.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local tabset_level = 100
local tab_level = 101
local flag = false
local stringify = pandoc.utils.stringify

function is_tabset(classes)

local res = false

for _,v in ipairs(classes) do
res = res or (v == "tabset")
end

return res
end

function Block(block)
if block.tag == "Header" then
local level = block.level
if level <= tabset_level then
flag = is_tabset(block.classes)
tabset_level = flag and level or 100
tab_level = tabset_level + 1
elseif level == tab_level then
local id = block.identifier
block.identifier = id .. "-tab"
return {
block,
pandoc.RawBlock(
"html",
"<h" .. level .. " style='visibility: hidden'>" ..
stringify(block) ..
"</h" .. level .. ">"
)
}
end
end
end
14 changes: 14 additions & 0 deletions inst/rmd/h/tocify/tabset.tocify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
document.addEventListener('DOMContentLoaded', function() {
const anchors = Array.from(document.querySelectorAll("ul.nav.nav-tabs li a")).
filter(a => a.attributes.role.value === "tab").
reduce((hash, a) => {
hash["#" + a.innerText.replace(/ /, "_")] = a;
return hash;
}, {});
window.addEventListener('hashchange', function() {
const anchor = anchors[location.hash];
if (anchor !== undefined) {
anchor.click();
}
});
});