Skip to content

Commit 514678d

Browse files
committed
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: VIZ_VERSION=3.17.0 npm pack @viz-js/viz@$VIZ_VERSION tar Oxvf viz-js-viz-$VIZ_VERSION.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 802cbb4 commit 514678d

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

assets/js/application.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,18 @@ var PostelizeAnchor = {
782782
},
783783
}
784784

785+
var Graphviz = {
786+
render: function() {
787+
let vizInstance
788+
[...document.querySelectorAll("pre[class=graphviz]")].forEach(async (x) => {
789+
if (!vizInstance) vizInstance = await Viz.instance()
790+
const svg = vizInstance.renderSVGElement(x.innerText)
791+
x.parentNode.insertBefore(svg, x);
792+
x.style.display = 'none'
793+
});
794+
}
795+
}
796+
785797
// Scroll to Top
786798
$('#scrollToTop').removeClass('no-js');
787799
$(window).on('scroll', function() {

layouts/_default/baseof.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,9 @@ <h1 data-pagefind-meta="title">About{{ if (isset .Params "subtitle") }} - {{ .Pa
191191
{{ end }}
192192

193193
</body>
194+
{{ if .Page.Store.Get "hasGraphviz" -}}
195+
<script src="{{ relURL "js/viz-global.js" }}"> </script>
196+
<script type="text/javascript">Graphviz.render();</script>
197+
{{ end -}}
194198
</html>
195199
{{ 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)