Merge branch 'main' into feat-data-cache

This commit is contained in:
tangly1024
2025-01-05 19:15:55 +08:00
committed by GitHub
38 changed files with 823 additions and 654 deletions

View File

@@ -235,6 +235,15 @@ async function convertNotionToSiteDate(pageId, from, pageRecordMap) {
if (post?.type === 'Post' && post.status === 'Published') {
postCount++
}
// 新特性,判断文章的发布和下架时间,如果不在有效期内则进行下架处理
const publish = isInRange(post.date)
if (!publish) {
console.log(post.title, '未处于发布时段 [', post.date, '] 内,将强制隐藏')
// 隐藏
post.status = 'Invisible'
}
return (
post &&
post?.slug &&
@@ -558,12 +567,13 @@ function getCategoryOptions(schema) {
* @returns {Promise<{title,description,pageCover,icon}>}
*/
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 defaultTitle = NOTION_CONFIG?.TITLE || 'NotionNext BLOG'
const defaultDescription =
NOTION_CONFIG?.DESCRIPTION || '这是一个由NotionNext生成的站点'
const defaultPageCover = NOTION_CONFIG?.HOME_BANNER_IMAGE || '/bg_image.jpg'
const defaultIcon = NOTION_CONFIG?.AVATAR || '/avatar.svg'
const defaultLink = NOTION_CONFIG?.LINK || BLOG.LINK
// 空数据的情况返回默认值
if (!collection && !block) {
return {
title: defaultTitle,
@@ -600,6 +610,78 @@ function getSiteInfo({ collection, block, NOTION_CONFIG }) {
return { title, description, pageCover, icon, link }
}
/**
* 判断文章是否在发布时间内
* @param {*} param0
* @returns
*/
function isInRange(date = {}) {
const { start_date, start_time, end_date, end_time, time_zone } = date
// 如果没有传入时区,使用默认时区 'Asia/Shanghai'
const effectiveTimeZone = time_zone || 'Asia/Shanghai'
// 辅助函数:根据时区和日期时间字符串创建 Date 对象
function parseDateTime(date, time, timeZone) {
if (!date) return null // 如果没有传日期,返回 null
const dateTimeString = `${date}T${time}:00${getTimezoneOffset(timeZone)}`
return new Date(dateTimeString) // 返回一个 Date 对象
}
// 辅助函数:获取时区的偏移量
function getTimezoneOffset(timeZone) {
const date = new Date()
const options = { timeZone, hour12: false, timeZoneName: 'short' }
const timeString = new Intl.DateTimeFormat('en-US', options).format(date)
const match = timeString.match(/([A-Za-z]+)([+\-]\d{1,2})(\d{2})/) // 捕获时区信息,确保偏移小时和分钟
if (match) {
const offset = match[2] + match[3] // 组合偏移小时和分钟
return offset // 返回格式:+08:00 或 -03:00
}
return '' // 默认没有时区偏移
}
// 当前时间(转换为目标时区)
const currentDateTime = new Date()
const currentDateTimeInZone = parseDateTime(
currentDateTime.toISOString().slice(0, 10),
currentDateTime.toISOString().slice(11, 16),
effectiveTimeZone
)
// 判断开始时间范围
let startDateTime = null
if (start_date) {
startDateTime = parseDateTime(
start_date,
start_time || '00:00',
effectiveTimeZone
) // 如果没有 start_time 默认设置为 '00:00'
}
// 判断结束时间范围
let endDateTime = null
if (end_date) {
endDateTime = parseDateTime(
end_date,
end_time || '23:59',
effectiveTimeZone
) // 如果没有 end_time 默认设置为 '23:59'
}
// 如果有 start_date当前时间必须大于等于 start_date 和 start_time
if (startDateTime && currentDateTimeInZone < startDateTime) {
return false
}
// 如果有 end_date当前时间必须小于等于 end_date 和 end_time
if (endDateTime && currentDateTimeInZone > endDateTime) {
return false
}
return true // 如果都通过了判断,返回 true
}
/**
* 获取导航用的精减文章列表
* gitbook主题用到只保留文章的标题分类标签分类信息精减掉摘要密码日期等数据

View File

@@ -1,10 +1,14 @@
import { NotionAPI } from 'notion-client'
import { NotionAPI as NotionLibrary } from 'notion-client'
import BLOG from '@/blog.config'
export default function getNotionAPI() {
return new NotionAPI({
const notionAPI = getNotionAPI()
function getNotionAPI() {
return new NotionLibrary({
activeUser: BLOG.NOTION_ACTIVE_USER || null,
authToken: BLOG.NOTION_TOKEN_V2 || null,
userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
})
}
export default notionAPI

View File

@@ -1,5 +1,4 @@
import BLOG from '@/blog.config'
import { NotionAPI } from 'notion-client'
import { getDateValue, getTextContent } from 'notion-utils'
import formatDate from '../utils/formatDate'
// import { createHash } from 'crypto'
@@ -12,7 +11,7 @@ import {
} from '../utils'
import { extractLangPrefix } from '../utils/pageId'
import { mapImgUrl } from './mapImage'
import getNotionAPI from '@/lib/notion/getNotionAPI'
import notionAPI from '@/lib/notion/getNotionAPI'
/**
* 获取页面元素成员属性
@@ -57,12 +56,11 @@ export default async function getPageProperties(
case 'person': {
const rawUsers = val.flat()
const users = []
const api = getNotionAPI()
for (let i = 0; i < rawUsers.length; i++) {
if (rawUsers[i][0][1]) {
const userId = rawUsers[i][0]
const res = await api.getUsers(userId)
const res = await notionAPI.getUsers(userId)
const resValue =
res?.recordMapWithRoles?.notion_user?.[userId[1]]?.value
const user = {

View File

@@ -1,7 +1,7 @@
import BLOG from '@/blog.config'
import { getDataFromCache, getOrSetDataWithCache, setDataToCache } from '@/lib/cache/cache_manager'
import { deepClone, delay } from '../utils'
import getNotionAPI from '@/lib/notion/getNotionAPI'
import notionAPI from '@/lib/notion/getNotionAPI'
/**
* 获取文章内容
@@ -49,9 +49,8 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) {
retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : ''
)
try {
const api = getNotionAPI()
const start = new Date().getTime()
const pageData = await api.getPage(id)
const pageData = await notionAPI.getPage(id)
const end = new Date().getTime()
console.log('[API<<--响应]', `耗时:${end - start}ms - from:${from}`)
return pageData
@@ -169,14 +168,12 @@ export const fetchInBatches = async (ids, batchSize = 100) => {
ids = [ids]
}
const api = getNotionAPI()
let fetchedBlocks = {}
for (let i = 0; i < ids.length; i += batchSize) {
const batch = ids.slice(i, i + batchSize)
console.log('[API-->>请求] Fetching missing blocks', batch, ids.length)
const start = new Date().getTime()
const pageChunk = await api.getBlocks(batch)
const pageChunk = await notionAPI.getBlocks(batch)
const end = new Date().getTime()
console.log(
`[API<<--响应] 耗时:${end - start}ms Fetching missing blocks count:${ids.length} `