URLs relative to the markdown file #246
-
Is there a way to use URLs to assets relative to the .md file, essentially treating them as imports? Here's an example of what I'm trying to achieve. This is a SvelteKit project with mdsvex:
I want to group my posts and assets together, rather than putting all assets in a public or static folder. So Hello, this is an image
![my image](./my-image.png) of course, this doesn't work. A workaround is to do this: <script>
import myImage from './my-image.png'
</script>
Hello, this is an image
<img src={myImage} /> but I'd like to use the markdown syntax. I wrote a remark plugin that transform relative URLs in links, but the bundler is not aware of the file so it doesn't get included in the build. Ideally, it would transform relative URLs into imports and then return however the bundler processes them (in SvelteKit/Vite's case, a valid URL), but within the context of a remark/rehype plugin I don't think you can do that. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
<script>
import myImage from './my-image.png'
</script>
Hello, this is an image
![my image]({myImage}) Markdown syntax everywhere except the import. |
Beta Was this translation helpful? Give feedback.
-
Technically possible in a plugin but relatively complex. The simplest way I could think to do it is to transform: Hello, this is an image
![my image](./my-image.png) Into: <script>
import MyImage from './my-image.png';
</script>
<img src="{MyImage}" title="my image" /> Then, assuming your bundler is set up to handle images, it will take care of copying across the necessary files. You can then continue using image paths relative to your files as you do with javascript etc. The other alternative is to resolve the files manually and copy them across to the static folder, but at this point you are pretending to be a bundler. Vite will handle this for us and getting images into your JS toolchain can be useful for hashing/ optimisations/ etc. I put together a quick example here: https://runkit.com/pngwn/mdsvex-url-to-import This example handles the case when there is no script and when a script already exists. It will also handle collisions (like when you have two files that would be 'camel cased' to the same value ie I have no idea if it conflicts with other plugins or what happens if there is a context module script block present, but it doesn't look too bad for a quick implementation. |
Beta Was this translation helpful? Give feedback.
-
@mattjennings @pngwn I took inspiration from you and made https://www.npmjs.com/package/mdsvex-enhanced-images. The plugin will make MDsveX process normal Markdown images using enhanced-images. Prior art: |
Beta Was this translation helpful? Give feedback.
Technically possible in a plugin but relatively complex.
The simplest way I could think to do it is to transform:
Into:
Then, assuming your bundler is set up to handle images, it will take care of copying across the necessary files. You can then continue using image paths relative to your files as you do with javascript etc.
The other alternative is to resolve the files manually and copy them across to the static folder, but at this point you are pretending to be a bundler. Vite will handle this for us and getting images into your JS too…