mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
Merge pull request #2398 from tangly1024/feat/notion-config
Feat/notion config
This commit is contained in:
@@ -4,11 +4,14 @@ import { getAllCategories } from '@/lib/notion/getAllCategories'
|
||||
import getAllPageIds from '@/lib/notion/getAllPageIds'
|
||||
import { getAllTags } from '@/lib/notion/getAllTags'
|
||||
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
|
||||
import getPageProperties from '@/lib/notion/getPageProperties'
|
||||
import getPageProperties, {
|
||||
adjustPageProperties
|
||||
} from '@/lib/notion/getPageProperties'
|
||||
import { getPostBlocks, getSingleBlock } from '@/lib/notion/getPostBlocks'
|
||||
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
|
||||
import { deepClone } from '@/lib/utils'
|
||||
import { idToUuid } from 'notion-utils'
|
||||
import { siteConfig } from '../config'
|
||||
import { extractLangId, extractLangPrefix } from '../utils/pageId'
|
||||
|
||||
export { getAllTags } from '../notion/getAllTags'
|
||||
@@ -234,38 +237,43 @@ function getCategoryOptions(schema) {
|
||||
* @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)
|
||||
if (!collection && !block) {
|
||||
return {
|
||||
title: BLOG.TITLE,
|
||||
description: BLOG.DESCRIPTION,
|
||||
pageCover: BLOG.HOME_BANNER_IMAGE,
|
||||
icon: BLOG.AVATAR,
|
||||
link: BLOG.LINK
|
||||
title: defaultTitle,
|
||||
description: defaultDescription,
|
||||
pageCover: defaultPageCover,
|
||||
icon: defaultIcon,
|
||||
link: defaultLink
|
||||
}
|
||||
}
|
||||
|
||||
const title = collection?.name?.[0][0] || BLOG.TITLE
|
||||
const title = collection?.name?.[0][0] || defaultTitle
|
||||
const description = collection?.description
|
||||
? Object.assign(collection).description[0][0]
|
||||
: BLOG.DESCRIPTION
|
||||
: defaultDescription
|
||||
|
||||
const pageCover = collection?.cover
|
||||
? mapImgUrl(collection?.cover, collection, 'collection')
|
||||
: BLOG.HOME_BANNER_IMAGE
|
||||
: defaultPageCover
|
||||
|
||||
// 用户头像压缩一下
|
||||
let icon = compressImage(
|
||||
collection?.icon
|
||||
? mapImgUrl(collection?.icon, collection, 'collection')
|
||||
: BLOG.AVATAR
|
||||
: defaultIcon
|
||||
)
|
||||
// 站点网址
|
||||
const link = NOTION_CONFIG?.LINK || BLOG.LINK
|
||||
const link = NOTION_CONFIG?.LINK || defaultLink
|
||||
|
||||
// 站点图标不能是emoji
|
||||
const emojiPattern = /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g
|
||||
if (!icon || emojiPattern.test(icon)) {
|
||||
icon = BLOG.AVATAR
|
||||
icon = defaultIcon
|
||||
}
|
||||
return { title, description, pageCover, icon, link }
|
||||
}
|
||||
@@ -442,14 +450,19 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
||||
}
|
||||
}
|
||||
|
||||
// 文章计数
|
||||
let postCount = 0
|
||||
|
||||
// 站点配置优先读取配置表格,否则读取blog.config.js 文件
|
||||
const NOTION_CONFIG = (await getConfigMapFromConfigPage(collectionData)) || {}
|
||||
|
||||
// 处理每一条数据的字段
|
||||
collectionData.forEach(function (element) {
|
||||
adjustPageProperties(element, NOTION_CONFIG)
|
||||
})
|
||||
|
||||
const siteInfo = getSiteInfo({ collection, block, pageId })
|
||||
|
||||
// 文章计数
|
||||
let postCount = 0
|
||||
|
||||
// 查找所有的Post和Page
|
||||
const allPages = collectionData.filter(post => {
|
||||
if (post?.type === 'Post' && post.status === 'Published') {
|
||||
@@ -464,7 +477,7 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) {
|
||||
})
|
||||
|
||||
// Sort by date
|
||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||
if (siteConfig('POSTS_SORT_BY', '', NOTION_CONFIG) === 'date') {
|
||||
allPages.sort((a, b) => {
|
||||
return b?.publishDate - a?.publishDate
|
||||
})
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import getAllPageIds from './getAllPageIds'
|
||||
import getPageProperties from './getPageProperties'
|
||||
import { getNotionPageData } from '@/lib/db/getSiteData'
|
||||
import { delCacheData } from '@/lib/cache/cache_manager'
|
||||
|
||||
/**
|
||||
* 获取所有文章列表
|
||||
* @param notionPageData
|
||||
* @param from
|
||||
* @param pageType 页面类型数组 ['Post','Page']
|
||||
* @returns {Promise<*[]>}
|
||||
*/
|
||||
export async function getAllPosts({ notionPageData, from, pageType }) {
|
||||
if (!notionPageData) {
|
||||
notionPageData = await getNotionPageData({ from })
|
||||
}
|
||||
if (!notionPageData) {
|
||||
return []
|
||||
}
|
||||
|
||||
const { block, schema, tagOptions, collectionQuery, collectionId, collectionView, viewIds } = notionPageData
|
||||
const data = []
|
||||
const pageIds = getAllPageIds(collectionQuery, collectionId, collectionView, viewIds)
|
||||
for (let i = 0; i < pageIds.length; i++) {
|
||||
const id = pageIds[i]
|
||||
const value = block[id]?.value
|
||||
if (!value) {
|
||||
continue
|
||||
}
|
||||
const properties = (await getPageProperties(id, block[id].value, schema, null, tagOptions)) || null
|
||||
data.push(properties)
|
||||
}
|
||||
|
||||
// remove all the the items doesn't meet requirements
|
||||
const posts = data.filter(post => {
|
||||
return post.title && post?.status?.[0] === 'Published' && pageType.indexOf(post?.type?.[0]) > -1
|
||||
})
|
||||
|
||||
if (!posts || posts.length === 0) {
|
||||
const cacheKey = 'page_block_' + BLOG.NOTION_PAGE_ID
|
||||
await delCacheData(cacheKey)
|
||||
}
|
||||
|
||||
// Sort by date
|
||||
if (BLOG.POSTS_SORT_BY === 'date') {
|
||||
posts.sort((a, b) => {
|
||||
return b?.publishDate - a?.publishDate
|
||||
})
|
||||
}
|
||||
return posts
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { getDateValue, getTextContent } from 'notion-utils'
|
||||
import formatDate from '../utils/formatDate'
|
||||
// import { createHash } from 'crypto'
|
||||
import md5 from 'js-md5'
|
||||
import { siteConfig } from '../config'
|
||||
import { checkContainHttp, sliceUrlFromHttp } from '../utils'
|
||||
import { mapImgUrl } from './mapImage'
|
||||
|
||||
@@ -117,49 +118,6 @@ export default async function getPageProperties(
|
||||
}
|
||||
}) || []
|
||||
delete properties.content
|
||||
|
||||
// 处理URL
|
||||
// 1.按照用户配置的URL_PREFIX 转换一下slug
|
||||
// 2.为文章添加一个href字段,存储最终调整的路径
|
||||
if (properties.type === 'Post') {
|
||||
if (BLOG.POST_URL_PREFIX) {
|
||||
properties.slug = generateCustomizeSlug(properties)
|
||||
}
|
||||
properties.href = properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Page') {
|
||||
properties.href = properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
|
||||
// 菜单路径为空、作为可展开菜单使用
|
||||
properties.href = properties.slug ?? '#'
|
||||
properties.name = properties.title ?? ''
|
||||
}
|
||||
|
||||
// 开启伪静态路径
|
||||
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
|
||||
if (
|
||||
!properties?.href?.endsWith('.html') &&
|
||||
!properties?.href?.startsWith('http')
|
||||
) {
|
||||
properties.href += '.html'
|
||||
}
|
||||
}
|
||||
|
||||
// 最终检查超链接
|
||||
properties.href = checkContainHttp(properties?.href)
|
||||
? sliceUrlFromHttp(properties?.href)
|
||||
: `/${properties.href}`
|
||||
|
||||
// 设置链接在页内或新页面打开
|
||||
if (properties.href?.indexOf('http') === 0) {
|
||||
properties.target = '_blank'
|
||||
} else {
|
||||
properties.target = '_self'
|
||||
}
|
||||
|
||||
// 密码字段md5
|
||||
properties.password = properties.password
|
||||
? md5(properties.slug + properties.password)
|
||||
: ''
|
||||
return properties
|
||||
}
|
||||
|
||||
@@ -202,6 +160,55 @@ function mapProperties(properties) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤处理页面数据
|
||||
* 过滤处理过程会用到NOTION_CONFIG中的配置
|
||||
*/
|
||||
export function adjustPageProperties(properties, NOTION_CONFIG) {
|
||||
// 处理URL
|
||||
// 1.按照用户配置的URL_PREFIX 转换一下slug
|
||||
// 2.为文章添加一个href字段,存储最终调整的路径
|
||||
if (properties.type === 'Post') {
|
||||
if (siteConfig('POST_URL_PREFIX', '', NOTION_CONFIG)) {
|
||||
properties.slug = generateCustomizeSlug(properties, NOTION_CONFIG)
|
||||
}
|
||||
properties.href = properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Page') {
|
||||
properties.href = properties.slug ?? properties.id
|
||||
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
|
||||
// 菜单路径为空、作为可展开菜单使用
|
||||
properties.href = properties.slug ?? '#'
|
||||
properties.name = properties.title ?? ''
|
||||
}
|
||||
|
||||
// 开启伪静态路径
|
||||
if (siteConfig('PSEUDO_STATIC', false, NOTION_CONFIG)) {
|
||||
if (
|
||||
!properties?.href?.endsWith('.html') &&
|
||||
!properties?.href?.startsWith('http')
|
||||
) {
|
||||
properties.href += '.html'
|
||||
}
|
||||
}
|
||||
|
||||
// 最终检查超链接
|
||||
properties.href = checkContainHttp(properties?.href)
|
||||
? sliceUrlFromHttp(properties?.href)
|
||||
: `/${properties.href}`
|
||||
|
||||
// 设置链接在页内或新页面打开
|
||||
if (properties.href?.indexOf('http') === 0) {
|
||||
properties.target = '_blank'
|
||||
} else {
|
||||
properties.target = '_self'
|
||||
}
|
||||
|
||||
// 密码字段md5
|
||||
properties.password = properties.password
|
||||
? md5(properties.slug + properties.password)
|
||||
: ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义URL
|
||||
* 可以根据变量生成URL
|
||||
@@ -209,9 +216,14 @@ function mapProperties(properties) {
|
||||
* @param {*} postProperties
|
||||
* @returns
|
||||
*/
|
||||
function generateCustomizeSlug(postProperties) {
|
||||
function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
|
||||
let fullPrefix = ''
|
||||
const allSlugPatterns = BLOG.POST_URL_PREFIX.split('/')
|
||||
const allSlugPatterns = siteConfig(
|
||||
'POST_URL_PREFIX',
|
||||
'',
|
||||
NOTION_CONFIG
|
||||
).split('/')
|
||||
|
||||
allSlugPatterns.forEach((pattern, idx) => {
|
||||
if (pattern === '%year%' && postProperties?.publishDay) {
|
||||
const formatPostCreatedDate = new Date(postProperties?.publishDay)
|
||||
|
||||
Reference in New Issue
Block a user