From 61e9f05e03616b123932fc9060e72f2c5da31fc8 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Thu, 14 Jan 2021 22:47:46 +0900 Subject: [PATCH 1/2] add tocify-tabset.lua --- R/html_document.R | 7 ++++- inst/rmarkdown/lua/tocify-tabset.lua | 38 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 inst/rmarkdown/lua/tocify-tabset.lua diff --git a/R/html_document.R b/R/html_document.R index bf0488e9ce..ce3b80e10b 100644 --- a/R/html_document.R +++ b/R/html_document.R @@ -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) @@ -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)) @@ -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, diff --git a/inst/rmarkdown/lua/tocify-tabset.lua b/inst/rmarkdown/lua/tocify-tabset.lua new file mode 100644 index 0000000000..e82a1df80c --- /dev/null +++ b/inst/rmarkdown/lua/tocify-tabset.lua @@ -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", + "" .. + stringify(block) .. + "" + ) + } + end + end +end From db89d89b96bc677b34c3860c40e6805943f049d7 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Thu, 14 Jan 2021 22:48:16 +0900 Subject: [PATCH 2/2] add tabset.tocify.js --- R/html_dependencies.R | 2 +- inst/rmd/h/tocify/tabset.tocify.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 inst/rmd/h/tocify/tabset.tocify.js diff --git a/R/html_dependencies.R b/R/html_dependencies.R index 23892ca7f6..0cd7ce61d6 100644 --- a/R/html_dependencies.R +++ b/R/html_dependencies.R @@ -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") } diff --git a/inst/rmd/h/tocify/tabset.tocify.js b/inst/rmd/h/tocify/tabset.tocify.js new file mode 100644 index 0000000000..5f6b4609d3 --- /dev/null +++ b/inst/rmd/h/tocify/tabset.tocify.js @@ -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(); + } + }); +});