mirror of
https://github.com/andatoshiki/toshiki-notebook.git
synced 2026-06-06 00:46:29 +00:00
feat(feed): add site feed generation plugin for rss subscription support
This commit is contained in:
parent
f5eef5b904
commit
15835c4a64
56
docs/.vitepress/plugins/genFeed.ts
Normal file
56
docs/.vitepress/plugins/genFeed.ts
Normal file
@ -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(/<h1 id=(.*)>(.*?)<a .*?>/)?.[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())
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user