#' Insert a URL or image in a markdown document
#'
#' This function helps users to paste url/image in markdown
#'
#' @return Character.
#' @author Jiaxiang Li
#'
#' @import rstudioapi
#' @importFrom stringr str_split str_match str_detect
#' @importFrom glue glue
#' @importFrom rebus %R% or DOT one_or_more LOWER optional capture END
# transfer into df
#' @param raw_text character.
transfer2df <- function(raw_text){
df <-
raw_text %>%
stringr::str_split(pattern = '\n', n = Inf, simplify = TRUE) %>%
as.vector() %>%
readr::read_delim(delim = ' ', col_names = FALSE)
return(df)
}
# get output
get_output <- function(df){
if(ncol(df) < 2) {
df <- df %>%
dplyr::mutate(X2 = NA)
}
output <-
df %>%
dplyr::mutate(X2 = purrr::map2_chr(X1, X2, get_title)) %>%
dplyr::transmute(text = glue::glue("1. [{X2}]({X1})")) %>%
dplyr::summarise(text = stringr::str_flatten(text, '\n')) %>%
dplyr::pull()
return(output)
}
# insert output
insert_output <- function(output){
rstudioapi::insertText(output)
}
# friendly notes
friendly_noting <- function(text){
usethis::ui_code_block(text) %>% print()
usethis::ui_done("url(s) already done!") %>% print()
}
# summary
#' Build a url in markdown
#'
#' This function helps users to paste url in markdown
#'
#' @return Character.
#' @author Jiaxiang Li
#'
#' @import rstudioapi
#' @importFrom stringr str_split str_match str_detect
#' @importFrom glue glue
#' @importFrom rebus %R% or DOT one_or_more LOWER optional capture END
m1_v2 <- function(){
text <- get_input() %>%
transfer2df() %>%
get_output()
friendly_noting(text)
insert_output(text)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.