question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can the frontmatter be dynamic?

See original GitHub issue

Hi, I have a blog created with MDsveX written in Sveltekit and deployed on Netlify. It works amazingly.

But whenever I want to use images I have to use links in the blog post. I want to be able to import photos directly into the .svx files. Here’s an example of what I want to do:

image

Is something like this possible?

E: Can those variables, coverimg and thumbnailimg be exported and then imported along with frontmatter, because that would solve my problem as well?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
babichjacobcommented, Aug 11, 2021

This already works:

---
title: My lovely article
author: Dr. Fabuloso the Fabulous
coverimageAlt: Cool coverphoto
---

<script context="module">
  import coverimage from "$assets/demopost/some-image.jpg"

  metadata.coverimage = coverimage;
</script>

Blog post content
1reaction
pngwncommented, Sep 18, 2021

Interesting. I’ll have a think about this. We could even do something simpler for the common case and have some default handling for certain things:

/** @type {MdsvexOptions} */
export const mdsvexConfig = {
  extensions: [".svelte.md"],
  frontmatter: {
    types: {
      coverimage: 'image-import'
    },
  },
};

That said we could also just detect this automatically (for images) and enable/ disable based on an boolean options, or do it based on extension instead:

/** @type {MdsvexOptions} */
export const mdsvexConfig = {
  extensions: [".svelte.md"],
  frontmatter: {
    types: [
     /\.jpg$|\.png$/, 'image-import' // or a function to handle it
    ],
  },
};

In line with other parts of the design for v1, I’m likely to handle this in an ‘official’ plugin:

import { transformFrontmatter } from 'mdsvex/plugins';

/** @type {MdsvexOptions} */
export const mdsvexConfig = {
  extensions: [".svelte.md"],
  plugins: [
    transformFrontmatter({
       transform: { 
         // not sure if a matcher on the value or a handler for the prop name is most appropriate, probably name based
         match: /\.jpg$|\.png$/,          
         handler: 'import' // builtin or a function to handle it
       } 
    })
  ],
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to create frontmatter dynamically? - support - HUGO
Basically, what I'm trying to do is build something inside the frontmatter. So the user creating the content file (the event owner, in...
Read more >
What is Front Matter and How is it Used to Create Dynamic ...
Front matter is one of two main parts of markdown files. This part allows the storing and accessing of metadata vital to populating...
Read more >
When making dynamic pages with local data, is it possible to ...
On my current site, I am generating dynamic pages based on a local data file. I would love to be able to use...
Read more >
dynamic command doesn't work in the front matter - Reddit
Same issue here. Dynamic templater commands do not work IN YAML FRONTMATTER. It works perfectly fine in the body. The command remains after ......
Read more >
Using Frontmatter with Dynamic Pages in Middleman 4
But I can't use Frontmatter in the template file, so I was thinking I could use a YAML data file instead? I tried...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found