Skip to content
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

Convert file path to import statement in YAML Frontmatter #695

Open
jaekong opened this issue Feb 14, 2025 · 0 comments
Open

Convert file path to import statement in YAML Frontmatter #695

jaekong opened this issue Feb 14, 2025 · 0 comments

Comments

@jaekong
Copy link

jaekong commented Feb 14, 2025

---
thumbnail: ./hello.jpg
---

^ this does not work

Currently, you cannot provide file paths inside source folder in frontmatter and expect MDsveX to automatically import it. Vite's dynamic import cannot predict complex glob at build-time, so it is difficult to implement at SvelteKit side as well. So it would be great if there was an option for MDsveX to directly insert import statement during markdown to svelte transform. (possible use case - assets like thumbnail image path, background music, maybe custom css file)

Usually for this kind of things, you can just use remark / rehype plugin like with mdsvex-relative-images. But frontmatter area cannot have import statement while it's in markdown AST, and when it's in HTML AST it already has <script context="module"> node generated by MDsveX. Currently I am working around this by regex-ing and re-writing <script module> node which is not ideal since it is very likely to break when MDsveX changes something.

Related Issue at mdsvex-relative-images repo (with my workaround): mattjennings/mdsvex-relative-images#7 (comment)

I don't know if implementing this directly inside MDsveX is a good idea or not, but if this seems like a good way to move forward, I think few lines of code above these lines will do the trick.

const fm =
metadata &&
`export const metadata = ${stringified};${newline}` +
`\tconst { ${Object.keys(metadata).join(', ')} } = metadata;`;

@github-project-automation github-project-automation bot moved this to Triage + Refine in mdsvex Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage + Refine
Development

No branches or pull requests

1 participant