Skip to content

Instantly share code, notes, and snippets.

@JRJurman
Created May 18, 2023 03:56
Show Gist options
  • Save JRJurman/ee39f3418d4bbdc74e6e9b0ba7c7c220 to your computer and use it in GitHub Desktop.
Save JRJurman/ee39f3418d4bbdc74e6e9b0ba7c7c220 to your computer and use it in GitHub Desktop.
Simple Excalidraw Build Script using excalidraw-to-svg
const fs = require('fs');
const glob = require('glob');
const excalidrawToSvg = require('excalidraw-to-svg');
// Script to read in .excalidraw files and write svg files to dist
// these follow the same folder structure inside of diagrams (if we ever add folders)
const excalidrawFiles = glob.sync('./diagrams/**/*.excalidraw');
excalidrawFiles.forEach((fileName) => {
/* read files and turn into SVG */
const diagram = fs.readFileSync(fileName, 'utf8');
const svg = excalidrawToSvg(diagram);
/* changes to DOM for presentation */
// make the svg centered on the page
svg.style.margin = 'auto';
/* write files to dist folder */
// change the file name to make the final svg file name
// e.g. diagrams/c4/JellyfishContext.excalidraw -> dist/c4/JellyfishContext.svg
const svgFileName = fileName.replace('diagrams', 'dist').replace('.excalidraw', '.svg');
const svgDirectory = svgFileName.split('/').slice(0, -1).join('/');
// make the directories and write the final file
fs.mkdirSync(svgDirectory, { recursive: true });
fs.writeFileSync(svgFileName, svg.outerHTML);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment