Skip to content

Commit

Permalink
Escape article titles (r-lib#2471)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and SebKrantz committed Jun 1, 2024
1 parent 2b386e1 commit 95f10b2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* `build_article()` now escapes html characters in the title (#2286).
* `build_article()` no longer generates the wrong source link when you build your site outside of the root directory (#2172).
* `build_reference()` matches usage for S3 and S4 methods to the style used by R 4.0.0 and later (#2187).
* `<source>` tags now have their `srcref` attributes tweaked in the same way that the `src` attributes of `<img>` tags are (#2402).
Expand Down
2 changes: 1 addition & 1 deletion R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ build_article <- function(name,
as_is <- isTRUE(purrr::pluck(front, "pkgdown", "as_is"))

default_data <- list(
pagetitle = front$title,
pagetitle = escape_html(front$title),
toc = toc <- front$toc %||% TRUE,
opengraph = list(description = front$description %||% pkg$package),
source = repo_source(pkg, input),
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/assets/articles/vignettes/needs-escape.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: "a <-> b"
---

2 changes: 1 addition & 1 deletion tests/testthat/assets/reference/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Version: 1.0.0
Title: A test package
Description: A test package
Authors@R: person("Hadley Wickham")
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
5 changes: 5 additions & 0 deletions tests/testthat/assets/reference/R/funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ NULL
#' @examples
#' testpackage:::f()
f <- function() {runif(5L)}


#' g <-> h
#' @keywords internal
g <- function() 1
12 changes: 12 additions & 0 deletions tests/testthat/assets/reference/man/g.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ test_that("check doesn't include getting started vignette", {
expect_error(data_articles_index(pkg), NA)
})

test_that("titles are escaped when needed", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(build_article(pkg = pkg, name = "needs-escape"))

html <- xml2::read_html(file.path(pkg$dst_path, "articles/needs-escape.html"))
expect_equal(xpath_text(html, "//title", trim = TRUE), "a <-> b • testpackage")
expect_equal(xpath_text(html, "//h1", trim = TRUE), "a <-> b")
})


test_that("output is reproducible by default, i.e. 'seed' is respected", {
pkg <- local_pkgdown_site(test_path("assets/articles"))
suppressMessages(build_article(pkg = pkg, name = "random"))
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-build-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ test_that("examples are reproducible by default, i.e. 'seed' is respected", {
expect_snapshot(cat(examples))
})

test_that("title and page title escapes html", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
suppressMessages(build_reference(pkg, topics = "g"))

html <- xml2::read_html(file.path(pkg$dst_path, "reference", "g.html"))
expect_equal(xpath_text(html, "//title", trim = TRUE), "g <-> h — g • testpackage")
expect_equal(xpath_text(html, "//h1", trim = TRUE), "g <-> h")
})

test_that("get_rdname handles edge cases", {
expect_equal(get_rdname(list(file_in = "foo..Rd")), "foo.")
expect_equal(get_rdname(list(file_in = "foo.rd")), "foo")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("fails if article index incomplete", {
pkg <- local_pkgdown_site(test_path("assets/articles"), meta = "
articles:
- title: Title
contents: [starts_with('html'), random, standard, toc-false, widget]
contents: [starts_with('html'), random, standard, toc-false, widget, needs-escape]
")
expect_snapshot(check_pkgdown(pkg), error = TRUE)
})
Expand Down

0 comments on commit 95f10b2

Please sign in to comment.