Skip to content

Commit

Permalink
修复部分NOTION_CONFIG读取问题
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 15, 2024
1 parent 70e5364 commit 517a967
Show file tree
Hide file tree
Showing 44 changed files with 903 additions and 557 deletions.
43 changes: 38 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,38 @@ import { deepClone } from './utils'
* @param {*} extendConfig ; 参考配置对象{key:val},如果notion中找不到优先尝试在这里面查找
* @returns
*/
export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
let global = null
export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
if (!key) {
return null
}

// 特殊配置处理;某些配置只在服务端生效;而Global的NOTION_CONFIG仅限前端组件使用,因此需要从extendConfig中读取
switch (key) {
case 'NEXT_REVALIDATE_SECOND':
case 'POST_RECOMMEND_COUNT':
case 'IMAGE_COMPRESS_WIDTH':
case 'PSEUDO_STATIC':
case 'POSTS_SORT_BY':
case 'POSTS_PER_PAGE':
case 'POST_PREVIEW_LINES':
case 'POST_URL_PREFIX':
case 'POST_LIST_STYLE':
case 'POST_LIST_PREVIEW':
case 'POST_URL_PREFIX_MAPPING_CATEGORY':
return convertVal(extendConfig[key] || defaultVal || BLOG[key])
default:
}

let global = {}
try {
const isClient = typeof window !== 'undefined'
// const isClient = typeof window !== 'undefined'
// eslint-disable-next-line react-hooks/rules-of-hooks
global = isClient ? useGlobal() : {}
global = useGlobal()
// eslint-disable-next-line react-hooks/rules-of-hooks
// global = useGlobal()
} catch (error) {}
} catch (error) {
console.warn('SiteConfig警告', key, error)
}

// 首先 配置最优先读取NOTION中的表格配置
let val = null
Expand Down Expand Up @@ -66,6 +89,16 @@ export const siteConfig = (key, defaultVal = null, extendConfig = null) => {
}

// 从Notion_CONFIG读取的配置通常都是字符串,适当转义
return convertVal(val)
}

