0

I'd like to get a list of blog posts and the front matter to use in a site's homepage. I've looked around in the documentation of Netlify CMS but I didn't find anything that could work with this use case. I am using SvelteKit for this project

webdev03
  • 5
  • 1
  • 5

1 Answers1

0

It would depend on how the posts are saved. My choice is usually as markdown files. So netlify-cms isn't really involved, except in a round-about manner. It helps to make the files with a pretty GUI but that's it really.

I look at the markdown files and load them with frontmatter to generate the HTML for the blog post pages.

Helpful resources would be Node fs (file system) fs.readdirSync("path/to/markdown") and fs.readFileSync("path/to/file.md"). There are non-sync versions also.

Here's my snippet.

const postsDirectory = join(process.cwd(), 'content/posts')
export function getPostSlugs() {
    return fs.readdirSync(postsDirectory)
}
kalm42
  • 784
  • 9
  • 19
  • I saw a similar thing with `import.meta.glob` with my sveltekit/vite project. It turns out that it won't work in production when I deploy it to netlify, though, unfortunately. – webdev03 Jan 17 '22 at 09:59
  • @webdev03 The snippet I provided must be run in a node context. The fs package is specific to node. It will not work in the browser. – kalm42 Mar 21 '22 at 16:09