Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# cli (development version)

* Interpolating an `fs_path` (or any object mapped to the `file` style via
the theme's `class-map`) into the target of a link, e.g.
`{.run [text]({path})}`, no longer corrupts the link's URL (#683).

* `keypress()` improvements:
- `timeout` argument to wait at most a given number of seconds for a
key press.
Expand Down
8 changes: 7 additions & 1 deletion R/inline.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,16 @@ inline_transformer <- function(code, envir) {
id <- NULL
}

# Don't apply implicit `class-map` styling (e.g. `fs_path` -> `file`)
# inside URL-building link containers
in_link <- any(vlapply(app$doc, function(x) {
any(x$class %in% c("run", "href", "help", "topic", "vignette"))
}))

rcls <- class(val)
stls <- app$get_current_style()$`class-map`
cls <- na.omit(match(rcls, names(stls)))[1]
if (!is.na(cls)) {
if (!is.na(cls) && !in_link) {
class <- c(class, stls[[cls]])
}

Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-links.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,34 @@ test_that_cli(configs = "plain", links = "all", ".run with custom format", {
})
})

test_that("fs_path is not auto-linked inside a link target (#683)", {
withr::local_options(
cli.hyperlink = TRUE,
cli.hyperlink_run = TRUE
)

# fs_path values must not be implicitly styled as files when they are
# substituted into a link target, as the embedded file link would
# corrupt the target URL.
path <- structure("~/foo.R", class = c("fs_path", "character"))
chr <- "~/foo.R"

# the target should be identical to the plain-character case
expect_equal(
format_inline("{.run ['hi mom']({path})}"),
format_inline("{.run ['hi mom']({chr})}")
)
expect_equal(
format_inline("{.run ['file.R'](pkgdown::preview_page('{path}'))}"),
format_inline("{.run ['file.R'](pkgdown::preview_page('{chr}'))}")
)

# and the target must not contain a nested (file) hyperlink
out <- format_inline("{.run ['hi mom']({path})}")
expect_match(out, "x-r-run:~/foo.R", fixed = TRUE)
expect_false(grepl("file://", out, fixed = TRUE))
})

# -- {.topic} -------------------------------------------------------------

test_that_cli(configs = "plain", links = c("all", "none"), "{.topic}", {
Expand Down
Loading