/**
* 配置默认都是string类型;
* 识别配置的值是否数字、布尔、[]数组,若是则转成对应类型
* @param {*} val
* @returns
*/
export const convertVal = val => {
if (typeof val === 'string') {
// 解析布尔
if (val === 'true' || val === 'false') {
Expand Down
13 changes: 7 additions & 6 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,13 @@ function getCategoryOptions(schema) {
* @param from
* @returns {Promise<{title,description,pageCover,icon}>}
*/
function getSiteInfo({ collection, block, NOTION_CONFIG, pageId }) {
const defaultTitle = siteConfig('TITLE', '', NOTION_CONFIG)
const defaultDescription = siteConfig('DESCRIPTION', '', NOTION_CONFIG)
const defaultPageCover = siteConfig('HOME_BANNER_IMAGE', '', NOTION_CONFIG)
const defaultIcon = siteConfig('AVATAR', '', NOTION_CONFIG)
const defaultLink = siteConfig('LINK', '', NOTION_CONFIG)
function getSiteInfo({ collection, block, NOTION_CONFIG }) {
const defaultTitle = NOTION_CONFIG?.TITLE || BLOG.TITLE
const defaultDescription = NOTION_CONFIG?.DESCRIPTION || BLOG.DESCRIPTION
const defaultPageCover =
NOTION_CONFIG?.HOME_BANNER_IMAGE || BLOG.HOME_BANNER_IMAGE
const defaultIcon = NOTION_CONFIG?.AVATAR || BLOG.AVATAR
const defaultLink = NOTION_CONFIG?.LINK || BLOG.LINK
if (!collection && !block) {
return {
title: defaultTitle,
Expand Down
2 changes: 1 addition & 1 deletion lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
}

// 开启伪静态路径
if (siteConfig('PSEUDO_STATIC', false, NOTION_CONFIG)) {
if (JSON.parse(NOTION_CONFIG?.PSEUDO_STATIC || BLOG.PSEUDO_STATIC)) {
if (
!properties?.href?.endsWith('.html') &&
!properties?.href?.startsWith('http')
Expand Down
6 changes: 3 additions & 3 deletions lib/robots.txt.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import { siteConfig } from './config'

export async function generateRobotsTxt(NOTION_CONFIG) {
const LINK = siteConfig('LINK', '', NOTION_CONFIG)
export async function generateRobotsTxt(props) {
const { siteInfo } = props
const LINK = siteInfo?.link
const content = `
# *
User-agent: *
Expand Down
39 changes: 21 additions & 18 deletions lib/rss.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import BLOG from '@/blog.config'
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内容
Expand All @@ -25,30 +25,33 @@ const createFeedContent = async post => {
}
}

export async function generateRss(NOTION_CONFIG, posts) {
const link = siteConfig('LINK', '', NOTION_CONFIG)
const author = siteConfig('AUTHOR', '', NOTION_CONFIG)
const lang = siteConfig('LANG', '', NOTION_CONFIG)
const subPath = siteConfig('SUB_PATH', '', NOTION_CONFIG)

export async function generateRss(props) {
const { NOTION_CONFIG, siteInfo, latestPosts } = props
const TITLE = siteInfo?.title
const DESCRIPTION = siteInfo?.description
const LINK = siteInfo?.link
const AUTHOR = NOTION_CONFIG?.AUTHOR || BLOG.AUTHOR
const LANG = NOTION_CONFIG?.LANG || BLOG.LANG
const SUB_PATH = NOTION_CONFIG?.SUB_PATH || BLOG.SUB_PATH
const CONTACT_EMAIL = NOTION_CONFIG?.CONTACT_EMAIL || BLOG.CONTACT_EMAIL
const year = new Date().getFullYear()
const feed = new Feed({
title: siteConfig('TITLE', '', NOTION_CONFIG),
description: siteConfig('DESCRIPTION', '', NOTION_CONFIG),
link: `${link}/${subPath}`,
language: lang,
favicon: `${link}/favicon.png`,
copyright: `All rights reserved ${year}, ${author}`,
title: TITLE,
description: DESCRIPTION,
link: `${LINK}/${SUB_PATH}`,
language: LANG,
favicon: `${LINK}/favicon.png`,
copyright: `All rights reserved ${year}, ${AUTHOR}`,
author: {
name: author,
email: siteConfig('CONTACT_EMAIL', '', NOTION_CONFIG),
link: link
name: AUTHOR,
email: CONTACT_EMAIL,
link: LINK
}
})
for (const post of posts) {
for (const post of latestPosts) {
feed.addItem({
title: post.title,
link: `${link}/${post.slug}`,
link: `${LINK}/${post.slug}`,
description: post.summary,
content: await createFeedContent(post),
date: new Date(post?.publishDay)
Expand Down
5 changes: 4 additions & 1 deletion pages/category/[category]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export async function getStaticProps({ params: { category }, locale }) {
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
// 滚动列表 给前端返回所有数据
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
props.posts = props.posts?.slice(
0,
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
)
}

delete props.allPages
Expand Down
13 changes: 9 additions & 4 deletions pages/category/[category]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export async function getStaticProps({ params: { category, page } }) {
.filter(post => post && post.category && post.category.includes(category))
// 处理文章页数
props.postCount = props.posts.length
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
// 处理分页
props.posts = props.posts.slice(
siteConfig('POSTS_PER_PAGE') * (page - 1),
siteConfig('POSTS_PER_PAGE') * page
POSTS_PER_PAGE * (page - 1),
POSTS_PER_PAGE * page
)

delete props.allPages
Expand All @@ -53,7 +54,9 @@ export async function getStaticProps({ params: { category, page } }) {

export async function getStaticPaths() {
const from = 'category-paths'
const { categoryOptions, allPages } = await getGlobalData({ from })
const { categoryOptions, allPages, NOTION_CONFIG } = await getGlobalData({
from
})
const paths = []

categoryOptions?.forEach(category => {
Expand All @@ -65,7 +68,9 @@ export async function getStaticPaths() {
)
// 处理文章页数
const postCount = categoryPosts.length
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
paths.push({ params: { category: category.name, page: '' + i } })
Expand Down
23 changes: 13 additions & 10 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export async function getStaticProps(req) {
const { locale } = req
const from = 'index'
const props = await getGlobalData({ from, locale })

const POST_PREVIEW_LINES = siteConfig(
'POST_PREVIEW_LINES',
12,
props?.NOTION_CONFIG
)
props.posts = props.allPages?.filter(
page => page.type === 'Post' && page.status === 'Published'
)
Expand All @@ -37,29 +41,28 @@ export async function getStaticProps(req) {
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
// 滚动列表默认给前端返回所有数据
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
props.posts = props.posts?.slice(
0,
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
)
}

// 预览文章内容
if (siteConfig('POST_LIST_PREVIEW')) {
if (siteConfig('POST_LIST_PREVIEW', false, props?.NOTION_CONFIG)) {
for (const i in props.posts) {
const post = props.posts[i]
if (post.password && post.password !== '') {
continue
}
post.blockMap = await getPostBlocks(
post.id,
'slug',
siteConfig('POST_PREVIEW_LINES')
)
post.blockMap = await getPostBlocks(post.id, 'slug', POST_PREVIEW_LINES)
}
}

// 生成robotTxt
generateRobotsTxt(props?.NOTION_CONFIG)
generateRobotsTxt(props)
// 生成Feed订阅
if (JSON.parse(BLOG.ENABLE_RSS)) {
generateRss(props?.NOTION_CONFIG, props?.latestPosts || [])
generateRss(props)
}

// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
Expand Down
25 changes: 15 additions & 10 deletions pages/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const Page = props => {

export async function getStaticPaths({ locale }) {
const from = 'page-paths'
const { postCount } = await getGlobalData({ from, locale })
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
const { postCount, NOTION_CONFIG } = await getGlobalData({ from, locale })
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
)
return {
// remove first page, we 're not gonna handle that.
paths: Array.from({ length: totalPages - 1 }, (_, i) => ({
Expand All @@ -36,28 +38,31 @@ export async function getStaticProps({ params: { page } }) {
const from = `page-${page}`
const props = await getGlobalData({ from })
const { allPages } = props
const POST_PREVIEW_LINES = siteConfig(
'POST_PREVIEW_LINES',
12,
props?.NOTION_CONFIG
)

const allPosts = allPages?.filter(
page => page.type === 'Post' && page.status === 'Published'
)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
// 处理分页
props.posts = allPosts.slice(
siteConfig('POSTS_PER_PAGE') * (page - 1),
siteConfig('POSTS_PER_PAGE') * page
POSTS_PER_PAGE * (page - 1),
POSTS_PER_PAGE * page
)
props.page = page

// 处理预览
if (siteConfig('POST_LIST_PREVIEW')) {
if (siteConfig('POST_LIST_PREVIEW', false, props?.NOTION_CONFIG)) {
for (const i in props.posts) {
const post = props.posts[i]
if (post.password && post.password !== '') {
continue
}
post.blockMap = await getPostBlocks(
post.id,
'slug',
siteConfig('POST_PREVIEW_LINES')
)
post.blockMap = await getPostBlocks(post.id, 'slug', POST_PREVIEW_LINES)
}
}

Expand Down
15 changes: 11 additions & 4 deletions pages/search/[keyword]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ export async function getStaticProps({ params: { keyword }, locale }) {
)
props.posts = await filterByMemCache(allPosts, keyword)
props.postCount = props.posts.length
const POST_LIST_STYLE = siteConfig(
'POST_LIST_STYLE',
'Page',
props?.NOTION_CONFIG
)
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)

// 处理分页
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
// 滚动列表 给前端返回所有数据
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
if (POST_LIST_STYLE === 'scroll') {
// 滚动列表默认给前端返回所有数据
} else if (POST_LIST_STYLE) {
props.posts = props.posts?.slice(0, POSTS_PER_PAGE)
}
props.keyword = keyword
return {
Expand Down
5 changes: 3 additions & 2 deletions pages/search/[keyword]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ export async function getStaticProps({ params: { keyword, page }, locale }) {
)
props.posts = await filterByMemCache(allPosts, keyword)
props.postCount = props.posts.length
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
// 处理分页
props.posts = props.posts.slice(
siteConfig('POSTS_PER_PAGE') * (page - 1),
siteConfig('POSTS_PER_PAGE') * page
POSTS_PER_PAGE * (page - 1),
POSTS_PER_PAGE * page
)
props.keyword = keyword
props.page = page
Expand Down
5 changes: 4 additions & 1 deletion pages/tag/[tag]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export async function getStaticProps({ params: { tag }, locale }) {
if (siteConfig('POST_LIST_STYLE') === 'scroll') {
// 滚动列表 给前端返回所有数据
} else if (siteConfig('POST_LIST_STYLE') === 'page') {
props.posts = props.posts?.slice(0, siteConfig('POSTS_PER_PAGE'))
props.posts = props.posts?.slice(
0,
siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
)
}

props.tag = tag
Expand Down
11 changes: 7 additions & 4 deletions pages/tag/[tag]/page/[page].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export async function getStaticProps({ params: { tag, page }, locale }) {
.filter(post => post && post?.tags && post?.tags.includes(tag))
// 处理文章数
props.postCount = props.posts.length
const POSTS_PER_PAGE = siteConfig('POSTS_PER_PAGE', 12, props?.NOTION_CONFIG)
// 处理分页
props.posts = props.posts.slice(
siteConfig('POSTS_PER_PAGE') * (page - 1),
siteConfig('POSTS_PER_PAGE') * page
POSTS_PER_PAGE * (page - 1),
POSTS_PER_PAGE * page
)

props.tag = tag
Expand All @@ -43,7 +44,7 @@ export async function getStaticProps({ params: { tag, page }, locale }) {

export async function getStaticPaths() {
const from = 'tag-page-static-path'
const { tagOptions, allPages } = await getGlobalData({ from })
const { tagOptions, allPages, NOTION_CONFIG } = await getGlobalData({ from })
const paths = []
tagOptions?.forEach(tag => {
// 过滤状态类型
Expand All @@ -52,7 +53,9 @@ export async function getStaticPaths() {
.filter(post => post && post?.tags && post?.tags.includes(tag.name))
// 处理文章页数
const postCount = tagPosts.length
const totalPages = Math.ceil(postCount / siteConfig('POSTS_PER_PAGE'))
const totalPages = Math.ceil(
postCount / siteConfig('POSTS_PER_PAGE', 12, NOTION_CONFIG)
)
if (totalPages > 1) {
for (let i = 1; i <= totalPages; i++) {
paths.push({ params: { tag: tag.name, page: '' + i } })
Expand Down
Loading

0 comments on commit 517a967

Please sign in to comment.