Skip to content

Commit

Permalink
Align markdown_text_inline() error arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 20, 2024
1 parent 37ea00d commit f34e1e1
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 27 deletions.
10 changes: 9 additions & 1 deletion R/build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ data_articles_index_section <- function(section, index, pkg, call = caller_env()
error_pkg = pkg,
error_call = call
)
title <- markdown_text_inline(
section$title,
error_path = paste0("articles[", index, "].title"),
error_pkg = pkg,
error_call = call
)

config_check_string(
section$desc,
error_path = paste0("articles[", index, "].desc"),
Expand All @@ -456,8 +463,9 @@ data_articles_index_section <- function(section, index, pkg, call = caller_env()
description = lapply(section_vignettes$description, markdown_text_block),
)


list(
title = markdown_text_inline(section$title),
title = title,
desc = markdown_text_block(section$desc),
class = section$class,
contents = purrr::transpose(contents)
Expand Down
4 changes: 2 additions & 2 deletions R/build-home-authors.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ author_name <- function(x, authors, pkg) {
if (!is.null(author$html)) {
name <- markdown_text_inline(
author$html,
paste0("authors.", name, ".html"),
pkg = pkg
error_path = paste0("authors.", name, ".html"),
error_pkg = pkg
)
}

Expand Down
19 changes: 14 additions & 5 deletions R/build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data_reference_index <- function(pkg = ".", error_call = caller_env()) {

unwrap_purrr_error(
rows <- meta %>%
purrr::imap(data_reference_index_rows, pkg = pkg) %>%
purrr::imap(data_reference_index_rows, pkg = pkg, call = error_call) %>%
purrr::compact() %>%
unlist(recursive = FALSE)
)
Expand Down Expand Up @@ -82,13 +82,18 @@ check_contents <- function(contents, index, pkg, prefix, call = caller_env()) {
}


data_reference_index_rows <- function(section, index, pkg) {
data_reference_index_rows <- function(section, index, pkg, call = caller_env()) {
is_internal <- identical(section$title, "internal")

rows <- list()
if (has_name(section, "title")) {
rows[[1]] <- list(
title = markdown_text_inline(section$title, pkg = pkg),
title = markdown_text_inline(
section$title,
error_path = paste0("reference[", index, "].title"),
error_call = call,
error_pkg = pkg
),
slug = make_slug(section$title),
desc = markdown_text_block(section$desc),
is_internal = is_internal
Expand All @@ -97,15 +102,19 @@ data_reference_index_rows <- function(section, index, pkg) {

if (has_name(section, "subtitle")) {
rows[[2]] <- list(
subtitle = markdown_text_inline(section$subtitle, pkg = pkg),
subtitle = markdown_text_inline(
section$subtitle,
error_path = paste0("reference[", index, "].subtitle"),
error_call = call,
error_pkg = pkg
),
slug = make_slug(section$subtitle),
desc = markdown_text_block(section$desc),
is_internal = is_internal
)
}

if (has_name(section, "contents")) {
id <- section$title %||% section$subtitle %||% index
topics <- section_topics(section$contents, pkg$topics, pkg$src_path)

names <- topics$name
Expand Down
7 changes: 6 additions & 1 deletion R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ config_pluck_markdown_inline <- function(pkg,
call = caller_env()) {

text <- config_pluck_string(pkg, path, default, call = call)
markdown_text_inline(text, where = path, pkg = pkg, call = call)
markdown_text_inline(
text,
error_path = path,
error_pkg = pkg,
error_call = call
)
}

config_pluck_markdown_block <- function(pkg,
Expand Down
12 changes: 6 additions & 6 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ markdown_text <- function(text, ...) {
}

markdown_text_inline <- function(text,
where = "<inline>",
pkg,
call = caller_env(),
error_path,
error_pkg,
error_call = caller_env(),
...) {
html <- markdown_text(text, ...)
if (is.null(html)) {
Expand All @@ -21,9 +21,9 @@ markdown_text_inline <- function(text,
children <- xml2::xml_children(xml2::xml_find_first(html, ".//body"))
if (length(children) > 1) {
config_abort(
pkg,
"{.field {where}} must supply an inline element, not a block element.",
call = call
error_pkg,
"{.field {error_path}} must be inline markdown.",
call = error_call
)
}

Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
Error in `data_articles_index_()`:
! articles[1].title must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
Code
data_articles_index_(list(list(title = "a\n\nb")))
Condition
Error in `data_articles_index_()`:
! articles[1].title must be inline markdown.
i Edit _pkgdown.yml to fix the problem.
Code
data_articles_index_(list(list(title = "a", contents = 1)))
Condition
Expand Down Expand Up @@ -91,11 +97,3 @@
Output
## [1] 0.080750138 0.834333037 0.600760886 0.157208442 0.007399441

# reports on bad open graph meta-data

Code
build_article(pkg = pkg, name = "bad-opengraph")
Condition
Error in `build_article()`:
! Can't find article 'bad-opengraph'

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/build-home-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
data_home_sidebar_authors_(authors = list(sidebar = list(before = "x\n\ny")))
Condition
Error in `data_home_sidebar_authors_()`:
! authors.sidebar.before must supply an inline element, not a block element.
! authors.sidebar.before must be inline markdown.
i Edit _pkgdown.yml to fix the problem.

# sidebar can accept additional before and after text
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/build-reference-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,24 @@
Error in `data_reference_index_()`:
! reference[1].title must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
Code
data_reference_index_(list(list(title = "a\n\nb")))
Condition
Error in `data_reference_index_()`:
! reference[1].title must be inline markdown.
i Edit _pkgdown.yml to fix the problem.
Code
data_reference_index_(list(list(subtitle = 1)))
Condition
Error in `data_reference_index_()`:
! reference[1].subtitle must be a string, not the number 1.
i Edit _pkgdown.yml to fix the problem.
Code
data_reference_index_(list(list(subtitle = "a\n\nb")))
Condition
Error in `data_reference_index_()`:
! reference[1].subtitle must be inline markdown.
i Edit _pkgdown.yml to fix the problem.
Code
data_reference_index_(list(list(title = "bla", contents = 1)))
Condition
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/markdown.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# markdown_text_inline() works with inline markdown

Code
markdown_text_inline("x\n\ny", pkg = pkg)
markdown_text_inline("x\n\ny", error_pkg = pkg, error_path = "title")
Condition
Error:
! <inline> must supply an inline element, not a block element.
! title must be inline markdown.
i Edit _pkgdown.yml to fix the problem.

1 change: 1 addition & 0 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ test_that("validates articles yaml", {
data_articles_index_(1)
data_articles_index_(list(1))
data_articles_index_(list(list(title = 1)))
data_articles_index_(list(list(title = "a\n\nb")))
data_articles_index_(list(list(title = "a", contents = 1)))
})
})
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-build-reference-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ test_that("gives informative errors", {
data_reference_index_(1)
data_reference_index_(list(1))
data_reference_index_(list(list(title = 1)))
data_reference_index_(list(list(title = "a\n\nb")))
data_reference_index_(list(list(subtitle = 1)))
data_reference_index_(list(list(subtitle = "a\n\nb")))
data_reference_index_(list(list(title = "bla", contents = 1)))
data_reference_index_(list(list(title = "bla", contents = NULL)) )
data_reference_index_(list(list(title = "bla", contents = list("a", NULL))))
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ test_that("markdown_text_inline() works with inline markdown", {
expect_equal(markdown_text_inline("**lala**"), "<strong>lala</strong>")

pkg <- local_pkgdown_site()
expect_snapshot(markdown_text_inline("x\n\ny", pkg = pkg), error = TRUE)
expect_snapshot(error = TRUE, {
markdown_text_inline("x\n\ny", error_pkg = pkg, error_path = "title")
})
})

test_that("markdown_text_block() works with inline and block markdown", {
Expand Down

0 comments on commit f34e1e1

Please sign in to comment.