Skip to content

Instantly share code, notes, and snippets.

@elipousson
Created April 4, 2024 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elipousson/1a93b405305e558f21caf58cd166ca77 to your computer and use it in GitHub Desktop.
Save elipousson/1a93b405305e558f21caf58cd166ca77 to your computer and use it in GitHub Desktop.
Example script for converting an excalidraw diagram to a d2 diagram
library(dplyr)
library(rlang)
library(purrr)
# library(d2r)
library(minixcali)
path <- "Untitled-2024-03-28-1327.excalidraw"
file <- xkd_read(path, simplifyVector = TRUE)
fmt_shape <- function(type) {
case_match(
type,
"ellipse" ~ "oval",
"rectangle" ~ NA_character_,
.default = type
)
}
elements <- file[["elements"]] |>
mutate(
text = stringr::str_remove_all(text, "\n")
)
containers <- elements |>
filter(type != "text")
text <- elements |>
filter(type == "text")
arrows <- elements |>
filter(type == "arrow")
d2_text <- text |>
select(id, containerId, text) |>
left_join(
containers |>
select(id, type),
by = join_by(containerId == id)
) |>
filter(
type != "arrow",
) |>
mutate(
type = fmt_shape(type)
) |>
pmap_chr(
\(containerId, text, type, ...) {
d2_container(
id = containerId,
label = text,
shape = type
)
}
)
connections <- set_names(
arrows$endBinding$elementId,
arrows$startBinding$elementId
)
# connections <- connections[!is.na(connections)]
d2_connections <-
d2_connections(
connections
)
c(
d2_text,
d2_connections
) |>
d2r::d2_render(
"example-excalidraw.png"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment