Skip to content
Snippets Groups Projects
Commit 306caca1 authored by David Dorchies's avatar David Dorchies :zany_face:
Browse files

feat(mermaid): improve cache management

Refs #14
parent 7910db78
No related branches found
No related tags found
No related merge requests found
Pipeline #134177 passed
......@@ -12,7 +12,9 @@
#' @param diagram Diagram in mermaid markdown-like language or file (as a connection or file name) containing a diagram specification
#' @param theme Mermaid theme (See https://mermaid.js.org/config/theming.html#available-themes)
#' @param format Image format (either `"jpg"`, or `"png"`, or `"svg"`)
#' @param file Path to the downloaded image
#' @param file.dest Path to the downloaded image
#' @param link Link generated by [mermaid_gen_link]
#' @param server URL of the server used to generate the link
#'
#' @return The path to the downloaded image.
#' @export
......@@ -33,12 +35,13 @@
mermaid <- function(diagram,
format = "png",
theme = "default",
file = file.path(tempdir(), paste0(rlang::hash(diagram), ".", format))) {
if (!file.exists(file)) {
link <- mermaid_gen_link(diagram, theme = theme, format = format)
download.file(link, file, quiet = TRUE, mode = "wb")
file.dest = file.path(tempdir(),
paste0(rlang::hash(link), ".", format)),
link = mermaid_gen_link(diagram, theme = theme, format = format)) {
if (!file.exists(file.dest)) {
download.file(link, file.dest, quiet = TRUE, mode = "wb")
}
return(file)
return(file.dest)
}
#' Compress data in pako format
......@@ -65,7 +68,7 @@ return(compressed_data)
#' @rdname mermaid
#' @export
mermaid_gen_link <- function(diagram, theme = "default", format = "png") {
mermaid_gen_link <- function(diagram, theme = "default", format = "png", server = "https://mermaid.ink") {
is_connection_or_file <- inherits(diagram[1], "connection") ||
file.exists(diagram[1])
if (is_connection_or_file) {
......@@ -80,7 +83,7 @@ mermaid_gen_link <- function(diagram, theme = "default", format = "png") {
deflated <- pako_deflate(jGraph)
dEncode = gsub("\n", "", jsonlite::base64url_enc(deflated))
mode <- ifelse(format != "svg", "img", "svg")
link = sprintf('https://mermaid.ink/%s/pako:%s', mode, dEncode)
link = sprintf("%s/%s/pako:%s", server, mode, dEncode)
if (format != "svg") {
link <- paste0(link, "?type=", format)
}
......
......@@ -9,10 +9,16 @@ mermaid(
diagram,
format = "png",
theme = "default",
file = file.path(tempdir(), paste0(rlang::hash(diagram), ".", format))
file.dest = file.path(tempdir(), paste0(rlang::hash(link), ".", format)),
link = mermaid_gen_link(diagram, theme = theme, format = format)
)
mermaid_gen_link(diagram, theme = "default", format = "png")
mermaid_gen_link(
diagram,
theme = "default",
format = "png",
server = "https://mermaid.ink"
)
}
\arguments{
\item{diagram}{Diagram in mermaid markdown-like language or file (as a connection or file name) containing a diagram specification}
......@@ -21,7 +27,11 @@ mermaid_gen_link(diagram, theme = "default", format = "png")
\item{theme}{Mermaid theme (See https://mermaid.js.org/config/theming.html#available-themes)}
\item{file}{Path to the downloaded image}
\item{file.dest}{Path to the downloaded image}
\item{link}{Link generated by \link{mermaid_gen_link}}
\item{server}{URL of the server used to generate the link}
}
\value{
The path to the downloaded image.
......
test_that("Conversion of mmd to pako works", {
diagram <- "flowchart LR\n A --> B"
diagram <- "flowchart LR\n A --> B"
test_that("mermaid_gen_link: conversion of mmd to pako works", {
pako <- "https://mermaid.ink/img/pako:eNqrVkrOT0lVslJKy8kvT85ILCpR8AmKyVNQcFTQ1bVTcFLSUcpNLcpNzExRsqpWKslIzQUpTklNSyzNKVGqrQUAjIcUfg?type=png"
# multiline string
expect_equal(mermaid_gen_link(diagram), pako)
# character vector
diagram <- strsplit(diagram, "\n")[[1]]
expect_equal(mermaid_gen_link(diagram), pako)
# from file
f <- tempfile()
writeLines(diagram, f)
expect_equal(mermaid_gen_link(f), pako)
})
test_that("mermaid returns a file", {
f <- mermaid(diagram)
expect_true(file.exists(f))
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment