-
Notifications
You must be signed in to change notification settings - Fork 96
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
Injecting variables / including static assets (ie. images) #475
Comments
We have the similar issue. Current workaround is transform plugin. We define link to asset in formatter, after in processor we change it to "dynamic" import with viteimagetools, and store resulting asset links in metadata property. As a result we can use new generated field in layout. |
And for images inside md, relative to .md file there was another solution mentioned somewhere in discussions. Probably it could be done better if move to tag, but that one also works. |
I appreciate the response! I will look into a transform plugin as a workaround.
I can't find a repository at NodeArt/BusinessForUkraine. Is it possible this is a private repo? |
Oups, sorry, my fault. |
it just note updated to new +page schema, but transform implementation is the same. |
@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: 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. |
@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 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 |
For anyone who is trying to host their seveltkit site on github pages, you might've already resolved all the links in the app like this <script>
import { base } from "$app/paths";
</script>
<img src={`${base}/assets/img.png`} /> Just do the same thing in <script context="module">
import { base } from "$app/paths";
</script>
<img src="{base}/assets/Images/Articles/01.webp"/> Note: don't use To author:I know this is not a templating language, but it would be nice to directly pass the props from import { base } from "$app/paths";
const post = await import(
`../../../data/articles/${params.type}/${params.slug}.md`
);
return {
html: post.default.render({base: base}).html,
meta: post.metadata,
}; and in the markdown: <img src="%base%/assets/Images/Articles/01.webp"/> |
First 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 viaexport 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 #122 (comment) (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 ofpage.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:
If there's a whole different way I should be approaching this, I'm open to feedback. Thank you for your time!
The text was updated successfully, but these errors were encountered: