render-diagram
render-diagram copied to clipboard
Render draw.io diagrams from a URL or embedded into a webpage
Diagram renderer
Description
Render diagrams from draw.io from a URL or embedded into a webpage, whilst maintaining interactive support in the diagram, e.g. links, layers and pages.
Usage
Include this script anywhere:
<script src="https://laingsimon.github.io/render-diagram/drawio-renderer.js"></script>
Then include diagrams using any of the below approaches:
Embedded content
<div class="drawio-diagram" data-diagram-data="diagram-content"></div>
This is an analogue of <img src="data:image/png;base64,xxxx"/> and is equivalent to embedding the diagram based on the embed option in draw.io.
Working example
Relative content
<div class="drawio-diagram" data-diagram-url="url"></div>
This is an analogue of <img src="url"/>.
Working example
With options
<div class="drawio-diagram" data-diagram-options='{options}'></div>
NOTE: Make sure the options use double quotes to encapsulate property names, e.g. "toolbar". One of the easiest ways to achieve this is to use single quotes to encapsulate the json blob, as shown above.
Options
Any option that is valid for the draw.io renderer is valid here, some notable examples are:
| option | data type | purpose | default | example |
|---|---|---|---|---|
highlight |
colour-string | The colour to highlight links with | none |
blue |
target |
string | The window name to open links in | - | _blank |
lightbox |
boolean | Whether to enable the lightbox view of the diagram | false |
true |
nav |
boolean | Whether to permit navigation from links | true |
true |
toolbar |
space-delimited-string | Which buttons to show in the toolbar, see below | - | zoom layers |
Toolbar options
Some of the known toolbar options are:
zoom- Whether to show the zoom buttonslayers- Whether to show the layers buttonpages- Whether to show the pages button
If this option is missing or empty then the toolbar will be hidden.
Working example
See other examples and test cases here
Other facilities
Relay option
Open draw.io with drawing content by providing a URL. This is possible with draw.io, but only where the URL is publicly accessible. This facility loads the content in the browser (rather than server side) therefore can be hosted privately by the user/organisation.
Security: The diagram data is accessed in the same manner as if the diagram was downloaded by the user from the given url, the diagram data is not sent/retrieved by any other service.
Other tools
There are some other tools available that permit similar behaviour, namely:
- Draw.io Embed-Diagrams - presents diagrams as PNG files, can click-to-open them as a SVG
- Supports links, pages and layers but only via popup
- https://github.com/jgraph/drawio-html5
- Requires the 'host application' to provide the data to the tool, rather than the image data coming from a URL
Editing diagrams
The following link format can be used to open diagrams in draw.io for editing. The url must be accessible publicly so draw.io can access the drawing.
https://www.draw.io/#Uhttps://laingsimon.github.io/render-diagram/Sample%20file.xml
Example
How it works
The above script will perform the following steps:
- Include a link to the stylesheet for this tools: https://laingsimon.github.io/render-diagram/drawio-renderer.css
- Include a link to the draw.io render script: https://www.draw.io/js/viewer.min.js
Once the page has finished loading - Look for elements with the
drawio-diagramcss-class - For each element, render the diagram based on whether it has a
data-diagram-dataordata-diagram-urlattribute- Pass any settings in
drawio-diagram-options(json blob) to draw.io
- Pass any settings in
Diagrams can only be embedded in one of the following element types:
divspansectionbody
Supported environments
- [x] - Html
- [x] - GitHub pages
- [ ] - GitHub flavoured markdown; script tags are modified in the content
- [x] - Regular markdown
Motivation
Diagrams can be easily embedded within a page by using tooling within Draw.io. This works without any issue, however requires the data about the diagram to be embedded in other content. Diagrams drawn from a file (as produced by saving the diagram to a file) isn't natively supported.
This means that you should save the diagram as a PNG with the data embedded. Once again this works fine, and achieves the expected behaviour as part of this tool - but with the following drawbacks
- It is less clear (although would become common practice) that the PNG contains diagram data
- An XML - or other identified file - would convey this meaning more effectively
- PNGs are non-interactive therefore the following functionality from draw.io isn't supported
- Links
- Pages
- Layers
It was desired that a tool should be able to:
- Render a diagram from a hosted file (so that becomes the only file requiring an edit if the diagram needs to be changed)
- Render a diagram in a familiar fashion - as similar to
imgtags as possible - Render a diagram in SVG format to preserve Link, Page and Layer support
- Render a diagram from any webpage and (github) markdown content
All but the last point has been covered, due to enforced limitations over the <script> tag within markdown content.
Known limitations
- [All]: GitHub flavoured markdown; script tags are modified in the content
- [Relative content]: Diagram content must be present on the same origin/host/address, or CORS must be enabled on the source of the content