Skip to content

Commit

Permalink
Merge branch 'main' into issue-2462
Browse files Browse the repository at this point in the history
  • Loading branch information
jayhesselberth authored Apr 18, 2024
2 parents c9fc4b4 + 9d22b8c commit 2ed19af
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 29 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# pkgdown (development version)

* Clipboard buttons report their action again ("Copied!") (#2468)
=======
* `build_readme()` now correctly tweaks links to markdown files that use an anchor, e.g. `foo.md#heading-name` (#2313).
* `build_home()` no longer errors when you have an empty `.md` file (#2309).
* `data_template()` gives a more informative error if you've misspecified the navbar (#2312).
* The skip link now becomes visible when focussed (#2138). Thanks to @glin for the styles!
* `build_reference_index()` gives more informative errors if your `contents` field is malformed (#2323).
* The left and right footers no longer contain an extra empty paragraph tag and the footer gains additional padding-top to keep the whitespace constant (#2381).
Expand Down
9 changes: 8 additions & 1 deletion R/check.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#' Check `_pkgdown.yml`
#'
#' @description
#' Check that your `_pkgdown.yml` is valid without building the whole
#' site.
#' site. Currently this:
#'
#' * Checks the reference and article indexes to ensure that pkgdown can
#' read them, and that every documentation topic and vignette/article is
#' included in the index.
#'
#' * Validates any opengraph metadata that you might have supplied
#'
#' @export
#' @inheritParams as_pkgdown
Expand Down
4 changes: 4 additions & 0 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ markdown_text_block <- function(text, ...) {
markdown_body <- function(path, strip_header = FALSE) {
xml <- markdown_path_html(path, strip_header = strip_header)

if (is.null(xml)) {
return(NULL)
}

# Extract body of html - as.character renders as xml which adds
# significant whitespace in tags like pre
transformed_path <- withr::local_tempfile()
Expand Down
16 changes: 11 additions & 5 deletions R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ navbar_links <- function(pkg, depth = 0L) {
yaml_character(pkg, c("navbar", "structure", "left")),
names(components)
)

# Backward compatibility
left <- navbar$left %||% components[left_comp]
right <- navbar$right %||% components[right_comp]

list(
left = render_navbar_links(left, depth = depth, bs_version = pkg$bs_version),
right = render_navbar_links(right, depth = depth, bs_version = pkg$bs_version)
left = render_navbar_links(left, depth = depth, pkg = pkg),
right = render_navbar_links(right, depth = depth, pkg = pkg)
)
}

render_navbar_links <- function(x, depth = 0L, bs_version = 3) {
render_navbar_links <- function(x, depth = 0L, pkg) {
if (!is.list(x)) {
cli::cli_abort(
"Invalid navbar specification in {pkgdown_config_href({pkg$src_path})}",
call = quote(data_template())
)
}

stopifnot(is.integer(depth), depth >= 0L)

tweak <- function(x) {
Expand All @@ -83,7 +89,7 @@ render_navbar_links <- function(x, depth = 0L, bs_version = 3) {
x <- lapply(x, tweak)
}

if (bs_version == 3) {
if (pkg$bs_version == 3) {
rmarkdown::navbar_links_html(x)
} else {
bs4_navbar_links_html(x)
Expand Down
6 changes: 4 additions & 2 deletions R/tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ tweak_link_md <- function(html) {
return()

hrefs <- xml2::xml_attr(links, "href")
needs_tweak <- grepl("\\.md$", hrefs) & xml2::url_parse(hrefs)$scheme == ""

urls <- xml2::url_parse(hrefs)
needs_tweak <- urls$scheme == "" & grepl("\\.md$", urls$path)

fix_links <- function(x) {
x <- gsub("\\.md$", ".html", x)
x <- gsub("\\.md\\b", ".html", x)
x <- gsub("\\.github/", "", x)
x
}
Expand Down
8 changes: 7 additions & 1 deletion man/check_pkgdown.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Code
build_articles(pkg, lazy = FALSE)
Message
-- Building articles -----------------------------------------------------------
Writing `articles/index.html`
Reading vignettes/kitten.Rmd
Writing `articles/kitten.html`
Expand All @@ -20,6 +21,7 @@
Code
build_articles(pkg)
Message
-- Building articles -----------------------------------------------------------
Writing `articles/index.html`
Reading vignettes/html-vignette.Rmd
Writing `articles/html-vignette.html`
Expand Down Expand Up @@ -112,6 +114,8 @@

Code
build_redirects(pkg)
Message
-- Building redirects ----------------------------------------------------------

# pkgdown deps are included only once in articles

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/_snaps/build-favicons.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Code
expect_output(build_favicons(pkg), "Building favicons")
Message
-- Building favicons -----------------------------------------------------------
Condition
Error in `build_favicons()`:
! Can't find package logo PNG or SVG to build favicons.
Expand All @@ -12,6 +14,7 @@
Code
build_favicons(pkg)
Message
-- Building favicons -----------------------------------------------------------
Favicons already exist in 'pkgdown'
i Set `overwrite = TRUE` to re-create.

1 change: 1 addition & 0 deletions tests/testthat/_snaps/build-github.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code
build_github_pages(pkg)
Message
-- Extra files for GitHub pages ------------------------------------------------
Writing `.nojekyll`
Writing `CNAME`

1 change: 1 addition & 0 deletions tests/testthat/_snaps/build-home-citation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Writing `404.html`

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/_snaps/build-home.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Writing `404.html`

Expand All @@ -11,6 +12,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Writing `404.html`

Expand All @@ -19,6 +21,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Condition
Warning:
Expand All @@ -32,6 +35,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Writing `404.html`

Expand All @@ -40,6 +44,7 @@
Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Reading .github/404.md
Writing `404.html`
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/build-news.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Code
build_news(pkg)
Message
-- Building news ---------------------------------------------------------------
Writing `news/news-2.0.html`
Writing `news/news-1.1.html`
Writing `news/news-1.0.html`
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/build-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code
build_reference(pkg)
Message
-- Building function reference -------------------------------------------------
Writing `reference/index.html`
Reading man/f.Rd
Condition
Expand All @@ -17,6 +18,7 @@
Code
build_reference(pkg, topics = "e")
Message
-- Building function reference -------------------------------------------------
Writing `reference/index.html`
Reading man/e.Rd
Writing `reference/e.html`
Expand All @@ -26,6 +28,7 @@
Code
build_reference(pkg, topics = "e")
Message
-- Building function reference -------------------------------------------------
Writing `reference/index.html`
Reading man/e.Rd
Writing `reference/e.html`
Expand All @@ -35,6 +38,7 @@
Code
build_reference(pkg, topics = "a")
Message
-- Building function reference -------------------------------------------------
Writing `reference/index.html`
Reading man/a.Rd
Writing `reference/a.html`
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/figure.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Code
build_reference(pkg, devel = FALSE)
Message
-- Building function reference -------------------------------------------------
Writing `reference/index.html`
Reading man/figure.Rd
Writing `reference/figure.html`
Expand All @@ -12,6 +13,7 @@
Code
build_articles(pkg)
Message
-- Building articles -----------------------------------------------------------
Writing `articles/index.html`
Reading vignettes/figures.Rmd
Writing `articles/figures.html`
Expand Down
14 changes: 11 additions & 3 deletions tests/testthat/_snaps/navbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,18 @@
Error in `navbar_links()`:
! navbar.structure.left must be a character vector.

# data_navbar() errors with bad left/right

Code
data_navbar(pkg)
Condition
Error in `data_template()`:
! Invalid navbar specification in _pkgdown.yml

# render_navbar_links BS3 & BS4 default

Code
cat(render_navbar_links(x, bs_version = 3))
cat(render_navbar_links(x, pkg = list(bs_version = 3)))
Output
<li>
<a href="articles/pkgdown.html">Get started</a>
Expand Down Expand Up @@ -220,7 +228,7 @@
---

Code
cat(render_navbar_links(x, bs_version = 4))
cat(render_navbar_links(x, pkg = list(bs_version = 4)))
Output
<li class="nav-item">
<a class="nav-link" href="articles/pkgdown.html">Get started</a>
Expand All @@ -246,7 +254,7 @@
# render_navbar_links BS4 no divider before first element

Code
cat(render_navbar_links(x, bs_version = 4))
cat(render_navbar_links(x, pkg = list(bs_version = 4)))
Output
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</a>
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/setup.R

This file was deleted.

4 changes: 4 additions & 0 deletions tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ test_that("handles empty inputs", {

expect_equal(markdown_text_block(NULL), NULL)
expect_equal(markdown_text_block(""), NULL)

path <- withr::local_tempfile()
file.create(path)
expect_equal(markdown_body(path), NULL)
})

test_that("header attributes are parsed", {
Expand Down
17 changes: 14 additions & 3 deletions tests/testthat/test-navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ test_that("data_navbar() errors with bad side specifications", {
expect_snapshot(data_navbar(pkg), error = TRUE)
})

test_that("data_navbar() errors with bad left/right", {
pkg <- local_pkgdown_site(meta = "
navbar:
right: [github]
")

expect_snapshot(data_navbar(pkg), error = TRUE)
})


test_that("for bs4, default bg and type come from bootswatch", {
style <- navbar_style(bs_version = 5)
expect_equal(style, list(bg = "light", type = "light"))
Expand Down Expand Up @@ -164,8 +174,9 @@ test_that("render_navbar_links BS3 & BS4 default", {
),
news = list(text = "News", href = "news/index.html")
)
expect_snapshot(cat(render_navbar_links(x, bs_version = 3)))
expect_snapshot(cat(render_navbar_links(x, bs_version = 4)))

expect_snapshot(cat(render_navbar_links(x, pkg = list(bs_version = 3))))
expect_snapshot(cat(render_navbar_links(x, pkg = list(bs_version = 4))))
})

test_that("render_navbar_links BS4 no divider before first element", {
Expand All @@ -183,7 +194,7 @@ test_that("render_navbar_links BS4 no divider before first element", {
)
)
)
expect_snapshot(cat(render_navbar_links(x, bs_version = 4)))
expect_snapshot(cat(render_navbar_links(x, pkg = list(bs_version = 4))))
})

test_that("can specific link target", {
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-tweak-tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,18 @@ test_that("docs with no headings are left unchanged", {
test_that("local md links are replaced with html", {
html <- xml2::read_html('
<a href="local.md"></a>
<a href="local.md#fragment"></a>
<a href="http://remote.com/remote.md"></a>
')
tweak_link_md(html)

expect_equal(
xpath_attr(html, "//a", "href"),
c("local.html", "http://remote.com/remote.md")
c("local.html", "local.html#fragment", "http://remote.com/remote.md")
)
})


test_that("tweak_link_external() add the external-link class if needed", {
html <- xml2::read_html('
<a href="#anchor"></a>
Expand Down
Loading

0 comments on commit 2ed19af

Please sign in to comment.