-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Add rehype-svgo
to list of plugins
#169
Conversation
Signed-off-by: Tomer Aberbach <[email protected]>
Awesome! This has really good synergy with my own
|
Coincidentally, that's actually exactly why I made it haha. I use
Good catch. Removed the CJS entry point! Force of habit...
Just personally like using this one to combine a bunch of my dev deps into one thing. No worries though; it uses
Ayyy nice. Didn't notice this one. Updated!
I tried this, but the tests fail. For whatever reason, after doing AST -> SVG string -> AST I end up with an
I've actually found it to be okay after you figure out the configuration (thankfully I only had to do this one and now I use the same config everywhere) |
Perhaps a slightly more correct way would involve https://github.com/syntax-tree/xast-util-from-xml. It would then still need to map the xast nodes back to hast nodes manually. That step might be a bit complex. TypeScript works best if you are very explicit. TS cannot narrow So, I’d recommend something along the lines of: visit(tree, (node, index, parent) => {
if (
parent &&
typeof index === 'number' &&
node.tagName === 'svg'
) {
const replacement = fromHtml(
optimize(toHtml(node, { space: `svg` }), svgoConfig).data,
{ fragment: true }
)
parent.children.splice(index, 1, ...replacement.children)
}
}) |
It also looks like svgo uses xast internally already: https://github.com/svg/svgo/blob/main/lib/xast.js. You likely shave of a lot of time by skipping the serializing/parsing that now happens twice by mapping objects and using the svgo internals: https://github.com/svg/svgo/blob/8d6385bd9ab49d1d300a10268930238baa5eb269/lib/svgo.js#L65-L84. I would really recommend looking into this, as it’s why unified exists! So then not all these tools have to parse/stringify between everything! |
rehype-svgo
to pluginsrehype-svgo
to list of plugins
Thanks! |
This comment has been minimized.
This comment has been minimized.
Good feedback by @wooorm! Some additional context: remcohaszing/mermaid-isomorphic#3, https://twitter.com/remcohaszing/status/1777704432039264467 Maybe SVGO is open to addding a public API that accepts and returns a xast tree directly. Just an idea: It would be cool if you add support for SVG in data URLs in |
Good catch with the
TypeScript actually already infers
Neat. I think I agree this would be ideal. I can open up an issue with svgo |
Optimizing inline SVG in data URLs would also be cool |
Initial checklist
Description of changes
Add
rehype-svgo
to the plugin list!