question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Allow to render to any virtual dom implementation using hyperscript

See original GitHub issue

The point of this feature request is to allow better integration of KaTeX inside virtual dom libraries like : React, Preact, Vue.js, Hast, Inferno, Snabbdom… (many more)

Each of these lib basically support an hyperscript implementation to create their flavour of virtual dom :

This feature would be a new method taking an hyperscript implementation like :

/**
 * @param {string} expr - the expression to render
 * @param {object} options - katex rendering options
 * @param {function} h - an hyperscript implementation
 * @return {object} - a (virtual) dom tree
 */
katex.renderToVTree(expr, options, h);

Without this integration, there are many known issues :

  • Right now a library like rehype-katex has to render katex nodes to strings and to reparse them to integrate in their virtual dom implementation (HAST).
  • Many markdown editors that support KaTeX are unable to render the preview with the minimal amount of DOM modification needed, which usually results in preview content being discarded/recreated at each keystroke in the editor.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinbarabashcommented, Feb 9, 2019

There was a similar request to have buildHTML output HAST, see #800. I think having buildHTML output something that is easily consumable by other tools would be good to have. I don’t think that KaTeX needs to define its own DOM representation. As long as style is an object I think we’re okay. In terms of adding a new representation, that feels like a short term solution. Long term, if we’re outputting some version of HAST we may as well use it internally as well. It’s probably easier adding it as a new additional format first and then migrating to use it internally as a separate PR.

1reaction
zipangcommented, Feb 1, 2019

@spontaliku-softaria : thank you for this pertinent remark ! You’re mostly right : there are some importants variations to consider in the implementation of the hyperscript DSL.

Some flavours of hyperscript support the extended (Emmet-like) tagname syntax, like in the following example :

h('#id.class1.class2') 
<=> h('div', { className: 'class1 class2', id: 'id'})

Some of them identify the SVG tags while some other offer a specific hyperscript variant for SVG or support a special namespace attribute :

h('path', {d : 'M 0 0 L 10 10'}) 
<=> svg('path', {d : 'M 0 0 L 10 10'}) 
<=> h('path', {d : 'M 0 0 L 10 10', namespace: 'http://www.w3.org/2000/svg'})

And finally, some implementations allow for a varying signature where arguments are recognized by their type, when not on their expected position :

h('div', 'some text')
<=> h('div', {}, ['some text'])

However, i have found that there is some valid ground for a common form that is accepted by almost every implementations and it is the following one :

h(tagName : String!, properties: Object!, children: [String|hNode]!)

Where

  • all three parameters are expected in their respective positions and not null or undefined,
  • className and class should be both set as a string property that accepts a space separated list of class names
  • namespace="http://www.w3.org/2000/svg" should be added to the properties object to create a SVG node.

It’s not perfect (there are still problems occuring on the attributes of the style property as to whether they should be camelCased or kebab-cased) but it allready gives us a good 80/20 ratio : 20% of efforts for ~ 80% support on every hyperscript library.

Now… that could be the moment to propose a Level 1 and Level 2 specification for hyperscript along with a test suite, and see the implementations converge, but it is another story… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

hyperscript documentation - ///_hyperscript
Hyperscript is a scripting language for doing front end web development. It is designed to make it very easy to respond to events...
Read more >
What does the 'h' stand for in Vue's render method? - CSS-Tricks
The h stands for hyperscript. It's a riff of HTML, which means Hypertext Markup Language: since we're dealing with a script, it's become ......
Read more >
render(element, vnodes) - Mithril.js
Documentation on m.render(), which Mithril.js uses to efficiently patch real DOM trees with virtual DOM nodes.
Read more >
Million.js: A Fast Compiler-Augmented Virtual DOM for the Web
allow developers to choose a JavaScript UI library that fits their project [17] through the virtual DOM at the cost of load time...
Read more >
Differences to React - Preact
The reason Preact does not attempt to include every single feature of React is in order to remain small and focused - otherwise...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found