mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
58 lines
2.0 KiB
JavaScript
58 lines
2.0 KiB
JavaScript
import fs from 'fs'
|
|
import { Feed } from 'feed'
|
|
import BLOG from '@/blog.config'
|
|
import ReactDOMServer from 'react-dom/server'
|
|
import { getPostBlocks } from './notion'
|
|
import NotionPage from '@/components/NotionPage'
|
|
|
|
export async function generateRss(posts) {
|
|
const year = new Date().getFullYear()
|
|
const feed = new Feed({
|
|
title: BLOG.TITLE,
|
|
description: BLOG.DESCRIPTION,
|
|
link: `${BLOG.LINK}/${BLOG.SUB_PATH}`,
|
|
language: BLOG.LANG,
|
|
favicon: `${BLOG.LINK}/favicon.png`,
|
|
copyright: `All rights reserved ${year}, ${BLOG.AUTHOR}`,
|
|
author: {
|
|
name: BLOG.AUTHOR,
|
|
email: BLOG.CONTACT_EMAIL,
|
|
link: BLOG.LINK
|
|
}
|
|
})
|
|
for (const post of posts) {
|
|
feed.addItem({
|
|
title: post.title,
|
|
link: `${BLOG.LINK}/${post.slug}`,
|
|
description: post.summary,
|
|
content: await createFeedContent(post),
|
|
date: new Date(post?.date?.start_date || post?.createdTime)
|
|
})
|
|
}
|
|
|
|
fs.mkdirSync('./public/rss', { recursive: true })
|
|
fs.writeFileSync('./public/rss/feed.xml', feed.rss2())
|
|
fs.writeFileSync('./public/rss/atom.xml', feed.atom1())
|
|
fs.writeFileSync('./public/rss/feed.json', feed.json1())
|
|
}
|
|
|
|
/**
|
|
* 生成RSS内容
|
|
* @param {*} post
|
|
* @returns
|
|
*/
|
|
const createFeedContent = async post => {
|
|
// 加密的文章内容只返回摘要
|
|
if (post.password && post.password !== '') {
|
|
return post.summary
|
|
}
|
|
const blockMap = await getPostBlocks(post.id, 'rss-content')
|
|
if (blockMap) {
|
|
post.blockMap = blockMap
|
|
const content = ReactDOMServer.renderToString(<NotionPage post={post} />)
|
|
const regexExp =
|
|
/<div class="notion-collection-row"><div class="notion-collection-row-body"><div class="notion-collection-row-property"><div class="notion-collection-column-title"><svg.*?class="notion-collection-column-title-icon">.*?<\/svg><div class="notion-collection-column-title-body">.*?<\/div><\/div><div class="notion-collection-row-value">.*?<\/div><\/div><\/div><\/div>/g
|
|
return content.replace(regexExp, '')
|
|
}
|
|
}
|