Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds test for autolink_curly #117

Merged
merged 4 commits into from
Oct 29, 2021
Merged
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
15 changes: 0 additions & 15 deletions R/downlit-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,6 @@ tweak_children <- function(node, xpath, fun, ..., replace = c("node", "contents"
invisible()
}

autolink_curly <- function(text) {
package_name <- extract_curly_package(text)
if (is.na(package_name)) {
return(NA_character_)
}

href <- href_package(package_name)
if (is.na(href)) {
return(NA_character_)
}

paste0("<a href='", href, "'>", package_name, "</a>")
}


as_xml <- function(x) {
xml2::xml_contents(xml2::xml_contents(xml2::read_html(x)))[[1]]
}
16 changes: 15 additions & 1 deletion R/link.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ autolink <- function(text) {
paste0("<a href='", href, "'>", escape_html(text), "</a>")
}


#' @export
#' @rdname autolink
autolink_url <- function(text) {
Expand All @@ -33,6 +32,21 @@ autolink_url <- function(text) {
href_expr(expr[[1]])
}

autolink_curly <- function(text) {
package_name <- extract_curly_package(text)
if (is.na(package_name)) {
return(NA_character_)
}

href <- href_package(package_name)
if (is.na(href)) {
return(NA_character_)
}

paste0("<a href='", href, "'>", package_name, "</a>")
}


# Helper for testing
href_expr_ <- function(expr, ...) {
href_expr(substitute(expr), ...)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ test_that("can link to functions in registered packages", {
expect_equal(href_expr_(addterm.default()), href_topic_remote("addterm", "MASS"))
})

test_that("can link to package names in registered packages", {
expect_equal(
autolink_curly("{downlit}"),
"<a href='https://downlit.r-lib.org/'>downlit</a>"
)

expect_equal(autolink_curly("{package}"), NA_character_)

# No curly = no link
expect_equal(autolink_curly(""), NA_character_)
})

test_that("can link to functions in base packages", {
expect_equal(href_expr_(abbreviate()), href_topic_remote("abbreviate", "base"))
expect_equal(href_expr_(median()), href_topic_remote("median", "stats"))
Expand Down