-
Notifications
You must be signed in to change notification settings - Fork 104
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
AcornJSX Refactor #19
Conversation
Wow, this is incredible! From the power of the parser this is definitely a nice solution. The use case is, that I'm using gatsby. The latest version, gives you the raw html that is parsed from markdown. We have the requirement however, that we need to insert custom React components into the markdown. To inject components into that html I see this approach the only way. So the current solution is to put the raw html string into However, as Gatsby is already executing the markdown parser at webpack compile time, at that step, a json representation of the virtual dom could be generated - something like the inferno docs are using: I think for our use case generating something like this would be perfect
So probably the first 350ms could be much faster. Actually, just using So probably for us the most optimal solution would be:
function h(type, props, children) {
return {type, props, children}
}
To sum it up - we're just going through all this trouble to inject React Components into Markdown. It would be really interesting to know your use case @TroyAlford , there must be a good reason you're going through all this trouble. |
My use-case is my wiki project, which is all React. When installed, I want the option for the wiki itself to have a series of custom components specified in a plugin model. Because the wiki already associates metadata with each individual page, and allows HTML-editing as well as WYSIWYG, the benefit of this is that someone who wants to customize their own install of the wiki can now supply custom tags, which automatically get the page metadata bound to them as props. From this point, the wiki maintainer can do things like create a pre-formatted display, with full React functionality, as an available tag within the markup of the wiki - and whoever is writing the article can simply say From a software perspective, this allows the server-side to ONLY store |
Since you posed that perhaps we should think about multiple use-cases... I wonder if we should consider outputting 2 separate components. One intended to be used browser-side, and only 9kb - but more simplistic functionality... and one intended to be used server-side (though someone is certainly welcome to pay the 80kb size increase to have the power client-side) - which uses Acorn. This gives us both sets of flexibility - and allows us to handle both complex cases and simple ones, at least until a better solution comes along (such as finding a way to tree-shake a big portion of Acorn). What do you think of that idea? |
Closes #17, #18, and #20
This is a complete rewrite based on
acorn
andacorn-jsx
- which is the same underlying JSX parser which is used by Babel. As a result, it handles a lot more edge cases. Unfortunately, it also adds something like ~70kb to the size of the minified output. :(That said - I think it's worth discussion. It already passes all existing unit tests at this point, and adds the additional features of being able to parse JSX with non-string props embedded, such as:
It still does not handle parsing function props (
myFn={() => { /* do stuff */ }}
), as doing so will require some further thought. It should also throw away props which include symbols (myProp={foo}
), as the parser won't recognize the value of those symbols. (Though it may be reasonable to either look for abinding
key which matches the symbol name, or something similar?)I'm opening this PR for review and consideration - but not yet sure that it should be merged, due to the significant increase in package size.