Skip to content
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 4 commits into from
Nov 25, 2022

Conversation

tbranyen
Copy link
Owner

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.

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
Copy link
Owner Author

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>
`);

@tbranyen tbranyen merged commit 960c84f into master Nov 25, 2022
@tbranyen
Copy link
Owner Author

Getting close to having mermaid support, was trickier than anticipated given that it uses the DOM to generate SVG instead of doing it virtually :-/

image

// 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);
        });
      }
    }
  },
});

@tbranyen
Copy link
Owner Author

tbranyen commented Nov 25, 2022

Wow I got it working and I'm pretty shocked this works:

image

// 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);
        });
      }
    }
  },
});

@tbranyen tbranyen deleted the fix-create-tree-parse-call branch November 28, 2022 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant