Skip to content

Commit 2f5c40b

Browse files
committed
Move to slightly better location; tweak implementation a little
1 parent ab73fd3 commit 2f5c40b

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# pkgdown (development version)
22

3+
* Links generated with `\code{\link{foo}()}` now have the `()` moved into the `<a>` in the generated output (@maelle).
34
* Plots in dark mode are now transformed with a CSS filter to improve their
45
visibility (thanks to @gadenbuie).
56
* Updated the translations vignette with instructions for building non-English sites through Github Actions.

R/tweak-page.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tweak_page <- function(html, name, pkg = list(bs_version = 3)) {
1717

1818
tweak_anchors(html)
1919
tweak_link_md(html)
20+
tweak_link_seealso(html)
2021
tweak_link_external(html, pkg = pkg)
2122
tweak_img_src(html)
2223
tweak_strip(html, !identical(pkg$development$mode, "release"))

R/tweak-reference.R

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ tweak_reference_highlighting <- function(html) {
2323
# Add div.sourceCode for copy button
2424
xml2::xml_add_parent(pre[!handled], "div", class = "sourceCode")
2525

26-
# Fix seealso links
27-
seealso_links <- xml2::xml_find_all(
28-
html,
29-
"//code[a and text() = '()']"
30-
)
31-
seealso_kids <- xml2::xml_children(seealso_links)
32-
xml2::xml_text(seealso_kids) <- paste0(xml2::xml_text(seealso_kids), "()")
33-
text_nodes <- xml2::xml_find_all(
34-
seealso_links,
35-
"./text()[normalize-space(.) = '()']"
36-
)
37-
xml2::xml_remove(text_nodes)
38-
3926
invisible()
4027
}
4128

R/tweak-tags.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ tweak_link_R6 <- function(html, cur_package) {
167167
invisible()
168168
}
169169

170+
# Fix seealso links
171+
tweak_link_seealso <- function(html) {
172+
seealso_links <- xml2::xml_find_all(html, "//code[a and text() = '()']")
173+
174+
# add () inside link
175+
seealso_text <- xml2::xml_children(seealso_links)
176+
xml2::xml_text(seealso_text) <- paste0(xml2::xml_text(seealso_text), "()")
177+
178+
# remove () outside the link
179+
text_nodes <- xml2::xml_find_all(seealso_links, "./text()[. = '()']")
180+
xml2::xml_remove(text_nodes)
181+
182+
invisible()
183+
}
184+
170185
tweak_tables <- function(html) {
171186
# Ensure all tables have class="table" apart from arguments
172187
table <- xml2::xml_find_all(html, ".//table")

tests/testthat/test-tweak-reference.R

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,6 @@ test_that("can highlight 'rmd'", {
141141
expect_equal(xpath_attr(html, "//code//span[not(span)]", "class")[[1]], "an")
142142
})
143143

144-
test_that("fix seealso links", {
145-
skip_if_no_pandoc("2.16")
146-
html <- xml2::read_html('<code><a>build_site</a>()</code>')
147-
tweak_reference_highlighting(html)
148-
149-
expect_equal(
150-
as.character(xml2::xml_child(html)),
151-
"<body><code><a>build_site()</a></code></body>"
152-
)
153-
})
154-
155144
test_that("fails cleanly", {
156145
html <- xml2::read_xml('<div><pre><code></code></pre></div>')
157146
tweak_highlight_other(html)

tests/testthat/test-tweak-tags.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ test_that("tweak_img_src() doesn't modify absolute links", {
262262
)
263263
})
264264

265+
test_that("trailing () moved inside links", {
266+
skip_if_no_pandoc("2.16")
267+
html <- xml2::read_html('<code><a>build_site</a>()</code>')
268+
tweak_link_seealso(html)
269+
270+
expect_equal(xpath_contents(html, ".//code"), "<a>build_site()</a>")
271+
})
272+
265273
# stripped divs etc -------------------------------------------------------
266274

267275
test_that("selectively remove hide- divs", {

0 commit comments

Comments
 (0)