Skip to content

Instantly share code, notes, and snippets.

@yakatz
Created August 7, 2024 14:57
Show Gist options
  • Save yakatz/4bb93359f4e86fb6e20f1a9a6b829695 to your computer and use it in GitHub Desktop.
Save yakatz/4bb93359f4e86fb6e20f1a9a6b829695 to your computer and use it in GitHub Desktop.
Script to generate `mermaid` diagrams
//jshint esversion:8
//jshint node:true
import fs from 'fs';
import path from 'path';
import { run as mermaidRun } from "@mermaid-js/mermaid-cli"
const srcDir = "./mermaid";
const dstDir = "./generated";
const outputFormat = "png";
const puppeteerConfig = ({
headless: 1
});
const quiet = true;
(async () => {
try {
const files = await fs.promises.readdir(srcDir);
for (const file of files.filter(fn => fn.endsWith('.mmd'))) {
const imageName = path.parse(file).name;
const dstFilename = imageName + '.' + outputFormat;
const input = path.join(srcDir, file);
const output = path.join(dstDir, dstFilename);
const statInput = await fs.promises.stat( input );
try {
const statOutput = await fs.promises.stat( output );
if ( statOutput.isFile() && statOutput.mtime >= statInput.mtime) {
console.log("Skipping '%s' because it is already newer than '%s'", output, input);
continue;
}
}
catch (e) {
}
console.log("Generating '%s'->'%s'", input, output);
await mermaidRun(
input,
output,
{
puppeteerConfig,
quiet,
outputFormat,
//parseMMDOptions: {
// mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }, svgId
//}
}
)
}
}
catch (e) {
console.error("We've thrown! Whoops!", e);
}
console.info("Mermaid Generation Done.");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment