-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby-remark-images): Use babel, and fallback to cheerio #21782
feat(gatsby-remark-images): Use babel, and fallback to cheerio #21782
Conversation
|
||
return resolve(node) | ||
return useBabel().catch(() => useCheerio()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given the potential perf hit (we should benchmark) - would it be feasible to just check node.type
and if it's html
use cheerio and if it's jsx
use babel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use something like https://www.npmjs.com/package/benchmark to check this vs current implementation using input that works now with cheerio (so using some remark AST fixture as input - likely couple of those). I'm very not comfortable with this change as-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, I haven't even thought about that 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. But I let cheerio for malformed JSX. Is it worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if MDX always sets HTML blocks as JSX, or if it somehow distinguishes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I don't know either. @johno halp ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as for "malformatted" jsx part - wouldn't gatsby-plugin-mdx throw exception even before calling gatsby-remark-images
if there was malfortmatted input that mdx can't handle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess MDX would first try to parse any block as JSX, and if it fails it considers the block as regular text. My supposition is based on this (supposedly working) snippet:
---
title: Hello World
---
import FooBar from 'foobar';
Hello world!
<FooBar />
Then it would make sense to consider that blocks of type jsx
would hold valid JSX, and therefore to only parse them using Babel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump @johno
I didn't take into account that I've also added the ability to force a link or no link, via the Regarding performance hit, all the JSX tests and my actual use cases are handled by |
Up |
Can you expand a bit on this - why this is needed? this seems to add a whole lot complexity? |
I'm designing a site with blog posts, some of them contains images whose layout has been purposefully set, that's why I'm using MDX. Some of the images link to external websites. With the current behavior, all the images would have been linked, some of them to external websites, the others to the original themselves. I think this would be confusing for visitors and I have preferred to disable links on the latters. Do you see any way to achieve that otherwise? I think I can't put the plugin twice in the config list, because it's really a layout for particular posts (I'm already using multiple filesystem sources to map to different content types with different templates). The last commit has the effects:
|
Ok I achieved the same without forcing links. I retrieve images' srcSet manually, actually it somewhat duplicates what the plugin does. I'm closing this PR and will create two, one for the Babel fix itself, and one for the new feature (with force stuff). |
Description
New take for supporting React within MDX.
This first tries to find and replace img tags using Babel with React support. If it's not valid JSX, it fails and uses the current algorithm using Cheerio.
Pro: This fully supports JSX syntax, so attributes like
<Component attr={func()}>
will be kept, whereas before this patch this code would result in<component attr="{func()">
and therefore would have broken React bindings.Con: This might slow down processing of current working pages, as each HTML blocks will first go through Babel, fail early, and go through Cheerio. But I haven't made any benchmark.
Documentation
I guess a mention on https://www.gatsbyjs.org/docs/working-with-images-in-markdown/#using-the-mdx-plugin should be enough.
Related Issues
Fixes #19785