feat: RSS feed
This commit is contained in:
+53
-12
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs'
|
||||
import process from 'process'
|
||||
import { Feed } from 'feed'
|
||||
|
||||
process.loadEnvFile()
|
||||
|
||||
@@ -28,6 +29,10 @@ function readArticleMetadata(path: string): Record<string, string> | null {
|
||||
const metadata: Record<string, string> = {
|
||||
path: path.replaceAll('/index.md', ''),
|
||||
}
|
||||
const dateMatch = path.match(/(\d{4}\/\d{2}\/\d{2})/)
|
||||
if (dateMatch && dateMatch[1]) {
|
||||
metadata['date'] = dateMatch[1].replaceAll('/', '-')
|
||||
}
|
||||
do {
|
||||
subMatch = METADATA_REGEX.exec(match[1])
|
||||
if (subMatch && subMatch[1] && subMatch[2]) {
|
||||
@@ -69,6 +74,18 @@ function formatArticlePage(metadata: Record<string, string>, baseHtml: string):
|
||||
return outHtml
|
||||
}
|
||||
|
||||
function addFeedArticle(metadata: Record<string, string>, feed: Feed) {
|
||||
if (metadata.draft !== 'true') {
|
||||
feed.addItem({
|
||||
title: metadata.title.replace(/(<([^>]+)>)/gi, '').trim(),
|
||||
id: metadata.path,
|
||||
link: `${process.env.VITE_BASE_URL}${metadata.path}/`,
|
||||
date: new Date(Date.parse(metadata.date)),
|
||||
image: metadata.thumbnail.replace('./', process.env.VITE_BASE_URL + metadata.path + '/'),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const indexContent = fs.readFileSync('dist/index.html', { encoding: 'utf8' })
|
||||
|
||||
if (!indexContent) {
|
||||
@@ -76,15 +93,39 @@ if (!indexContent) {
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
getFiles('articles')
|
||||
.filter((path) => path.match(/index.md$/))
|
||||
.forEach((path) => {
|
||||
const metadata = readArticleMetadata(path)
|
||||
if (metadata) {
|
||||
fs.writeFileSync(
|
||||
`dist/${metadata.path}/index.html`,
|
||||
formatArticlePage(metadata, indexContent),
|
||||
)
|
||||
console.info(`Wrote dist/${metadata.path}/index.html`)
|
||||
}
|
||||
})
|
||||
const metadatas = getFiles('articles')
|
||||
.filter((path) => path.match(/\d{4}\/\d{2}\/\d{2}\/index.md$/))
|
||||
.map((path) => readArticleMetadata(path))
|
||||
.filter((metadata) => !!metadata)
|
||||
|
||||
const feed = new Feed({
|
||||
title: process.env.VITE_APP_TITLE?.replace(/(<([^>]+)>)/gi, '').trim() ?? '',
|
||||
id: process.env.VITE_BASE_URL,
|
||||
link: process.env.VITE_BASE_URL,
|
||||
language: process.env.VITE_APP_LANG,
|
||||
favicon: process.env.VITE_BASE_URL + '/articles/favicon.ico',
|
||||
generator: 'md-blog',
|
||||
feedLinks: {
|
||||
json: process.env.VITE_BASE_URL + 'feed.json',
|
||||
atom: process.env.VITE_BASE_URL + 'atom.xml',
|
||||
rss: process.env.VITE_BASE_URL + 'rss',
|
||||
},
|
||||
updated: new Date(
|
||||
Math.max(
|
||||
0,
|
||||
...metadatas
|
||||
.filter((metadata) => metadata.draft !== 'true')
|
||||
.map((metadata) => Date.parse(metadata.date)),
|
||||
),
|
||||
),
|
||||
})
|
||||
|
||||
metadatas.forEach((metadata) => {
|
||||
fs.writeFileSync(`dist/${metadata.path}/index.html`, formatArticlePage(metadata, indexContent))
|
||||
console.info(`Wrote dist/${metadata.path}/index.html`)
|
||||
addFeedArticle(metadata, feed)
|
||||
})
|
||||
|
||||
fs.writeFileSync('dist/feed.json', feed.json1())
|
||||
fs.writeFileSync('dist/atom.xml', feed.atom1())
|
||||
fs.writeFileSync('dist/rss', feed.rss2())
|
||||
|
||||
Reference in New Issue
Block a user