Skip to content

Commit

Permalink
✨ feat: add rss support (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo authored Nov 5, 2024
1 parent 42e3fc0 commit 1de0d27
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@tailwindcss/typography": "^0.5.15",
"@vite-pwa/vitepress": "^0.5.3",
"feed": "^4.2.2",
"tailwindcss": "^3.4.14",
"tsx": "^4.19.2",
"vite-plugin-pwa": "^0.20.5",
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfigWithTheme, createContentLoader } from 'vitepress'
import { defineConfigWithTheme } from 'vitepress'
import { withPwa } from '@vite-pwa/vitepress'
import { genFeed } from './genFeed.js'

interface ThemeConfig {
postsPerPage?: number
Expand Down Expand Up @@ -114,5 +115,6 @@ export default withPwa(
},
},
themeConfig: themeConfig,
buildEnd: genFeed,
})
)
55 changes: 55 additions & 0 deletions src/.vitepress/genFeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import path from 'path'
import { writeFileSync } from 'fs'
import { Feed } from 'feed'
import { createContentLoader, type SiteConfig } from 'vitepress'

const baseUrl = `https://pfcc.blog`

export async function genFeed(config: SiteConfig) {
const feed = new Feed({
title: '飞桨开源社区博客',
description: 'Wonderful stories from PaddlePaddle contributors',
id: baseUrl,
link: baseUrl,
language: 'zh-CN',
image: 'https://pfcc.blog/logo.png',
favicon: `${baseUrl}/favicon.ico`,
copyright: 'Copyright (c) 2023-present, PaddlePaddle contributors',
})

const posts = await createContentLoader('posts/*.md', {
excerpt: true,
render: true,
}).load()

posts.sort(
(a, b) => +new Date(b.frontmatter.date as string) - +new Date(a.frontmatter.date as string)
)

const formatAuthor = (author: {
name: string
github?: string
}): { name: string; link?: string } => {
return {
name: author.name,
link: author.github ? `https://github.com/${author.github}` : undefined,
}
}

for (const { url, excerpt, frontmatter, html } of posts) {
feed.addItem({
title: frontmatter.title,
id: `${baseUrl}${url}`,
link: `${baseUrl}${url}`,
description: excerpt,
content: html?.replaceAll('​', ''),
author: [
formatAuthor(frontmatter.author),
...(frontmatter.co_authors?.map(formatAuthor) ?? []),
],
date: new Date(frontmatter.date as string),
})
}

writeFileSync(path.join(config.outDir, 'feed.rss'), feed.rss2())
}

0 comments on commit 1de0d27

Please sign in to comment.