Skip to content

Commit

Permalink
Merge f87ce7f into 960c84f
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Nov 25, 2022
2 parents 960c84f + f87ce7f commit 762556b
Show file tree
Hide file tree
Showing 8 changed files with 3,886 additions and 239 deletions.
13 changes: 2 additions & 11 deletions packages/diffhtml-static-sync/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ process.env.NODE_ENV = 'production';

let interval = null;
const domNodes = new Map();
const { html, outerHTML } = diffhtml;
const { outerHTML, html } = diffhtml;

window.staticSyncHandlers = new Set();
window.staticSyncSocket = undefined;
Expand Down Expand Up @@ -80,16 +80,7 @@ function open() {
ext === 'md'
)
) {
const children = html(markup);

console.log(children, markup);

if (children.childNodes.length > 1) {
outerHTML(document.documentElement, children.childNodes[1]);
}
else {
outerHTML(document.documentElement, children);
}
outerHTML(document.documentElement, html(markup));
}
// All other files cause a full page reload.
else {
Expand Down
42 changes: 40 additions & 2 deletions packages/diffhtml-website/build/generate.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
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({ nodeName, childNodes }) {
if (nodeName === 'mermaid') {
if (childNodes[0].nodeType === Internals.NODE_TYPE.TEXT) {
try {
mermaid.render('mermaid', childNodes[0].nodeValue.trim(), svg => {
// Replace with the newly rendered SVG
childNodes[0] = html(svg);
});
} catch (e) {
childNodes[0] = html`<pre>${e.stack}</pre>`;
}
}
}
},
});

const renderer = {};

// Patch the renderer to allow for better anchor tags to be generated.
Expand Down
Loading

0 comments on commit 762556b

Please sign in to comment.