Skip to content

Commit

Permalink
<script> and <style> are correctly ignored from search content (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored May 11, 2022
1 parent 82b9872 commit 91a0103
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: distill
Title: 'R Markdown' Format for Scientific and Technical Writing
Version: 1.3.12
Version: 1.3.13
Authors@R: c(
person("JJ", "Allaire", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0174-9868")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# distill (development version)

- Content in `<script>` and `<style>` are now considered not searchable and exclude from content in `search.json`. This should make the json file a lot lighter and loading faster as htmlwidget code (e.g plotly graph) would correctly be not included (thanks, \\\@mitchelloharawild, #425).
- Fix an issue with `search.json` that was still written even when `search: false` in set in config (thanks, \\\@mitchelloharawild, #425).
- Fix an issue prevent sizing of figures produced with **knitr** using `out.width` chunk option (thanks, \\\@ssp3nc3r, #286).
- Fix an issue with running `targets::tar_render()` with a distill Rmd document (thanks, \\\@tarensanders, #400)
Expand Down
7 changes: 7 additions & 0 deletions R/collections.R
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,13 @@ article_contents <- function(path) {
article_html <- xml2::xml_find_first(html, "//body")
}
if (!is.na(article_html)) {
# remove script tag content for article content
scripts <- xml2::xml_find_all(article_html, "//script")
xml2::xml_remove(scripts)
# remove style tag content for article content
style <- xml2::xml_find_all(article_html, "//style")
xml2::xml_remove(style)

contents <- as_utf8(xml2::xml_text(article_html))
}
contents
Expand Down
44 changes: 42 additions & 2 deletions tests/testthat/helpers.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
skip_if_pandoc_not_installed <- function() {
skip_if_not(rmarkdown::pandoc_available("2.0"), "pandoc >= 2.0 not available")
local_rmd_file <- function(..., .env = parent.frame()) {
path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd")
xfun::write_utf8(c(...), path)
path
}

local_render <- function(input, ..., .env = parent.frame()) {
skip_if_not_pandoc()
output_file <- withr::local_tempfile(.local_envir = .env)
rmarkdown::render(input, output_file = output_file, quiet = TRUE, ...)
}

.render_and_read <- function(input, ...) {
skip_if_not_pandoc()
output_file <- withr::local_tempfile()
res <- rmarkdown::render(input, output_file = output_file, quiet = TRUE, ...)
xfun::read_utf8(res)
}

# Use to test pandoc availability or version lower than
skip_if_not_pandoc <- function(ver = "2.0") {
if (!rmarkdown::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 = "2.0") {
if (rmarkdown::pandoc_available(ver)) {
msg <- if (is.null(ver)) {
"Pandoc is available"
} else {
sprintf("Version of Pandoc is greater than %s.", ver)
}
skip(msg)
}
}

12 changes: 12 additions & 0 deletions tests/testthat/test-collections.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("script and style are not included as `article_content`", {
skip_on_cran()
skip_if_not_pandoc()
rmd <- local_rmd_file(c(
"---", "title: test", "---",
"Some content", "",
"<script> console.log('test')</script>", "",
"<style>h1 { color: red }</style>", ""
))
html <- local_render(rmd, output_format = 'distill::distill_article')
expect_identical(str_trim(article_contents(html)), "Some content")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-create.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test_that("blogs can be created", {

skip_if_pandoc_not_installed()
skip_if_not_pandoc()

tmpdir <- withr::local_tempdir()

Expand All @@ -18,7 +18,7 @@ test_that("blogs can be created", {
})

test_that("websites can be created", {
skip_if_pandoc_not_installed()
skip_if_not_pandoc()
expect_error({
on.exit(unlink("testsite", recursive = TRUE), add = TRUE)
create_website("testsite", "Test Site", edit = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-distill_article.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("distill articles can be created", {
skip_if_pandoc_not_installed()
skip_if_not_pandoc()
expect_s3_class(distill_article(), "rmarkdown_output_format")
})

0 comments on commit 91a0103

Please sign in to comment.