Supongamos que tenemos un archivo de texto con formato markdown ìn.md
y queremos crear otro archivo markdown out.md
contiene sólo cursiva y negrita secciones.
El contenido del archivo en.md:
# Header
There is *italic* and **bold** text!
There is *another italic* and **another bold** text!
library(tidyverse)
text <- read_file("in.md")
bold_texts <- text %>%
str_extract_all("\\*\\*[^\\*]+\\*\\*") %>%
purrr::simplify() %>%
map_chr(~ .x %>% str_remove_all("\\*"))
bold_texts
#> [1] "bold" "another bold"
italic_texts <-
text %>%
str_remove_all(bold_texts %>% map_chr(~ paste0("\\*\\*", .x, "\\*\\*")) %>% paste0(collapse = "|")) %>%
str_extract_all("\\*[^\\*]+\\*") %>%
purrr::simplify() %>%
map_chr(~ .x %>% str_remove_all("\\*"))
italic_texts
#> [1] "italic" "another italic"
out_text <- c("#Bold texts:", bold_texts, "#Italic texts:", italic_texts) %>% paste0(collapse = "\n")
cat(out_text)
#> #Bold texts:
#> bold
#> another bold
#> #Italic texts:
#> italic
#> another italic
write_file(out_text, "out.md")
Creado en 2021-11-23 por el reprex paquete (v2.0.1)