-
Notifications
You must be signed in to change notification settings - Fork 0
/
_script.R
40 lines (33 loc) · 1.46 KB
/
_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
library(dplyr)
# clone mermaid repo but remove on exit
temp_dir <- tempfile(pattern = "mermaid")
on.exit(unlink(temp_dir, recursive = TRUE), add = TRUE)
processx::run("git", c("clone", "[email protected]:mermaid-js/mermaid.git", temp_dir))
processx::run("git", c("checkout", "v10.2.4"), wd = temp_dir)
# look for syntax files
syntax_files <- list.files(
paste0(temp_dir, "/packages/mermaid/src/docs/syntax"),
full.names = TRUE,
recursive = TRUE,
pattern = "\\.md$"
)
if (!dir.exists("syntax")) dir.create("syntax")
# convert to qmd file
for (file in syntax_files) {
new_lines <- readr::read_lines(file) %>%
paste(collapse = "\n") %>%
# fix mermaid blocks
gsub("```(mermaid-example|mmd|mermaid)", "```{mermaid}", .) %>%
# remove titles in codeblocks
gsub("```\\{mermaid\\}\n---\ntitle:[^-]*\n---", "```{mermaid}", .) %>%
# fix callout blocks, e.g. from ```warning\n...\n``` to `:::{.callout-warning}\n...\n:::`
gsub("```(note|warning|tip|important|caution)\n(.*?)\n```", ":::{.callout-\\1}\n\\2\n:::", .)
dest_file <- paste0("syntax/", gsub("\\.md", ".qmd", basename(file)))
readr::write_lines(new_lines, dest_file)
}
# # also render to gfm
# for (qmd in list.files("syntax", pattern = "*.qmd$", full.names = TRUE)) {
# dest_file <- paste0(dirname(qmd), "/_", gsub("\\.qmd$", ".md", basename(qmd)))
# out <- system(paste0("quarto render ", qmd, " --to gfm --output -"), intern = TRUE, ignore.stderr = TRUE)
# readr::write_lines(out, dest_file)
# }