From 15835c4a6459b74b2abbac50cb03accd46f55bf5 Mon Sep 17 00:00:00 2001 From: "@andatoshiki" Date: Sun, 17 Sep 2023 02:34:13 -0700 Subject: [PATCH] feat(feed): add site feed generation plugin for rss subscription support --- docs/.vitepress/plugins/genFeed.ts | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 docs/.vitepress/plugins/genFeed.ts diff --git a/docs/.vitepress/plugins/genFeed.ts b/docs/.vitepress/plugins/genFeed.ts new file mode 100644 index 00000000..d25060dc --- /dev/null +++ b/docs/.vitepress/plugins/genFeed.ts @@ -0,0 +1,56 @@ +import path from 'node:path' +import { writeFileSync } from 'node:fs' +import { Feed } from 'feed' +import { type SiteConfig, createContentLoader } from 'vitepress' +import { site as baseUrl, description, name } from '../config/meta' + +function reName(name: string) { + if (!name) name = 'Anda Toshiki' + return name === 'Anda Toshiki' ? 'andatoshiki' : name +} + +function getGithubLink(name: string) { + return `https://github.com/${reName(name)}` +} + +export async function genFeed(config: SiteConfig) { + const feed = new Feed({ + title: name, + description, + id: baseUrl, + link: baseUrl, + language: 'en-US', + image: 'https://note.toshiki.dev/logo-308px.png', + favicon: `${baseUrl}/favicon.ico`, + copyright: 'Copyright (c) 2023-present, Anda Toshiki at Toshiki Dev', + }) + + const posts = await createContentLoader('**/*.md', { + excerpt: true, + render: true, + }).load() + + posts.sort((a, b) => +new Date(b.frontmatter?.date as string) - +new Date(a.frontmatter?.date as string)) + + for (const { url, frontmatter, html } of posts) { + let postTitle = 'No Title' + postTitle = html?.match(/

(.*?)/)?.[2] || postTitle + feed.addItem({ + title: frontmatter?.title || postTitle, + id: `${baseUrl}${url.slice(1)}`, + link: `${baseUrl}${url.slice(1)}`, + guid: `${baseUrl}${url.slice(1)}`, + description: html, + content: html, + author: [ + { + name: frontmatter?.author || 'Anda Toshiki', + link: frontmatter?.author ? getGithubLink(frontmatter?.author) : undefined, + }, + ], + date: frontmatter?.date || new Date('2021-07-01'), + }) + } + + writeFileSync(path.join(config.outDir, 'feed.xml'), feed.rss2()) +}