diff --git a/NEWS.md b/NEWS.md index 9d61d6aaa..48c095fa8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # sandpaper 0.11.9 (in development) +BUG FIX +------- + +* Links to assets in instructor view no longer render a 404. (reported: + @brownsarahm, #404; fixed: @zkamvar, #408) + CONTINUOUS INTEGRATION ---------------------- diff --git a/R/utils-xml.R b/R/utils-xml.R index 19cbc24e3..f8be4d5ce 100644 --- a/R/utils-xml.R +++ b/R/utils-xml.R @@ -40,12 +40,12 @@ fix_codeblocks <- function(nodes = NULL) { add_code_heading <- function(codes = NULL, labels = "OUTPUT") { if (length(codes) == 0) return(codes) xml2::xml_set_attr(codes, "tabindex", "0") - heads <- xml2::xml_add_sibling(codes, "h3", labels, class = "code-label", + heads <- xml2::xml_add_sibling(codes, "h3", labels, class = "code-label", .where = "before") for (head in heads) { - xml2::xml_add_child(head, "i", + xml2::xml_add_child(head, "i", "aria-hidden" = "true", "data-feather" = "chevron-left") - xml2::xml_add_child(head, "i", + xml2::xml_add_child(head, "i", "aria-hidden" = "true", "data-feather" = "chevron-right") } invisible(codes) @@ -97,7 +97,7 @@ fix_setup_link <- function(nodes = NULL) { if (length(nodes) == 0) return(nodes) links <- xml2::xml_find_all(nodes, ".//a") hrefs <- xml2::url_parse(xml2::xml_attr(links, "href")) - setup_links <- hrefs$scheme == "" & + setup_links <- hrefs$scheme == "" & hrefs$server == "" & hrefs$path == "setup.html" xml2::xml_set_attr(links[setup_links], "href", "index.html#setup") @@ -115,9 +115,21 @@ use_learner <- function(nodes = NULL) { use_instructor <- function(nodes = NULL) { if (length(nodes) == 0) return(nodes) copy <- xml2::read_html(as.character(nodes)) - # lnk <- xml2::xml_find_all(copy, ".//a[not(starts-with(@href, 'http'))]") + # find all local links and transform non-html and nested links --------- + lnk <- xml2::xml_find_all(copy, + ".//a[@href][not(contains(@href, '://')) and not(starts-with(@href, '#'))]" + ) + lnk_hrefs <- xml2::xml_attr(lnk, "href") + lnk_paths <- xml2::url_parse(lnk_hrefs)$path + # links without HTML extension + not_html <- !fs::path_ext(lnk_paths) %in% c("html", "") + # links that are not in the root directory (e.g. files/a.html, but not ./a.html) + is_nested <- lengths(strsplit(sub("^[.][/]", "", lnk_paths), "/")) > 1 + is_above <- not_html | is_nested + lnk_hrefs[is_above] <- fs::path("../", lnk_hrefs[is_above]) + xml2::xml_set_attr(lnk, "href", lnk_hrefs) + # find all images and refer back to source img <- xml2::xml_find_all(copy, ".//img[not(starts-with(@src, 'http'))]") - # xml2::xml_set_attr(lnk, "href", fs::path("instructor/", xml2::xml_attr(lnk, "href"))) xml2::xml_set_attr(img, "src", fs::path("../", xml2::xml_attr(img, "src"))) as.character(copy) } diff --git a/tests/testthat/_snaps/utils-xml.md b/tests/testthat/_snaps/utils-xml.md new file mode 100644 index 000000000..b6fbe9db0 --- /dev/null +++ b/tests/testthat/_snaps/utils-xml.md @@ -0,0 +1,34 @@ +# paths in instructor view that are nested or not HTML get diverted + + Code + xml2::xml_find_all(html_test, ".//a[@href]") + Output + {xml_nodeset (10)} + [1] a + [2] b + [3] c + [4] d + [5] e + [6] f + [7] g + [8] h + [9] i + [10] j + +--- + + Code + xml2::xml_find_all(res, ".//a[@href]") + Output + {xml_nodeset (10)} + [1] a + [2] b + [3] c + [4] d + [5] e + [6] f + [7] g + [8] h + [9] i + [10] j + diff --git a/tests/testthat/test-utils-xml.R b/tests/testthat/test-utils-xml.R index 018b80120..30e8d60b1 100644 --- a/tests/testthat/test-utils-xml.R +++ b/tests/testthat/test-utils-xml.R @@ -1,4 +1,30 @@ + +test_that("paths in instructor view that are nested or not HTML get diverted", { + html_test <- xml2::read_html(commonmark::markdown_html(c( + "