Merge branch 'main' into feat/mail-encypt

This commit is contained in:
tangly1024
2025-07-27 21:24:37 +08:00
committed by GitHub
390 changed files with 4508 additions and 1516 deletions

View File

@@ -2,7 +2,7 @@
import BLOG from '@/blog.config'
import { useGlobal } from './global'
import { deepClone, isUrl } from './utils'
import { deepClone, isUrlLikePath } from './utils'
/**
* 读取配置顺序
@@ -146,7 +146,7 @@ export const convertVal = val => {
}
// 检测是否为 URL
if (isUrl(val)) {
if (isUrlLikePath(val)) {
return val
}

View File

@@ -840,4 +840,4 @@ export function getNavPages({ allPages }) {
publishDate: item.publishDate,
ext: item.ext || {}
}))
}
}

View File

@@ -31,6 +31,7 @@ export function GlobalContextProvider(props) {
) // 默认语言
const [theme, setTheme] = useState(NOTION_CONFIG?.THEME || THEME) // 默认博客主题
const [THEME_CONFIG, SET_THEME_CONFIG] = useState(null) // 主题配置
const [isLiteMode,setLiteMode] = useState(false)
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
@@ -85,10 +86,17 @@ export function GlobalContextProvider(props) {
// 添加路由变化时的语言处理
useEffect(() => {
initLocale(router.locale, changeLang, updateLocale)
}, [router])
// 处理极简模式
if (router.query.lite && router.query.lite==='true') {
setLiteMode(true)
}
}, [router])
// 首次加载成功
useEffect(() => {
initDarkMode(updateDarkMode, defaultDarkMode)
// 处理多语言自动重定向
if (
NOTION_CONFIG?.REDIRECT_LANG &&
JSON.parse(NOTION_CONFIG?.REDIRECT_LANG)
@@ -107,6 +115,7 @@ export function GlobalContextProvider(props) {
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${themeStr}`
router.push(newUrl)
}
if (!onLoading) {
setOnLoading(true)
}
@@ -134,6 +143,7 @@ export function GlobalContextProvider(props) {
return (
<GlobalContext.Provider
value={{
isLiteMode,
isLoaded,
isSignedIn,
user,

View File

@@ -0,0 +1,181 @@
/**
* 获取属性值,优先从 overrides 中读取,否则按顺序从 properties 中读取,最后返回默认值
* @param {Object} properties 原始属性对象
* @param {Array} keys 优先级字段名列表
* @param {Object} overrides 自定义覆盖对象(可选)
* @param {string} defaultValue 默认值(可选)
*/
function getPropertyValue(properties, keys, overrides = {}, defaultValue = '') {
for (const key of keys) {
if (overrides[key]) return overrides[key]
if (properties[key]) return properties[key]
}
return defaultValue
}
/**
* 提取 Notion 装饰文本的纯文本内容。
* 可选传入 resolveRef 来解析引用(例如 '‣' 指向的页面标题)
*
* @param {Array} text - Notion Decoration[] 格式的文本数组
* @returns {string}
*/
function getFullTextContent(text) {
if (!text) return ''
if (!Array.isArray(text)) return String(text)
return text.reduce((result, item) => {
const value = item[0]
const decorations = item[1]
if (value === '⁍') {
// 检查是否有公式
const equation = decorations?.find(d => d[0] === 'e')
if (equation) {
return result + equation[1] // 提取 LaTeX 内容
}
return result // 否则什么都不加
}
if (value === '‣') {
const ref = Array.isArray(decorations) ? decorations[0] : null
const type = ref?.[0]
const data = ref?.[1]
// todo: 处理更多类型 https://github.com/NotionX/react-notion-x/blob/9ee2d9334e260ee3600f4f8d7212f66b641b19cc/packages/notion-types/src/core.ts#L108
switch (type) {
case 'd':
// 日期字符串
const date =
data?.start_date ||
data?.start_time ||
data?.end_date ||
data?.end_time ||
'[Date]'
return result + date
case 'lm':
// Link Mention
const title = data?.title || data?.href || '[Link]'
return result + title
// 用户 ID这里不展开默认忽略或标记
case 'u':
default:
return result
}
}
// 默认拼接普通文本
return result + value
}, '')
}
export function getPageContentText(post, pageBlockMap) {
/**
* 将对象的指定字段拼接到字符串
* @param block
* @param customKeys 优先级字段名列表
* @returns string
*/
function getText(block, customKeys = ['title', 'caption']) {
const result = []
const properties = block.properties
if (!properties) {
return ''
}
const textArray = getPropertyValue(properties, customKeys)
result.push(getTextArray(textArray))
if (block.type !== 'page' && block['content']?.length > 0) {
for (const blockContent of block.content) {
result.push(getBlockContentText(blockContent))
}
}
return result.join(' ')
}
function getTextArray(textArray) {
const text = textArray ? getFullTextContent(textArray) : ''
if (text && text !== 'Untitled') {
return text
}
return ''
}
function getTransclusionReference(block) {
const result = []
const blockPointer = block.format.transclusion_reference_pointer
const blockPointerId = blockPointer.id
if (blockPointer && pageBlockMap.block[blockPointerId].value) {
const blockContentList = pageBlockMap.block[blockPointerId].value.content
for (const blockContent of blockContentList) {
result.push(getBlockContentText(blockContent))
}
}
return result.join(' ')
}
function getBlockContentText(id) {
const block = pageBlockMap?.block[id]?.value
if (!block) {
return ''
}
const blockType = block.type
// todo: 处理更多类型 https://github.com/NotionX/react-notion-x/blob/9ee2d9334e260ee3600f4f8d7212f66b641b19cc/packages/notion-types/src/block.ts#L3
switch (blockType) {
case 'transclusion_reference':
return getTransclusionReference(block)
case 'table':
return getTableText(block.content)
case 'page':
if (id !== postId) {
return getText(block)
}
return ''
case 'breadcrumb':
case 'external_object_instance':
case 'divider':
return ''
case 'image':
return getText(block, ['alt_text', 'title'])
// 除title以外,还有额外的link和description可供索引但认为不需要
case 'bookmark':
case 'quote':
case 'callout':
case 'header':
case 'sub_header':
case 'code':
case 'equation':
case 'text':
default:
return getText(block)
}
}
function getTableText(tableRowIds) {
const result = []
for (const blockRowId of tableRowIds) {
if (pageBlockMap.block[blockRowId]) {
const blockRow = pageBlockMap.block[blockRowId].value
const blockRowProperties = blockRow.properties
for (const blockRowPropertyValue of Object.values(blockRowProperties)) {
result.push(getTextArray(blockRowPropertyValue))
}
}
}
return result.join(' ')
}
const postId = post.id
const postContent = post.content
let contentTextList = []
// 防止搜到加密文章的内容
if (postContent && postContent.length > 0 && !post.password) {
for (const postContentId of postContent) {
const blockContentText = getBlockContentText(postContentId)
if (blockContentText) {
contentTextList.push(blockContentText)
}
}
}
// console.log('开始', contentTextList.join(''), '结束')
return contentTextList.join('')
}

