@@ -20,38 +20,43 @@ interface RssPluginOptions {
2020export 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 ( / \. m d $ / , '' )
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 ( / \. m d $ / , '' )
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"?>
0 commit comments