From 9d7c158e82110e9c96a48c8ccd52872b6f1231f0 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sun, 12 Mar 2023 11:54:44 +0800 Subject: [PATCH] =?UTF-8?q?RSS=20=E8=B0=83=E6=95=B4=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=BC=80=E5=85=B3=E3=80=81=E9=9A=90=E8=97=8Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 3 +++ lib/rss.js | 36 +++++++++++++++++-------------- pages/index.js | 6 ++++-- themes/nobelium/components/Nav.js | 2 +- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/blog.config.js b/blog.config.js index 2449cc31..14dcc2da 100644 --- a/blog.config.js +++ b/blog.config.js @@ -214,6 +214,9 @@ const BLOG = { icon: process.env.NEXT_PUBLIC_NOTION_PROPERTY_ICON || 'icon' }, + // RSS + ENABLE_RSS: process.env.NEXT_PUBLIC_ENABLE_RSS || true, // 是否开启RSS订阅功能 + // 作废配置 AVATAR: process.env.NEXT_PUBLIC_AVATAR || '/avatar.png', // 作者头像,被notion中的ICON覆盖。若无ICON则取public目录下的avatar.png TITLE: process.env.NEXT_PUBLIC_TITLE || 'NotionNext BLOG', // 站点标题 ,被notion中的页面标题覆盖 diff --git a/lib/rss.js b/lib/rss.js index 68214659..66723f93 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -5,21 +5,6 @@ import ReactDOMServer from 'react-dom/server' import { getPostBlocks } from './notion' import NotionPage from '@/components/NotionPage' -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() - const regexExp = - /
.*?<\/svg>
.*?<\/div><\/div>
.*?<\/div><\/div><\/div><\/div>/g - return content.replace(regexExp, '') - } -} - export async function generateRss(posts) { const year = new Date().getFullYear() const feed = new Feed({ @@ -38,7 +23,6 @@ export async function generateRss(posts) { for (const post of posts) { feed.addItem({ title: post.title, - guid: `${post.id}`, link: `${BLOG.LINK}/${post.slug}`, description: post.summary, content: await createFeedContent(post), @@ -51,3 +35,23 @@ export async function generateRss(posts) { 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() + const regexExp = + /
.*?<\/svg>
.*?<\/div><\/div>
.*?<\/div><\/div><\/div><\/div>/g + return content.replace(regexExp, '') + } +} diff --git a/pages/index.js b/pages/index.js index a26d57bd..087a37bc 100644 --- a/pages/index.js +++ b/pages/index.js @@ -43,8 +43,10 @@ export async function getStaticProps() { } } - // 异步生成Feed订阅 - generateRss(props?.latestPosts || []) + // 生成Feed订阅 + if (JSON.parse(BLOG.ENABLE_RSS)) { + generateRss(props?.latestPosts || []) + } return { props: { diff --git a/themes/nobelium/components/Nav.js b/themes/nobelium/components/Nav.js index 6c6951ea..a0594ebf 100644 --- a/themes/nobelium/components/Nav.js +++ b/themes/nobelium/components/Nav.js @@ -75,7 +75,7 @@ const NavBar = props => { const { locale } = useGlobal() let links = [ - { id: 2, name: locale.NAV.RSS, to: '/feed', show: CONFIG_NOBELIUM.MENU_RSS, target: '_blank' }, + { id: 2, name: locale.NAV.RSS, to: '/feed', show: BLOG.ENABLE_RSS && CONFIG_NOBELIUM.MENU_RSS, target: '_blank' }, { icon: 'fas fa-search', name: locale.NAV.SEARCH, to: '/search', show: CONFIG_NOBELIUM.MENU_SEARCH }, { icon: 'fas fa-archive', name: locale.NAV.ARCHIVE, to: '/archive', show: CONFIG_NOBELIUM.MENU_ARCHIVE }, { icon: 'fas fa-folder', name: locale.COMMON.CATEGORY, to: '/category', show: CONFIG_NOBELIUM.MENU_CATEGORY },