r-lib / downlit

Syntax Highlighting and Automatic Linking
https://downlit.r-lib.org
Other
90 stars 22 forks source link

FR: Allow use of https://search.r-project.org refmans for autolinking #195

Open TimTaylor opened 3 months ago

TimTaylor commented 3 months ago

Would you consider allowing users to specify the use of the help pages at https://search.r-project.org as opposed to https://rdrr.io/ (or perhaps any arbitrary alternative)?

E.g. it would be nice if we could make grep() documentation linked to

https://search.r-project.org/R/refmans/base/html/grep.html

instead of

https://rdrr.io/r/base/grep.html

hadley commented 3 months ago

Yeah, now that pages exist we should use them.

maelle commented 1 month ago

I am here to plug yet another alternative to rdrrr.io: R-universe by @jeroen. For packages that are not base R packages, that is.

# is babelquarto on R-universe
name <- "babelquarto"
package <- httr2::request("https://r-universe.dev/api/search") |>
  httr2::req_url_query(q = sprintf('package:%s', "babelquarto")) |>
  httr2::req_perform() |>
  httr2::resp_body_json()

str(package, max.level = 1)
#> List of 5
#>  $ results:List of 1
#>  $ query  :List of 1
#>  $ skip   : int 0
#>  $ limit  : int 100
#>  $ total  : int 1
# fortunately there is only one package with that name
# what universe
universe <- package$results[[1]]$`_user`

# topics
package_info <- httr2::request(
  sprintf(
  "https://%s.r-universe.dev/api/packages/%s",
    universe, name
  )
  )|>
  httr2::req_perform() |>
  httr2::resp_body_json()

# so an URL is
# although probably no need to use the API to find what topic a function belongs to
topic <- "render_book"
topic_info <- purrr::keep(package_info$`_help`, \(x) topic %in% x$topics)
if (length(topic_info) == 1) {
  sprintf(
  "https://%s.r-universe.dev/%s/doc/manual.html#%s",
    universe, name, topic_info[[1]]$page
  )
}
#> [1] "https://ropensci.r-universe.dev/babelquarto/doc/manual.html#render"

Created on 2024-10-08 with reprex v2.1.0