-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call createTree on parser close, not open #289
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the new parser createTree was called with only a tag name and not the attributes or childNodes. This PR fixes the new parser to wait until the element is closed before calling createTree. This work uncovered some inconsistencies and improved the parser by closing out the pointer and resetting to the parent in a function.
tbranyen
commented
Nov 25, 2022
With this fix we'll be able to hook into the parser again and do fun things like make mermaid tags (something I want for the website to display diagrams): import { use, innerHTML } from 'diffhtml';
use({
createTreeHook({ nodeName, childNodes }) {
if (nodeName === 'mermaid') {
childNodes[0].nodeValue = mermaid(childNodes[0].nodeValue);
}
}
});
innerHTML(document.body, `
<mermaid>
Some mermaid text
</mermaid>
`); |
Getting close to having mermaid support, was trickier than anticipated given that it uses the DOM to generate SVG instead of doing it virtually :-/ // Mermaid parsing
use({
createTreeHook({ nodeName, childNodes }) {
if (nodeName === 'mermaid') {
if (childNodes[0].nodeType === Internals.NODE_TYPE.TEXT) {
// FIXME no jsdom support
Object.prototype.getBBox = () => ({
width: 0,
height: 0,
});
// FIXME no jsdom support
Object.prototype.sanitize = (x) => x;
mermaid.render('id1', childNodes[0].nodeValue, svg => {
// Replace with the newly rendered SVG
childNodes[0] = html(svg);
});
}
}
},
}); |
Wow I got it working and I'm pretty shocked this works: // Create a jsdom and svgdom merged environment for Mermaid to work.
// 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) {
mermaid.render('mermaid', childNodes[0].nodeValue.trim(), svg => {
// Replace with the newly rendered SVG
childNodes[0] = html(svg);
});
}
}
},
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the new parser createTree was called with only a tag name and not the attributes or childNodes. This PR fixes the new parser to wait until the element is closed before calling createTree. This work uncovered some inconsistencies and improved the parser by closing out the pointer and resetting to the parent in a function.