Injecting variables / including static assets (ie. images)
See original GitHub issueFirst off, thanks for creating this! I’ve only found svelte/sveltekit recently, and together with this I’ve been really enjoying the process of building, and it’s very well put together! This markdown implementation is very clean compared to my previous template-based setup. I’m new to this system, please let me know if I’m asking in the wrong place or coming at this from the wrong direction. I’m hoping I’m missing something simple.
TL;DR: I’d like to optimize my images (used in markdown documents) and pass in the updated sources at build time. I’m assuming there must be a way to pass dynamic variables into MDsveX at render (via (await import(filename)).default.render()
?) like other svelte components can accept data via export let data
, so I can pass in updated URLs.
My use case: I’m trying to build a static blog that avoids unecessary bloat (filesize, huge javascript bundles, etc), and I was hoping svelte would let me do that and use a modern component system. My content structure is similar to alec’s in https://github.com/pngwn/MDsveX/issues/122#issuecomment-715471995 (folders containing page.md and a series of images/static assets). I’m using MDsveX for the content of posts and pages where I can. I’m import()
ing the markdown files on the server side (+page.server.ts), and then including the result of page.default.render()
in +page.svelte with {@html}
. This is working great for loading markdown!
My issue is that I’d like to optimize my images/other assets, and include multiple sizes/formats. I currently can’t know their final URLs until build time. I’d like to be able to include these assets in my markdown documents, but I can’t find a way to get the URLs in there on server-side.
I’m currently exporting any assets I need in a document via its frontmatter so I can retrieve the assets, optimize them, and have them ready to compile the file body. This works up to that point, but I can’t find a way to pass the information into the document for use.
Workarounds I’ve come up with, which I haven’t tried yet:
- I’m currently optimizing my images in a svelte endpoint, after importing the markdown document but before rendering. I’m not sure if a markdown plugin could do this earlier in the process, and make the data available where I need it?
- A custom frontmatter parser or transformer, ala #287 (I’m unsure if this is out of scope, or if the ‘simple’ transformers described there were implemented?)
- Creating a ‘variables’ file which I import from the markdown document, updating it at build time and reimporting the markdown document.
If there’s a whole different way I should be approaching this, I’m open to feedback. Thank you for your time!
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top GitHub Comments
@nosovk I wanted to thank you again for the help, and I found a slight tweak for my use-case which I hoped might be helpful to you as well. I don’t mean to come off as presumptous, sorry if you knew this already or this isn’t useful.
In the current version of the transform plugin, at least in my setup, the code injected by the mdsvex-frontmatter-to-import only runs when I navigate to the blog posts themselves, so thumbnails for each post are only available when I’m on their route (/blog/post-slug), and there are no thumbnails available from my equivalent of getPublishedPosts(). So it’s impossible to include thumbnails on a /blog page alongside post snippets.
I changed my version to instead inject into
<script context="module"></script>
instead of just<script></script>
, and now the transformer-injected code runs when I use my equivalent of getPublishedPosts(), and I can use thumbnails on my /blog page.Hope this might help, and thanks again either way!
As far as this issue is concerned, I think the answer seems to be “You can’t inject data when
import
ing markdown. To inject data, you have to use sveltekit’s layout/server system, or transform plugins.” My solution is working well enough on my end that I’m confident closing the issue. I feel it would be beneficial to consider including this pattern more explicitly in the documentation, but that’s a separate issue. Thank you for your time!@nosovk Thank you for the examples! I’ve got an adapted version of this transformer working for me very nicely.
With that said, I’ve just discovered that this workflow (import() markdown in +server, pass it to +page.svelte, render with {@html}) doesn’t seem to load JS for components, so everything becomes readonly. With this in mind, it seems like this import() setup just doesn’t do what I need so I’ll have to try a workflow closer to mdsvex-template.
Edit: With testing, I’m still able to use the frontmatter transformers alongside importing data with
export let data
, so I’m doing that. Saves me from having to run that transformer in +server. It’s a bit odd, like I can’t use [params] to get the post slug anymore so I need to scrape the URL, but the stuff I cared about is better so far. I can load arbitrary data I in+layout.server
. which was the original point of this ticket.I still think there’s value to keeping content separate from your routes if possible, and I feel certain I’m still going to run into problems with keeping source media files under routes, but so far this is promising.