-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/olivroy/reuseme
- Loading branch information
Showing
21 changed files
with
236 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,74 @@ | ||
#' Create a cli link to a GitHub issue | ||
#' Create a markdown link to a GitHub issue | ||
#' | ||
#' In RStudio, links to issues are automatically recognized. | ||
#' This function creates cli ansi links to entries of the form rstudio/rstudio#1100 | ||
#' This function creates intermediate markdown links to entries of the form rstudio/rstudio#1100 | ||
#' | ||
#' Note: doesn't support without <OWNER>/<REPO> | ||
#' Note: doesn't (yet) support without <OWNER>/<REPO> | ||
#' | ||
#' Basically trransform repo/org#xx -> [repo/org#xx](https://github.com/repo/org/issues/xx). | ||
#' | ||
#' Afterwards, we use [markup_href()] to create a cli link | ||
#' @param x A string, usually lines of files that contains issue numbers. | ||
#' | ||
#' @return A formatted linked issue to GitHub issue | ||
#' @return A markdown link linked issue to GitHub issue | ||
#' @export | ||
#' @keywords internal | ||
#' @family inline markup internal helpers | ||
#' @examples | ||
#' link_issue(c("We really need rstudio/gt#1469 to be fixed.")) |> cli::cli_bullets() | ||
link_issue <- function(x) { | ||
#' link_gh_issue(c("We really need rstudio/gt#1469 to be fixed.")) | ||
link_gh_issue <- function(x) { | ||
# Return early if no issue pattern is detected. | ||
if (!any(stringr::str_detect(x, "[:graph:]+/[^#\\s]+#\\d"))) { | ||
regex_gh_issue <- "([:graph:]+/[^#\\s]+)#(\\d+)" | ||
|
||
has_gh_issue <- stringr::str_detect( | ||
x, | ||
regex_gh_issue | ||
) | ||
if (!any(has_gh_issue)) { | ||
return(x) | ||
} | ||
# what we need to do is to t | ||
|
||
li <- stringr::str_split(x, pattern = "\\s") | ||
x_to_change <- x[has_gh_issue] | ||
# x_changed <- | ||
x_changed <- x_to_change |> stringr::str_replace_all( | ||
regex_gh_issue, | ||
paste0("[\\1#\\2](https://github.com/\\1/issues/\\2)") | ||
) | ||
|
||
issue_regex <- "([:graph:]+/[^#\\s]+)#(\\d+)" | ||
x[has_gh_issue] <- x_changed | ||
x | ||
} | ||
|
||
res <- purrr::map(li, function(x) { | ||
purrr::map_chr(x, function(y) { | ||
if (stringr::str_detect(y, issue_regex)) { | ||
rest <- stringr::str_extract(y, "([:graph:]+/[^#\\s]+)#(\\d+)(.*)", 3) | ||
if (!is.na(rest)) { | ||
y <- stringr::str_remove(y, paste0(rest, "$")) | ||
} | ||
rep <- stringr::str_replace_all(y, "([:graph:]+/[^#\\s]+)#(\\d+)", "https://github.com/\\1/issues/\\2") | ||
#' Create a cli href with a markdown link | ||
#' | ||
#' Transforms [text](url) -> {.href [text](url)} | ||
#' @family inline markup internal helpers | ||
#' @param x A character vector | ||
#' @returns A character vector with substrings changed | ||
#' @keywords internal | ||
#' @export | ||
#' @examples | ||
#' markup_href(c("[link](https://google.com)", "{.href [link](https://google.com)}")) | ||
#' | ||
markup_href <- function(x) { | ||
# already excluding markuped strings | ||
# only safe links for now | ||
regex_md_url <- "(?<!\\{\\.href\\s)(\\[.+\\])(\\(https.+\\))(?!\\})" | ||
|
||
has_md_url <- stringr::str_detect( | ||
x, | ||
regex_md_url | ||
) | ||
if (!any(has_md_url)) { | ||
return(x) | ||
} | ||
x_to_change <- x[has_md_url] | ||
x_changed <- x_to_change |> stringr::str_replace_all( | ||
regex_md_url, | ||
paste0("{.href \\1\\2}") | ||
) | ||
|
||
y <- paste0("{.href [", y, "](", rep, ")}", rest) | ||
} | ||
y | ||
}) | ||
}) | ||
purrr::map_chr(res, \(x) paste(x, collapse = " ")) | ||
x[has_md_url] <- x_changed | ||
x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.