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 missing variable in templates regarding toc #127

Merged
merged 8 commits into from
Sep 23, 2021
Merged
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
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ Depends:
Imports:
rmarkdown (>= 1.7)
Suggests:
xfun (>= 0.21),
xml2,
bslib,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
withr (>= 2.4.2)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
Expand Down
5 changes: 5 additions & 0 deletions R/revealjs_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
revealjs_presentation <- function(incremental = FALSE,
center = FALSE,
slide_level = 2,
toc = FALSE,
toc_depth = 3,
fig_width = 8,
fig_height = 6,
fig_retina = if (!fig_caption) 2,
Expand All @@ -88,6 +90,9 @@ revealjs_presentation <- function(incremental = FALSE,

# base pandoc options for all reveal.js output
args <- c()

# table of contents
args <- c(args, pandoc_toc_args(toc, toc_depth))

# template path and assets
if (identical(template, "default")) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ process_reveal_option <- function(option, value) {
}
}
pandoc_variable_arg(option, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,12 @@ <h3 class="date">$date$</h3>
$endif$
$if(toc)$
<section id="$idprefix$TOC">
<nav role="doc-toc">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</nav>
</section>
$endif$

Expand Down
7 changes: 7 additions & 0 deletions man/revealjs_presentation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local_temp_rmd_file <- function(..., .env = parent.frame()) {
path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd")
xfun::write_utf8(c(...), path)
path
}

local_temp_draft <- function(.env = parent.frame()) {
path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd")
# TODO: Use `rmarkdown::draft()` when rmarkdown 2.12 is out.
pkg_file <- getFromNamespace("pkg_file", "rmarkdown")
template_path <- pkg_file("rmarkdown", "templates", "revealjs_presentation",
package = "revealjs")
rmarkdown::draft(path, template_path, edit = FALSE)
}

.render_and_read <- function(input, xml = TRUE, ...) {
skip_if_not_pandoc()
output_file <- withr::local_tempfile(fileext = ".html")
res <- rmarkdown::render(input, output_file = output_file, quiet = TRUE, ...)
if (xml) {
xml2::read_html(res)
} else {
xfun::read_utf8(res)
}
}

# Use to test pandoc availability or version lower than
skip_if_not_pandoc <- function(ver = NULL) {
if (!pandoc_available(ver)) {
msg <- if (is.null(ver)) {
"Pandoc is not available"
} else {
sprintf("Version of Pandoc is lower than %s.", ver)
}
skip(msg)
}
}

# Use to test version greater than
skip_if_pandoc <- function(ver = NULL) {
if (pandoc_available(ver)) {
msg <- if (is.null(ver)) {
"Pandoc is available"
} else {
sprintf("Version of Pandoc is greater than %s.", ver)
}
skip(msg)
}
}
20 changes: 20 additions & 0 deletions tests/testthat/test-revealjs_presentation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("toc argument works", {
skip_if_not_pandoc()
skip_if_not_installed("xml2")
rmd <- local_temp_draft()
html <- .render_and_read(
rmd,
output_options = list(
toc = TRUE,
pandoc_args = c(pandoc_variable_arg("toc-title", "TOC"))
)
)
toc <- xml2::xml_find_all(html, "//section[@id='TOC']")
expect_length(toc, 1)
expect_equal(
xml2::xml_text(
xml2::xml_find_all(toc, "./nav/*[contains(@id, 'toc-title')]")
),
"TOC"
)
})