Skip to content

Commit d8b2513

Browse files
dschojvns
authored andcommitted
Add support for Graphviz dot diagrams
While Mermaid diagrams are more and more common on the internet, thanks to GitHub's support in their GitHub-flavored Markdown codeblocks, it is often frustratingly difficult to make elegant diagrams with them and to avoid overlapping or unnecessarily long edges. Graphviz, in contrast, allows much more control over the layout. The downside is that Graphviz is a command-line program, not a JavaScript library, and would require the diagrams to be pre-rendered and committed into the repository (a recipe for out-of-sync files). Enter `viz.js` (https://github.com/mdaines/viz-js). Essentially, it is a WebAssembly build of Graphviz. Therefore, it _does_ run in the browser. The downside, compared to Mermaid, is the size: While `mermaid.min.js` weighs around 25 kB, `viz-global.js` weighs around 1.4 MB. And I did not find any CDN that serves a current version, therefore I had to download it: npm pack @viz-js/viz tar Oxvf viz-js-viz-3.16.0.tgz package/dist/viz-global.js \ >static/js/viz-global.js With this commit, Graphviz dot diagrams can be added like this: {{< graphviz >}} digraph { a -> b -> c } {{< /graphviz >}} Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a175ea2 commit d8b2513

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

layouts/_default/baseof.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,19 @@ <h1 data-pagefind-meta="title">About{{ if (isset .Params "subtitle") }} - {{ .Pa
197197
mermaid.initialize({ startOnLoad: true });
198198
</script>
199199
{{ end -}}
200+
{{- if .Page.Store.Get "hasGraphviz" }}
201+
<script src="{{ relURL "js/viz-global.js" }}"> </script>
202+
<script type="text/javascript">
203+
(() => {
204+
let vizInstance
205+
[...document.querySelectorAll("pre[class=graphviz]")].forEach(async (x) => {
206+
if (!vizInstance) vizInstance = await Viz.instance()
207+
const svg = vizInstance.renderSVGElement(x.innerText)
208+
x.parentNode.insertBefore(svg, x);
209+
x.style.display = 'none'
210+
});
211+
})();
212+
</script>
213+
{{ end }}
200214
</html>
201215
{{ end }}

layouts/shortcodes/graphviz.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<pre class="graphviz">
2+
{{ .Inner | htmlEscape | safeHTML }}
3+
</pre>
4+
{{ .Page.Store.Set "hasGraphviz" true }}

static/js/viz-global.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)