将LINK,NEXT_REVALIDATE_SECOND配置提升到NOTION_CONFIG中

This commit is contained in:
tangly1024.com
2024-04-11 18:05:32 +08:00
parent 69ff9ecb60
commit 6ce9def3fa
20 changed files with 160 additions and 57 deletions

View File

@@ -123,7 +123,7 @@ export const redirectUserLang = (lang, pageId) => {
getQueryVariable('locale') || getQueryVariable('locale') ||
getQueryVariable('lang') || getQueryVariable('lang') ||
window?.navigator?.language window?.navigator?.language
const siteIds = pageId.split(',') const siteIds = pageId?.split(',') || []
// 默认是进首页; 如果检测到有一个多语言匹配了用户浏览器,则自动跳转过去 // 默认是进首页; 如果检测到有一个多语言匹配了用户浏览器,则自动跳转过去
for (let index = 0; index < siteIds.length; index++) { for (let index = 0; index < siteIds.length; index++) {

View File

@@ -1,15 +1,16 @@
import fs from 'fs'
import { Feed } from 'feed'
import BLOG from '@/blog.config' import BLOG from '@/blog.config'
import ReactDOMServer from 'react-dom/server'
import { getPostBlocks } from '@/lib/db/getSiteData'
import NotionPage from '@/components/NotionPage' import NotionPage from '@/components/NotionPage'
import { getPostBlocks } from '@/lib/db/getSiteData'
import { Feed } from 'feed'
import fs from 'fs'
import ReactDOMServer from 'react-dom/server'
import { siteConfig } from './config'
/** /**
* 生成RSS内容 * 生成RSS内容
* @param {*} post * @param {*} post
* @returns * @returns
*/ */
const createFeedContent = async post => { const createFeedContent = async post => {
// 加密的文章内容只返回摘要 // 加密的文章内容只返回摘要
if (post.password && post.password !== '') { if (post.password && post.password !== '') {
@@ -20,30 +21,35 @@ const createFeedContent = async post => {
post.blockMap = blockMap post.blockMap = blockMap
const content = ReactDOMServer.renderToString(<NotionPage post={post} />) const content = ReactDOMServer.renderToString(<NotionPage post={post} />)
const regexExp = 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 /<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, '') return content.replace(regexExp, '')
} }
} }
export async function generateRss(posts) { export async function generateRss(NOTION_CONFIG, posts) {
const link = siteConfig('LINK', BLOG.LINK, NOTION_CONFIG)
const author = siteConfig('AUTHOR', BLOG.AUTHOR, NOTION_CONFIG)
const lang = siteConfig('LANG', BLOG.LANG, NOTION_CONFIG)
const subPath = siteConfig('SUB_PATH', BLOG.SUB_PATH, NOTION_CONFIG)
const year = new Date().getFullYear() const year = new Date().getFullYear()
const feed = new Feed({ const feed = new Feed({
title: BLOG.TITLE, title: BLOG.TITLE,
description: BLOG.DESCRIPTION, description: BLOG.DESCRIPTION,
link: `${BLOG.LINK}/${BLOG.SUB_PATH}`, link: `${link}/${subPath}`,
language: BLOG.LANG, language: lang,
favicon: `${BLOG.LINK}/favicon.png`, favicon: `${link}/favicon.png`,
copyright: `All rights reserved ${year}, ${BLOG.AUTHOR}`, copyright: `All rights reserved ${year}, ${author}`,
author: { author: {
name: BLOG.AUTHOR, name: author,
email: BLOG.CONTACT_EMAIL, email: BLOG.CONTACT_EMAIL,
link: BLOG.LINK link: link
} }
}) })
for (const post of posts) { for (const post of posts) {
feed.addItem({ feed.addItem({
title: post.title, title: post.title,
link: `${BLOG.LINK}/${post.slug}`, link: `${link}/${post.slug}`,
description: post.summary, description: post.summary,
content: await createFeedContent(post), content: await createFeedContent(post),
date: new Date(post?.publishDay) date: new Date(post?.publishDay)

View File

@@ -82,7 +82,14 @@ export async function getStaticProps({
// 无法获取文章 // 无法获取文章
if (!props?.post) { if (!props?.post) {
props.post = null props.post = null
return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } return {
props,
revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
}
} }
// 文章内容加载 // 文章内容加载
@@ -116,7 +123,11 @@ export async function getStaticProps({
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -66,7 +66,14 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
// 无法获取文章 // 无法获取文章
if (!props?.post) { if (!props?.post) {
props.post = null props.post = null
return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } return {
props,
revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
}
} }
// 文章内容加载 // 文章内容加载
@@ -100,7 +107,11 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }
function checkSlug(row) { function checkSlug(row) {

View File

@@ -107,7 +107,14 @@ export async function getStaticProps({ params: { prefix }, locale }) {
// 无法获取文章 // 无法获取文章
if (!props?.post) { if (!props?.post) {
props.post = null props.post = null
return { props, revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) } return {
props,
revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
}
} }
// 文章内容加载 // 文章内容加载
@@ -142,7 +149,11 @@ export async function getStaticProps({ params: { prefix }, locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -61,7 +61,11 @@ export async function getStaticProps({ locale }) {
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -46,7 +46,11 @@ export async function getStaticProps({ params: { category }, locale }) {
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -12,7 +12,10 @@ import { useRouter } from 'next/router'
export default function Category(props) { export default function Category(props) {
// 根据页面路径加载不同Layout文件 // 根据页面路径加载不同Layout文件
const Layout = getLayoutByTheme({ theme: siteConfig('THEME'), router: useRouter() }) const Layout = getLayoutByTheme({
theme: siteConfig('THEME'),
router: useRouter()
})
return <Layout {...props} /> return <Layout {...props} />
} }
@@ -28,7 +31,10 @@ export async function getStaticProps({ params: { category, page } }) {
// 处理文章页数 // 处理文章页数
props.postCount = props.posts.length props.postCount = props.posts.length
// 处理分页 // 处理分页
props.posts = props.posts.slice(siteConfig('POSTS_PER_PAGE') * (page - 1), siteConfig('POSTS_PER_PAGE') * page) props.posts = props.posts.slice(
siteConfig('POSTS_PER_PAGE') * (page - 1),
siteConfig('POSTS_PER_PAGE') * page
)
delete props.allPages delete props.allPages
props.page = page props.page = page
@@ -37,7 +43,11 @@ export async function getStaticProps({ params: { category, page } }) {
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }
@@ -50,7 +60,9 @@ export async function getStaticPaths() {
// 过滤状态类型 // 过滤状态类型
const categoryPosts = allPages const categoryPosts = allPages
?.filter(page => page.type === 'Post' && page.status === 'Published') ?.filter(page => page.type === 'Post' && page.status === 'Published')
.filter(post => post && post.category && post.category.includes(category.name)) .filter(
post => post && post.category && post.category.includes(category.name)
)
// 处理文章页数 // 处理文章页数
const postCount = categoryPosts.length const postCount = categoryPosts.length
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE')) const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))

View File

@@ -24,6 +24,10 @@ export async function getStaticProps({ locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -59,7 +59,7 @@ export async function getStaticProps(req) {
generateRobotsTxt() generateRobotsTxt()
// 生成Feed订阅 // 生成Feed订阅
if (JSON.parse(BLOG.ENABLE_RSS)) { if (JSON.parse(BLOG.ENABLE_RSS)) {
generateRss(props?.latestPosts || []) generateRss(props?.NOTION_CONFIG, props?.latestPosts || [])
} }
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build' // 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
@@ -68,7 +68,11 @@ export async function getStaticProps(req) {
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -64,7 +64,11 @@ export async function getStaticProps({ params: { page } }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -40,7 +40,11 @@ export async function getStaticProps({ params: { keyword }, locale }) {
props.keyword = keyword props.keyword = keyword
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -44,7 +44,11 @@ export async function getStaticProps({ params: { keyword, page }, locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -54,7 +54,11 @@ export async function getStaticProps({ locale }) {
) )
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -27,7 +27,11 @@ export async function getStaticProps(req) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -27,7 +27,11 @@ export async function getStaticProps(req) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -1,5 +1,6 @@
// pages/sitemap.xml.js // pages/sitemap.xml.js
import BLOG from '@/blog.config' import BLOG from '@/blog.config'
import { siteConfig } from '@/lib/config'
import { getNotionPageData } from '@/lib/db/getSiteData' import { getNotionPageData } from '@/lib/db/getSiteData'
import { extractLangId, extractLangPrefix } from '@/lib/utils/pageId' import { extractLangId, extractLangPrefix } from '@/lib/utils/pageId'
import { getServerSideSitemap } from 'next-sitemap' import { getServerSideSitemap } from 'next-sitemap'
@@ -12,13 +13,12 @@ export const getServerSideProps = async ctx => {
const id = extractLangId(siteId) const id = extractLangId(siteId)
const locale = extractLangPrefix(siteId) const locale = extractLangPrefix(siteId)
// 第一个id站点默认语言 // 第一个id站点默认语言
const localeFields = generateLocalesSitemap( const siteData = await getNotionPageData({
await getNotionPageData({ pageId: id,
pageId: id, from: 'sitemap.xml'
from: 'sitemap.xml' })
}).allPages, const link = siteConfig('LINK', BLOG.LINK, siteData.NOTION_CONFIG)
locale const localeFields = generateLocalesSitemap(link, siteData.allPages, locale)
)
fields = fields.concat(localeFields) fields = fields.concat(localeFields)
} }
@@ -31,43 +31,43 @@ export const getServerSideProps = async ctx => {
return getServerSideSitemap(ctx, fields) return getServerSideSitemap(ctx, fields)
} }
function generateLocalesSitemap(allPages, locale) { function generateLocalesSitemap(link, allPages, locale) {
if (locale && locale.length > 0 && locale.indexOf('/') !== 0) { if (locale && locale.length > 0 && locale.indexOf('/') !== 0) {
locale = '/' + locale locale = '/' + locale
} }
const defaultFields = [ const defaultFields = [
{ {
loc: `${BLOG.LINK}${locale}`, loc: `${link}${locale}`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
}, },
{ {
loc: `${BLOG.LINK}${locale}/archive`, loc: `${link}${locale}/archive`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
}, },
{ {
loc: `${BLOG.LINK}${locale}/category`, loc: `${link}${locale}/category`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
}, },
{ {
loc: `${BLOG.LINK}${locale}/feed`, loc: `${link}${locale}/feed`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
}, },
{ {
loc: `${BLOG.LINK}${locale}/search`, loc: `${link}${locale}/search`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
}, },
{ {
loc: `${BLOG.LINK}${locale}/tag`, loc: `${link}${locale}/tag`,
lastmod: new Date().toISOString().split('T')[0], lastmod: new Date().toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'
@@ -81,7 +81,7 @@ function generateLocalesSitemap(allPages, locale) {
? post?.slug?.slice(1) ? post?.slug?.slice(1)
: post.slug : post.slug
return { return {
loc: `${BLOG.LINK}${locale}/${slugWithoutLeadingSlash}`, loc: `${link}${locale}/${slugWithoutLeadingSlash}`,
lastmod: new Date(post?.publishDay).toISOString().split('T')[0], lastmod: new Date(post?.publishDay).toISOString().split('T')[0],
changefreq: 'daily', changefreq: 'daily',
priority: '0.7' priority: '0.7'

View File

@@ -42,7 +42,11 @@ export async function getStaticProps({ params: { tag }, locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -33,7 +33,11 @@ export async function getStaticProps({ params: { tag, page }, locale }) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }

View File

@@ -26,7 +26,11 @@ export async function getStaticProps(req) {
delete props.allPages delete props.allPages
return { return {
props, props,
revalidate: parseInt(BLOG.NEXT_REVALIDATE_SECOND) revalidate: siteConfig(
'REVALIDATE_SECOND',
BLOG.NEXT_REVALIDATE_SECOND,
props.NOTION_CONFIG
)
} }
} }