Skip to content

Commit

Permalink
Support mermaid graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Nov 25, 2022
1 parent 9a7e73a commit b15b05f
Show file tree
Hide file tree
Showing 5 changed files with 2,203 additions and 42 deletions.
34 changes: 29 additions & 5 deletions packages/diffhtml-website/build/generate.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
const { readFileSync, writeFileSync, existsSync, mkdirSync } = require('fs');
const { join, dirname } = require('path');
const { html, toString, use } = require('diffhtml');
const { html, toString, use, Internals } = require('diffhtml');
const { marked } = require('marked');
const mermaid = require('mermaid');
const flattenPages = require('./util/flatten-pages');
const { keys } = Object;
const { assign, keys } = Object;

// Ensure Components middleware is loaded since Layout is a class
// component and toString will pick it up automatically.
require('diffhtml-components');
//use(require('diffhtml-middleware-linter')());

// Create a jsdom and svgdom merged environment for Mermaid to work.
const SVG = require('svgdom');
const { JSDOM } = require('jsdom');
const { window } = new JSDOM('');

// Patch the Element constructor which is inherited by SVGElement to contain
// the getBBox method to avoid runtime errors with mermaid.
window.Element.prototype.getBBox = SVG.SVGGraphicsElement.prototype.getBBox;

// Unfortunately this patching has to occur in order for the sanitize method
// to return the input and not break under mermaid. Would be great to have
// a fix that didn't involve this.
Object.prototype.sanitize = x => x;

assign(globalThis, {
document: window.document,
window,
});

// Mermaid parsing
use({
createTreeHook(...all) {
if (all[0].nodeName === 'mermaid') {
console.log(all);
createTreeHook({ nodeName, childNodes }) {
if (nodeName === 'mermaid') {
if (childNodes[0].nodeType === Internals.NODE_TYPE.TEXT) {
mermaid.render('mermaid', childNodes[0].nodeValue.trim(), svg => {
// Replace with the newly rendered SVG
childNodes[0] = html(svg);
});
}
}
},
});
Expand Down
Loading

0 comments on commit b15b05f

Please sign in to comment.