Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions R/tweak-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ tweak_reference_highlighting <- function(html) {
# Add div.sourceCode for copy button
xml2::xml_add_parent(pre[!handled], "div", class = "sourceCode")

# Fix seealso links
seealso_links <- xml2::xml_find_all(
html,
"//code[a and normalize-space(text()) = '()']"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you need the normalize-space here? I would think that it would be pretty unusually to have extra whitespace here.

)
seealso_kids <- xml2::xml_children(seealso_links)
xml2::xml_text(seealso_kids) <- paste0(xml2::xml_text(seealso_kids), "()")
purrr::walk(seealso_links, \(x) {
text_nodes <- xml2::xml_find_all(x, "./text()[normalize-space(.) = '()']")
xml2::xml_remove(text_nodes)
})

invisible()
}

Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-tweak-reference.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ test_that("can highlight 'rmd'", {
expect_equal(xpath_attr(html, "//code//span[not(span)]", "class")[[1]], "an")
})

test_that("fix seealso links", {
skip_if_no_pandoc("2.16")
html <- xml2::read_html(
'<code><a>build_site</a>()</code>'
)
tweak_reference_highlighting(html)

expect_equal(
as.character(xml2::xml_child(html)),
"<body><code><a>build_site()</a></code></body>"
)
})

test_that("fails cleanly", {
html <- xml2::read_xml('<div><pre><code></code></pre></div>')
tweak_highlight_other(html)
Expand Down