From 005fb33a8f4d008d1814ad35f88f8839bcf3e6c9 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Thu, 17 Mar 2022 09:54:03 +0800 Subject: [PATCH] =?UTF-8?q?rss=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rss.js | 33 +++++++++++++++++++++++++++++---- pages/feed.js | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/lib/rss.js b/lib/rss.js index 9c3f3fc7..d6cbaf4a 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -1,7 +1,31 @@ import { Feed } from 'feed' import BLOG from '@/blog.config' +import ReactDOMServer from 'react-dom/server' +import { NotionRenderer, Equation, Code, Collection, CollectionRow } from 'react-notion-x' +import { getPostBlocks } from './notion' -export function generateRss (posts) { +const mapPageUrl = id => 'https://www.notion.so/' + id.replace(/-/g, '') + +const createFeedContent = async post => { + const blockMap = await getPostBlocks(post.id, 'rss-content') + if (blockMap) { + const content = ReactDOMServer.renderToString() + const regexExp = /
.*?<\/svg>
.*?<\/div><\/div>
.*?<\/div><\/div><\/div><\/div>/g + return content.replace(regexExp, '') + } + return post.summary +} + +export async function generateRss (posts) { const year = new Date().getFullYear() const feed = new Feed({ title: BLOG.TITLE, @@ -17,14 +41,15 @@ export function generateRss (posts) { link: BLOG.LINK } }) - posts.forEach(post => { + for (const post of posts) { feed.addItem({ title: post.title, id: `${BLOG.LINK}/article/${post.slug}`, link: `${BLOG.LINK}/article/${post.slug}`, description: post.summary, + content: await createFeedContent(post), date: new Date(post?.date?.start_date || post.createdTime) }) - }) - return feed.rss2() + } + return feed.atom1() } diff --git a/pages/feed.js b/pages/feed.js index 3e3ebffa..e7499181 100644 --- a/pages/feed.js +++ b/pages/feed.js @@ -4,7 +4,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' export async function getServerSideProps ({ res }) { res.setHeader('Content-Type', 'text/xml') const globalNotionData = await getGlobalNotionData({ from: 'rss' }) - const xmlFeed = generateRss(globalNotionData?.allPosts?.slice(0, 10) || []) + const xmlFeed = await generateRss(globalNotionData?.allPosts?.slice(0, 10) || []) res.write(xmlFeed) res.end() return {