FMHYedit/.vitepress/hooks/rss.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-01-25 16:32:45 +00:00
import path from 'node:path'
import { writeFileSync } from 'node:fs'
import { Feed } from 'feed'
import {
createContentLoader,
type ContentData,
type SiteConfig
} from 'vitepress'
import consola from 'consola'
import { meta } from '../constants'
2023-12-31 11:15:48 +00:00
export async function generateFeed(config: SiteConfig): Promise<void> {
const feed: Feed = new Feed({
id: meta.hostname,
link: meta.hostname,
title: `FMHY blog`,
description: meta.description,
2024-01-25 16:32:45 +00:00
language: 'en-US',
image: 'https://github.com/fmhy.png',
2023-12-31 11:15:48 +00:00
favicon: `${meta.hostname}/favicon.ico`,
2024-01-25 16:32:45 +00:00
copyright: `Copyright (c) 2023-present FMHY`
})
2023-12-31 11:15:48 +00:00
2024-01-25 16:32:45 +00:00
const posts: ContentData[] = await createContentLoader('posts/*.md', {
2023-12-31 11:15:48 +00:00
excerpt: true,
render: true,
transform: (rawData) => {
return rawData.sort((a, b) => {
2024-01-25 16:32:45 +00:00
return (
Number(new Date(b.frontmatter.date)) -
Number(new Date(a.frontmatter.date))
)
})
}
}).load()
2023-12-31 11:15:48 +00:00
for (const { url, frontmatter, html } of posts) {
feed.addItem({
title: frontmatter.title as string,
2024-01-25 16:32:45 +00:00
id: `${meta.hostname}${url.replace(/\/\d+\./, '/')}`,
link: `${meta.hostname}${url.replace(/\/\d+\./, '/')}`,
2023-12-31 11:15:48 +00:00
date: frontmatter.date,
2024-01-25 16:32:45 +00:00
content: html!
})
2023-12-31 11:15:48 +00:00
}
2024-01-25 16:32:45 +00:00
writeFileSync(path.join(config.outDir, 'feed.rss'), feed.rss2())
return consola.info('Generated rss feed.')
2023-12-31 11:15:48 +00:00
}