Quick and hacky script to extract layers from a drawio diagram and inject them in exported SVG.
Requires Inkscape and drawio desktop to be installed, and assumes the command to be drawio
(not draw.io
).
The pipeline is basically:
- extract drawio diagram as raw XML
- parse XML and get each page's name and index
- convert each page to a separate SVG file
- parse each page's XML and extract layer names and IDs
- inject layer names and IDs in generated SVG
- pass through inkscape to handle some namespace and text tomfoolery
Step 5 changes the structure of the SVG (removes outermost g
tags, puts layers as outermost tags).
Works for my use case but it's probably not the general best way to do this.
The result is a bunch of SVG files with "preserved" layers that can be handled using Inkscape.
To run:
./drawiosvg.py <cmd> <diagram>.drawio
Can also take multiple files:
./drawiosvg.py <cmd> <diagram1>.drawio <diagram2>.drawio
cmd
can be either:
all
, which exports pages as separate SVGs and then injects inkscape layerspages
, which only exports pages as separate SVGslayers
, which only injects layer names in existing SVGs. SVG files should be have names such as<diagram>-<page>.svg
.
Output files are generated in the diagram's directory.
By default text is converted to path (to avoid weirdness with fonts). This means that it basically can't be edited. If you want to export text unchanged, use:
TEXT_TO_PATH=0 ./drawiosvg.py <cmd> <diagram>.drawio
You could also export pages, edit text in the SVGs, and then inject layers:
./drawiosvg.py pages <diagram>.drawio
# edit/fix text in Inkscape
./drawiosvg.py layers <diagram>.drawio