View File

@@ -4,11 +4,7 @@ import formatDate from '../utils/formatDate'
// import { createHash } from 'crypto'
import md5 from 'js-md5'
import { siteConfig } from '../config'
import {
checkStartWithHttp,
convertUrlStartWithOneSlash,
getLastSegmentFromUrl
} from '../utils'
import { convertUrlStartWithOneSlash, getLastSegmentFromUrl, isHttpLink, isMailOrTelLink } from '../utils'
import { extractLangPrefix } from '../utils/pageId'
import { mapImgUrl } from './mapImage'
import notionAPI from '@/lib/notion/getNotionAPI'
@@ -85,8 +81,9 @@ export default async function getPageProperties(
const fieldNames = BLOG.NOTION_PROPERTY_NAME
if (fieldNames) {
Object.keys(fieldNames).forEach(key => {
if (fieldNames[key] && properties[fieldNames[key]])
if (fieldNames[key] && properties[fieldNames[key]]) {
properties[key] = properties[fieldNames[key]]
}
})
}
@@ -190,9 +187,12 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
}
// http or https 开头的视为外链
if (checkStartWithHttp(properties?.href)) {
if (isHttpLink(properties?.href)) {
properties.href = properties?.slug
properties.target = '_blank'
} else if (isMailOrTelLink(properties?.href)) {
properties.href = properties?.slug
properties.target = '_self'
} else {
properties.target = '_self'
// 伪静态路径右侧拼接.html
@@ -238,7 +238,7 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
*/
function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
// 外链不处理
if (checkStartWithHttp(postProperties.slug)) {
if (isHttpLink(postProperties.slug)) {
return postProperties.slug
}
let fullPrefix = ''

View File

@@ -119,10 +119,19 @@ const compressImage = (image, width, quality = 50, fmt = 'webp') => {
width = siteConfig('IMAGE_COMPRESS_WIDTH')
}
// 将URL解析为一个对象
const urlObj = new URL(image)
// 获取URL参数
const params = new URLSearchParams(urlObj.search)
let urlObj
let params
try {
// 将URL解析为一个对象
urlObj = new URL(image)
// 获取URL参数
params = new URLSearchParams(urlObj.search)
} catch (err) {
// 捕获异常并打印错误的url
console.error('compressImage: Invalid URL:', image, err)
return image
}
// Notion图床
if (

View File

@@ -27,6 +27,6 @@ export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) {
}
} catch (error) {
console.error('ChucklePostAI请求失败', error)
return '获取文章摘要失败,请稍后再试。'
return null
}
}

View File

@@ -1,6 +1,31 @@
import BLOG from '@/blog.config'
import { getPageContentText } from '@/pages/search/[keyword]'
import algoliasearch from 'algoliasearch'
import { getPageContentText } from '@/lib/notion/getPageContentText'
// 全局初始化 Algolia 客户端和索引
let algoliaClient
let algoliaIndex
const initAlgolia = () => {
if (!algoliaClient) {
if (
!BLOG.ALGOLIA_APP_ID ||
!BLOG.ALGOLIA_ADMIN_APP_KEY ||
!BLOG.ALGOLIA_INDEX
) {
console.error('Algolia configuration is missing')
}
algoliaClient = algoliasearch(
BLOG.ALGOLIA_APP_ID,
BLOG.ALGOLIA_ADMIN_APP_KEY
)
algoliaIndex = algoliaClient.initIndex(BLOG.ALGOLIA_INDEX)
}
return { client: algoliaClient, index: algoliaIndex }
}
// 初始化全局实例
initAlgolia()
/**
* 生成全文索引
@@ -15,17 +40,57 @@ const generateAlgoliaSearch = ({ allPages, force = false }) => {
})
}
/**
* 检查数据是否需要从algolia删除
* @param {*} props
*/
export const checkDataFromAlgolia = async props => {
const { allPages } = props
const deletions = (allPages || [])
.map(p => {
if (p && (p.password || p.status === 'Draft')) {
return deletePostDataFromAlgolia(p)
}
})
.filter(Boolean) // 去除 undefined
await Promise.all(deletions)
}
/**
* 删除数据
* @param post
*/
const deletePostDataFromAlgolia = async post => {
if (!post) {
return
}
// 检查是否有索引
let existed
try {
existed = await algoliaIndex.getObject(post.id)
} catch (error) {
// 通常是不存在索引
}
if (existed) {
await algoliaIndex
.deleteObject(post.id)
.wait()
.then(r => {
console.log('Algolia索引删除成功', r)
})
.catch(err => {
console.log('Algolia异常', err)
})
}
}
/**
* 上传数据
* 根据上次修改文章日期和上次更新索引数据判断是否需要更新algolia索引
*/
const uploadDataToAlgolia = async post => {
// Connect and authenticate with your Algolia app
const client = algoliasearch(BLOG.ALGOLIA_APP_ID, BLOG.ALGOLIA_ADMIN_APP_KEY)
// Create a new index and add a record
const index = client.initIndex(BLOG.ALGOLIA_INDEX)
if (!post) {
return
}
@@ -34,7 +99,7 @@ const uploadDataToAlgolia = async post => {
let existed
let needUpdateIndex = false
try {
existed = await index.getObject(post.id)
existed = await algoliaIndex.getObject(post.id)
} catch (error) {
// 通常是不存在索引
}
@@ -64,7 +129,7 @@ const uploadDataToAlgolia = async post => {
content: truncate(getPageContentText(post, post.blockMap), 8192) // 索引8192个字符API限制总请求内容上限1万个字节
}
// console.log('更新Algolia索引', record)
index
algoliaIndex
.saveObject(record)
.wait()
.then(r => {

View File

@@ -80,28 +80,31 @@ export function convertUrlStartWithOneSlash(str) {
}
/**
* 是否是一个相对或绝对路径的ur类
* @param {*} str
* @returns
* 判断是否是一个“URL 样式”的路径(内部或外部)
* 内部如 /about外部如 http://xxx.com
* @param str
* @returns {boolean}
*/
export function isUrl(str) {
if (!str) {
return false
}
return str?.indexOf('/') === 0 || checkStartWithHttp(str)
export function isUrlLikePath(str) {
return typeof str === 'string' && (str.startsWith('/') || isHttpLink(str))
}
// 检查是否外链
export function checkStartWithHttp(str) {
// 检查字符串是否包含http
if (str?.indexOf('http:') === 0 || str?.indexOf('https:') === 0) {
// 如果包含找到http的位置
return true
} else {
// 不包含
return false
}
/**
* 判断是否是 http(s) 开头的链接(外部网页)
* @param str
* @returns {boolean}
*/
export function isHttpLink(str) {
return typeof str === 'string' && /^https?:\/\//i.test(str)
}
/**
* 检查是否是邮件或电话链接
* @param href
* @returns {boolean}
*/
export function isMailOrTelLink(href) {
return /^(mailto:|tel:)/i.test(href)
}
// 检查一个字符串是否UUID https://ihateregex.io/expr/uuid/

View File

@@ -1,16 +1,16 @@
/**
* 文章相关工具
*/
import { checkStartWithHttp } from '.'
import { isHttpLink } from '.'
import { getPostBlocks } from '@/lib/db/getSiteData'
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import { siteConfig } from '@/lib/config'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
import { getPageContentText } from '@/pages/search/[keyword]'
import { getAiSummary } from '@/lib/plugins/aiSummary'
import BLOG from '@/blog.config'
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
import { countWords } from '@/lib/plugins/wordCount'
import { getPageContentText } from '@/lib/notion/getPageContentText'
/**
* 获取文章的关联推荐文章列表,目前根据标签关联性筛选
@@ -59,7 +59,7 @@ export function checkSlugHasNoSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 0 &&
!checkStartWithHttp(slug) &&
!isHttpLink(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -76,7 +76,7 @@ export function checkSlugHasOneSlash(row) {
}
return (
(slug.match(/\//g) || []).length === 1 &&
!checkStartWithHttp(slug) &&
!isHttpLink(slug) &&
row.type.indexOf('Menu') < 0
)
}
@@ -94,7 +94,7 @@ export function checkSlugHasMorThanTwoSlash(row) {
return (
(slug.match(/\//g) || []).length >= 2 &&
row.type.indexOf('Menu') < 0 &&
!checkStartWithHttp(slug)
!isHttpLink(slug)
)
}