Convert image relative path to dynamic require expression? #1297
-
Hi everyone, I would like to use next-optimized-images to process images used in mdx files, which requires the It works if I use jsx directly If you open mdx plaground, you can see the mdx transformation result: // mdx
![](./test.jpg) // jsx
<p><img parentName="p" {...{
"src": "./test.jpg",
"alt": null
}}></img></p> So basically mdx just takes out the filepath and puts it as a string to the // jsx
<p><img parentName="p" {...{
"src": require("./test.jpg"),
"alt": null
}}></img></p> I tried to write a remark plugin but it seems whatever value I assign to the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Well, you can also write JSX in MDX, so you can type the component literally? It might be more of a question for the |
Beta Was this translation helpful? Give feedback.
-
@dpyzo0o without seeing the actual transform, it's hard to comment on what works/doesn't. In terms of creating a plugin/transform, use the input and the output as a guide. ![description](./image.jpg) to <img alt="description" src={require("./image.jpg")} /> so look at the two ASTs and transform one to the other https://astexplorer.net/#/gist/b83785f495e910aaa71d631699d5c964/54ba6104c2b206f5805951c6736536f528ea7c25 |
Beta Was this translation helpful? Give feedback.
-
I'm wondering about this one too, my situation is that upgrading from Gatsby v4 to 5, we also update MDX. Previously my mdx files had this: import myImage from '../../image.svg';
<img src={myImage} /> then in my MdxProvider: <MdxProvider components={{img: props => <ImageComponent {props} /> }} /> However with MDX v2 it seems that the Is this true that the |
Beta Was this translation helpful? Give feedback.
@dpyzo0o without seeing the actual transform, it's hard to comment on what works/doesn't.
In terms of creating a plugin/transform, use the input and the output as a guide.
You want to go from:
to
so look at the two ASTs and transform one to the other https://astexplorer.net/#/gist/b83785f495e910aaa71d631699d5c964/54ba6104c2b206f5805951c6736536f528ea7c25