Merge branch 'main' of https://github.com/tangly1024/NotionNext into feature/user-auth-clerk

This commit is contained in:
tangly1024.com
2024-09-06 16:50:57 +08:00
78 changed files with 3874 additions and 1094 deletions

View File

@@ -15,12 +15,6 @@ import {
} from './lang'
const GlobalContext = createContext()
/**
* 定义全局变量,包括语言、主题、深色模式、加载状态
* @param children
* @returns {JSX.Element}
* @constructor
*/
export function GlobalContextProvider(props) {
const {
post,
@@ -40,7 +34,7 @@ export function GlobalContextProvider(props) {
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
const [onLoading, setOnLoading] = useState(true) // 抓取文章数据
const router = useRouter()
// 是否全屏
@@ -74,10 +68,6 @@ export function GlobalContextProvider(props) {
htmlElement.classList?.add(newStatus ? 'dark' : 'light')
}
/**
* 更新语言
* 这里是代码级别的多语言,整个站点和文章内容的多语言不在此处理
*/
function changeLang(lang) {
if (lang) {
saveLangToLocalStorage(lang)
@@ -89,13 +79,12 @@ export function GlobalContextProvider(props) {
useEffect(() => {
initDarkMode(updateDarkMode, defaultDarkMode)
initLocale(lang, locale, updateLang, updateLocale)
// 可以
if (NOTION_CONFIG?.REDIRECT_LANG) {
redirectUserLang(NOTION_PAGE_ID)
}
setOnLoading(false)
}, [])
// 加载进度条
useEffect(() => {
const handleStart = url => {
const { theme } = router.query
@@ -103,10 +92,15 @@ export function GlobalContextProvider(props) {
const newUrl = `${url}${url.includes('?') ? '&' : '?'}theme=${theme}`
router.push(newUrl)
}
setOnLoading(true)
if (!onLoading) {
setOnLoading(true)
}
}
const handleStop = () => {
setOnLoading(false)
if (onLoading) {
setOnLoading(false)
}
}
const currentTheme = router?.query?.theme || theme
@@ -120,7 +114,7 @@ export function GlobalContextProvider(props) {
router.events.off('routeChangeComplete', handleStop)
router.events.off('routeChangeError', handleStop)
}
}, [router])
}, [router, onLoading])
return (
<GlobalContext.Provider

View File

@@ -1,3 +1,4 @@
import BLOG from '@/blog.config'
import { getQueryVariable, isBrowser, mergeDeep } from '@/lib/utils'
import enUS from './lang/en-US'
import frFR from './lang/fr-FR'
@@ -127,6 +128,10 @@ export const redirectUserLang = (lang, pageId) => {
if (!window.location.pathname === '/') {
return
}
// 没有开启多语言
if (BLOG.NOTION_PAGE_ID.indexOf(',') < 0) {
return
}
const userLang =
getQueryVariable('locale') ||

View File

@@ -22,6 +22,7 @@ export default {
COMMON: {
THEME: 'Theme',
ARTICLE_LIST: 'Article List',
RECOMMEND_POSTS: 'Recommend Posts',
MORE: 'More',
NO_MORE: 'No More',
LATEST_POSTS: 'Latest posts',

View File

@@ -22,6 +22,7 @@ export default {
COMMON: {
THEME: 'Theme',
ARTICLE_LIST: '文章列表',
RECOMMEND_POSTS: '推荐文章',
MORE: '更多',
NO_MORE: '没有更多了',
LATEST_POSTS: '最新发布',

View File

@@ -81,49 +81,69 @@ export async function getPageWithRetry(id, from, retryAttempts = 3) {
function filterPostBlocks(id, blockMap, slice) {
const clonePageBlock = deepClone(blockMap)
let count = 0
const blocksToProcess = Object.keys(clonePageBlock?.block || {})
// 循环遍历文档的每个block
for (const i in clonePageBlock?.block) {
const b = clonePageBlock?.block[i]
if (slice && slice > 0 && count > slice) {
delete clonePageBlock?.block[i]
continue
for (let i = 0; i < blocksToProcess.length; i++) {
const blockId = blocksToProcess[i]
const b = clonePageBlock?.block[blockId]
if (slice && slice > 0 && count > slice) {
delete clonePageBlock?.block[blockId]
continue
}
// 当BlockId等于PageId时移除
if (b?.value?.id === id) {
// 此block含有敏感信息
delete b?.value?.properties
continue
}
count++
if (b?.value?.type === 'sync_block' && b?.value?.children) {
const childBlocks = b.value.children
// 移除同步块
delete clonePageBlock.block[blockId]
// 用子块替代同步块
childBlocks.forEach((childBlock, index) => {
const newBlockId = `${blockId}_child_${index}`
clonePageBlock.block[newBlockId] = childBlock
blocksToProcess.splice(i + index + 1, 0, newBlockId)
})
// 重新处理新加入的子块
i--
continue
}
// 处理 c++、c#、汇编等语言名字映射
if (b?.value?.type === 'code') {
if (b?.value?.properties?.language?.[0][0] === 'C++') {
b.value.properties.language[0][0] = 'cpp'
}
// 当BlockId等于PageId时移除
if (b?.value?.id === id) {
// 此block含有敏感信息
delete b?.value?.properties
continue
if (b?.value?.properties?.language?.[0][0] === 'C#') {
b.value.properties.language[0][0] = 'csharp'
}
count++
// 处理 c++、c#、汇编等语言名字映射
if (b?.value?.type === 'code') {
if (b?.value?.properties?.language?.[0][0] === 'C++') {
b.value.properties.language[0][0] = 'cpp'
}
if (b?.value?.properties?.language?.[0][0] === 'C#') {
b.value.properties.language[0][0] = 'csharp'
}
if (b?.value?.properties?.language?.[0][0] === 'Assembly') {
b.value.properties.language[0][0] = 'asm6502'
}
}
// 如果是文件或嵌入式PDF需要重新加密签名
if (
(b?.value?.type === 'file' ||
b?.value?.type === 'pdf' ||
b?.value?.type === 'video' ||
b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
) {
const oldUrl = b?.value?.properties?.source?.[0][0]
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`
b.value.properties.source[0][0] = newUrl
if (b?.value?.properties?.language?.[0][0] === 'Assembly') {
b.value.properties.language[0][0] = 'asm6502'
}
}
// 如果是文件或嵌入式PDF需要重新加密签名
if (
(b?.value?.type === 'file' ||
b?.value?.type === 'pdf' ||
b?.value?.type === 'video' ||
b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
) {
const oldUrl = b?.value?.properties?.source?.[0][0]
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`
b.value.properties.source[0][0] = newUrl
}
}
// 去掉不用的字段
if (id === BLOG.NOTION_PAGE_ID) {

View File

@@ -43,7 +43,10 @@ bszCaller = {
return function (t) {
ready(function () {
try {
e(t), scriptTag && scriptTag.parentElement && scriptTag.parentElement.removeChild && scriptTag.parentElement.removeChild(scriptTag)
e(t)
if (scriptTag && scriptTag.parentElement && scriptTag.parentElement.contains(scriptTag)) {
scriptTag.parentElement.removeChild(scriptTag)
}
} catch (t) {
// console.log(t), bszTag.hides()
}

View File

@@ -146,7 +146,7 @@ export function getLastPartOfUrl(url) {
* @param type js 或 css
* @returns {Promise<unknown>}
*/
export function loadExternalResource(url, type) {
export function loadExternalResource(url, type = 'js') {
// 检查是否已存在
const elements =
type === 'js'