Skip to content

Commit a232072

Browse files
committed
Fix plugin error when no post
1 parent 147cfad commit a232072

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

plugins/vite-plugin-rss.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,43 @@ interface RssPluginOptions {
2020
export default function rssPlugin(options: RssPluginOptions): Plugin {
2121
return {
2222
name: 'vite-plugin-rss',
23-
closeBundle() {
24-
generateRssFeed(options.hostname)
23+
async closeBundle() {
24+
await generateRssFeed(options.hostname)
2525
}
2626
}
2727
}
2828

29-
function generateRssFeed(hostname: string): void {
30-
const postsDirectory = path.join(process.cwd(), 'src', 'posts')
31-
const outDir = path.join(process.cwd(), 'dist')
32-
33-
// Read all markdown files
34-
const files = fs.readdirSync(postsDirectory).filter(file => file.endsWith('.md'))
35-
36-
const posts: RssItem[] = files.map(filename => {
37-
const filePath = path.join(postsDirectory, filename)
38-
const fileContents = fs.readFileSync(filePath, 'utf8')
39-
const { data } = matter(fileContents)
29+
async function getPostsFromFileSystem(hostname: string): Promise<Array<RssItem>> {
30+
try {
31+
const postsDir = path.resolve(__dirname, '../src/posts')
32+
const files = fs.readdirSync(postsDir).filter(file => file.endsWith('.md'))
4033

41-
const slug = filename.replace(/\.md$/, '')
34+
const posts = files.map(file => {
35+
const filePath = path.join(postsDir, file)
36+
const content = fs.readFileSync(filePath, 'utf-8')
37+
const { data } = matter(content)
38+
const slug = file.replace(/\.md$/, '')
39+
return {
40+
title: data.title || 'Untitled',
41+
link: `${hostname}/post/${slug}`,
42+
description: data.excerpt || data.description || '',
43+
pubDate: new Date(data.date).toUTCString(),
44+
guid: `${hostname}/post/${slug}`,
45+
categories: data.tags || []
46+
}
47+
})
4248

43-
return {
44-
title: data.title || 'Untitled',
45-
link: `${hostname}/post/${slug}`,
46-
description: data.excerpt || data.description || '',
47-
pubDate: new Date(data.date).toUTCString(),
48-
guid: `${hostname}/post/${slug}`,
49-
categories: data.tags || []
50-
}
51-
})
49+
return posts.sort((a, b) => new Date(b.pubDate).getTime() - new Date(a.pubDate).getTime())
50+
} catch (error) {
51+
console.error('Error reading posts:', error)
52+
return []
53+
}
54+
}
55+
56+
async function generateRssFeed(hostname: string): Promise<void> {
5257

53-
// Sort by date (newest first)
54-
posts.sort((a, b) => new Date(b.pubDate).getTime() - new Date(a.pubDate).getTime())
58+
const posts = await getPostsFromFileSystem(hostname)
59+
const outDir = path.join(process.cwd(), 'dist')
5560

5661
// Generate RSS XML
5762
const rssXml = `<?xml version="1.0" encoding="UTF-8"?>

plugins/vite-plugin-sitemap.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ interface SitemapPage {
1616
}
1717

1818
async function getPostsFromFileSystem(): Promise<Array<{ slug: string; frontmatter: any }>> {
19-
const postsDir = resolve(__dirname, 'src/posts')
20-
2119
try {
20+
const postsDir = resolve(__dirname, '../src/posts')
2221
const files = readdirSync(postsDir).filter(file => file.endsWith('.md'))
2322

2423
const posts = files.map(file => {

0 commit comments

Comments
 (0)