Skip to content

Commit

Permalink
Use absolute URL for search index (#1774)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Sep 21, 2021
1 parent 85d8beb commit 9309673
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
8 changes: 8 additions & 0 deletions R/build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ build_search_index <- function(pkg) {

purrr::compact(index)

# Make URLs absolute if possible
url <- pkg$meta$url %||% ""
fix_path <- function(x) {
x$path <- sprintf("%s%s", url, x$path)
x
}
index <- purrr::map(index, fix_path)

}

news_search_index <- function(path, pkg) {
Expand Down
65 changes: 65 additions & 0 deletions tests/testthat/_snaps/build-search-docs/search-no-url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"path": ["/dev/authors.html"],
"id": [null],
"dir": [""],
"previous_headings": [""],
"what": ["Authors"],
"title": ["Authors"],
"text": ["Hadley Wickham. Author, maintainer. . Copyright holder, funder."],
"code": [""]
},
{
"path": ["/dev/index.html"],
"id": ["testpackage"],
"dir": [""],
"previous_headings": [""],
"what": ["A test package"],
"title": ["A test package"],
"text": ["nice package"],
"code": [""]
},
{
"path": ["/dev/index.html"],
"id": ["thing"],
"dir": [""],
"previous_headings": [""],
"what": ["Thing"],
"title": ["A test package"],
"text": ["cool!"],
"code": ["plot(1:10)"]
},
{
"path": ["/dev/index.html"],
"id": ["this-section-is-unnumbered"],
"dir": [""],
"previous_headings": [""],
"what": ["This section is unnumbered"],
"title": ["A test package"],
"text": ["expect bug now."],
"code": [""]
},
{
"path": ["/dev/news/index.html"],
"id": ["testpackage-1009000"],
"dir": ["Changelog"],
"previous_headings": [""],
"what": ["testpackage 1.0.0.9000"],
"title": ["testpackage 1.0.0.9000"],
"text": ["bullet (#222 @someone)"],
"code": [""]
},
{
"path": []
},
{
"path": ["/dev/news/index.html"],
"id": ["sub-heading"],
"dir": ["Changelog"],
"previous_headings": [""],
"what": ["sub-heading"],
"title": ["testpackage 1.0.0"],
"text": ["first thing (#111 @githubuser) second thing"],
"code": [""]
}
]
15 changes: 9 additions & 6 deletions tests/testthat/_snaps/build-search-docs/search.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": ["/authors.html"],
"path": ["https://example.com/dev/authors.html"],
"id": [null],
"dir": [""],
"previous_headings": [""],
Expand All @@ -10,7 +10,7 @@
"code": [""]
},
{
"path": ["/index.html"],
"path": ["https://example.com/dev/index.html"],
"id": ["testpackage"],
"dir": [""],
"previous_headings": [""],
Expand All @@ -20,7 +20,7 @@
"code": [""]
},
{
"path": ["/index.html"],
"path": ["https://example.com/dev/index.html"],
"id": ["thing"],
"dir": [""],
"previous_headings": [""],
Expand All @@ -30,7 +30,7 @@
"code": ["plot(1:10)"]
},
{
"path": ["/index.html"],
"path": ["https://example.com/dev/index.html"],
"id": ["this-section-is-unnumbered"],
"dir": [""],
"previous_headings": [""],
Expand All @@ -40,7 +40,7 @@
"code": [""]
},
{
"path": ["/news/index.html"],
"path": ["https://example.com/dev/news/index.html"],
"id": ["testpackage-1009000"],
"dir": ["Changelog"],
"previous_headings": [""],
Expand All @@ -50,7 +50,10 @@
"code": [""]
},
{
"path": ["/news/index.html"],
"path": []
},
{
"path": ["https://example.com/dev/news/index.html"],
"id": ["sub-heading"],
"dir": ["Changelog"],
"previous_headings": [""],
Expand Down
32 changes: 30 additions & 2 deletions tests/testthat/test-build-search-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ test_that("docsearch.json and sitemap.xml are valid", {
expect_true(xml2::xml_validate(xml2::read_xml(xml), schema))
})

test_that("build_search() builds the expected search.json", {
test_that("build_search() builds the expected search.json with an URL", {
path <- test_path("assets/news")
pkg <- as_pkgdown(path, list(news = list(cran_dates = FALSE)))
pkg <- as_pkgdown(
path,
list(
news = list(cran_dates = FALSE),
url = "https://example.com",
development = list(mode = "devel")
)
)
pkg$bs_version <- 4
tmp <- withr::local_tempdir()
pkg$dst_path <- tmp
Expand All @@ -24,3 +31,24 @@ test_that("build_search() builds the expected search.json", {
jsonlite::write_json(build_search_index(pkg), file.path(tmp, "search.json"), pretty = TRUE)
expect_snapshot_file(file.path(tmp, "search.json"))
})


test_that("build_search() builds the expected search.json with no URL", {
path <- test_path("assets/news")
pkg <- as_pkgdown(
path,
list(
news = list(cran_dates = FALSE),
url = NULL,
development = list(mode = "devel")
)
)
pkg$bs_version <- 4
tmp <- withr::local_tempdir()
pkg$dst_path <- tmp
build_news(pkg)
build_home(pkg)
build_sitemap(pkg)
jsonlite::write_json(build_search_index(pkg), file.path(tmp, "search-no-url.json"), pretty = TRUE)
expect_snapshot_file(file.path(tmp, "search-no-url.json"))
})

0 comments on commit 9309673

Please sign in to comment.