Replies: 1 comment
-
Hey @Karlstens here is what I did. I wanted to get around the Path based routing (I like to organize my blog posts by date but I do not want them routed in the blog by date) So I came up with this and it seems to work. (it's not perfect)
posts.data.tsconst pattern = `../posts/**/*.md`;
const data = [] as Post[];
export { data };
export default createContentLoader(pattern, {
excerpt: true,
transform(raw) {
return raw
.map(({ url, frontmatter, excerpt }) => ({
title: frontmatter.title,
author: frontmatter.author ?? blogConfig?.defaultAuthor ?? 'Unknown',
url: frontmatter.url ?? url,
excerpt,
tags: formatTags(frontmatter.tags),
category: frontmatter.category ?? blogConfig?.defaultCategory ?? 'Article',
date: formatDate(frontmatter.date),
}))
.sort((a, b) => b.date.time - a.date.time);
},
});```
### [slug].paths.ts
```ts
import fs from 'fs';
import path from 'path';
export default {
paths() {
return fs
.readdirSync('posts', {
recursive: true,
})
.filter((file) => file.endsWith('.md'))
.map((file_name) => {
const slug = path.parse(file_name).name;
const content = fs.readFileSync(`posts/${file_name}`, 'utf-8');
// console.log('generating path', slug);
return { params: { slug }, content };
});
},
}; [slug].md
post01.md
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Referring to these two articles;
https://vitepress.dev/guide/data-loading#createcontentloader
https://vitepress.dev/guide/routing#paths-loader-file
I am using createContentLoader to generate a list of all my articles, which is working well importing data as posts, and then a v-for to show all my posts frontmatter/lastupdated dynamically. Vue works really well, and I've even built toggle filters, very cool!
I am currently testing routing and the ability to generate markdown files;
docs\shapes[shape].paths.js
docs\shapes[shape].md
And those pages load and generate as expected, and appear in navigation. The @content technique also places entries in the Sidebar which is something I was also struggling with beforehand, so very glad to have found it. 🥳
Where I am now struggling, is I noticed that createContentLoader is failing to collect the Routing generated pages data and supply them in the posts object. Interestingly enough, posts does contain [shape].md which obviously links to a 404, but the generated child pages, in my case, rectangle.md, square.md and large-square.md are simply not listed.
Is there a trick to get createContentLoader and routing paths.js working together?
Beta Was this translation helpful? Give feedback.
All reactions