Skip to content

Instantly share code, notes, and snippets.

@ricardobeat
Last active July 3, 2024 13:07
Show Gist options
  • Save ricardobeat/4e8ad51dcd256d32afc572194752233e to your computer and use it in GitHub Desktop.
Save ricardobeat/4e8ad51dcd256d32afc572194752233e to your computer and use it in GitHub Desktop.
Extract SVG shapes from a draw.io mxlibrary file
import fs from 'node:fs'
import path from 'node:path'
let opts = {}
process.argv.forEach(function (val, index, array) {
if (val.startsWith('--')) {
opts[val.slice(2)] = process.argv[index+1]
}
});
const { from, out } = opts
if (!from || !out) {
console.warn("usage: extract --from file.xml --out ./shapes ")
process.exit(1)
}
let input = fs.readFileSync(from).toString()
let data = JSON.parse(input.replaceAll(/<\/?mxlibrary>/g, ''))
if (!fs.existsSync(out)) {
fs.mkdirSync(out)
}
console.log("Exporting shapes to %s", out)
data.forEach((shape, i) => {
let src = Buffer.from(shape.data.split(',')[1], 'base64').toString()
fs.writeFileSync(path.join(out, `shape-${i}.svg`), src, 'utf8')
})
console.log("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment