diff --git a/components/GoogleAdsense.js b/components/GoogleAdsense.js index 5f17f6ae288..f102847bf96 100644 --- a/components/GoogleAdsense.js +++ b/components/GoogleAdsense.js @@ -39,6 +39,7 @@ function requestAd(ads) { // 获取节点或其子节点中包含 adsbygoogle 类的节点 function getNodesWithAdsByGoogleClass(node) { const adsNodes = [] + // 检查节点及其子节点是否包含 adsbygoogle 类 function checkNodeForAds(node) { if ( @@ -53,6 +54,7 @@ function getNodesWithAdsByGoogleClass(node) { } } } + checkNodeForAds(node) return adsNodes } @@ -174,6 +176,9 @@ const AdSlot = ({ type = 'show' }) => { * @param {*} props */ const AdEmbed = () => { + const ADSENSE_GOOGLE_ID = siteConfig('ADSENSE_GOOGLE_ID') + const ADSENSE_GOOGLE_TEST = siteConfig('ADSENSE_GOOGLE_TEST') + const ADSENSE_GOOGLE_SLOT_AUTO = siteConfig('ADSENSE_GOOGLE_SLOT_AUTO') useEffect(() => { setTimeout(() => { // 找到所有 class 为 notion-text 且内容为 '' 的 div 元素 @@ -187,18 +192,12 @@ const AdEmbed = () => { const newInsElement = document.createElement('ins') newInsElement.className = 'adsbygoogle w-full py-1' newInsElement.style.display = 'block' - newInsElement.setAttribute( - 'data-ad-client', - siteConfig('ADSENSE_GOOGLE_ID') - ) + newInsElement.setAttribute('data-ad-client', ADSENSE_GOOGLE_ID) newInsElement.setAttribute( 'data-adtest', - siteConfig('ADSENSE_GOOGLE_TEST') ? 'on' : 'off' - ) - newInsElement.setAttribute( - 'data-ad-slot', - siteConfig('ADSENSE_GOOGLE_SLOT_AUTO') + ADSENSE_GOOGLE_TEST ? 'on' : 'off' ) + newInsElement.setAttribute('data-ad-slot', ADSENSE_GOOGLE_SLOT_AUTO) newInsElement.setAttribute('data-ad-format', 'auto') newInsElement.setAttribute('data-full-width-responsive', 'true') diff --git a/components/NotionPage.js b/components/NotionPage.js index 0ac8c1c0514..a3f1a085127 100644 --- a/components/NotionPage.js +++ b/components/NotionPage.js @@ -27,7 +27,7 @@ const NotionPage = ({ post, className }) => { }) const zoomRef = useRef(zoom ? zoom.clone() : null) - + const IMAGE_ZOOM_IN_WIDTH = siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200) // 页面首次打开时执行的勾子 useEffect(() => { // 检测当前的url并自动滚动到对应目标 @@ -64,7 +64,7 @@ const NotionPage = ({ post, className }) => { // 替换为更高清的图像 mutation?.target?.setAttribute( 'src', - compressImage(src, siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200)) + compressImage(src, IMAGE_ZOOM_IN_WIDTH) ) }, 800) } diff --git a/components/SEO.js b/components/SEO.js index b82087f3db7..b102ed1116e 100644 --- a/components/SEO.js +++ b/components/SEO.js @@ -12,9 +12,12 @@ import { useEffect } from 'react' */ const SEO = props => { const { children, siteInfo, post, NOTION_CONFIG } = props - let url = siteConfig('PATH')?.length - ? `${siteConfig('LINK')}/${siteConfig('SUB_PATH', '')}` - : siteConfig('LINK') + const PATH = siteConfig('PATH') + const LINK = siteConfig('LINK') + const SUB_PATH = siteConfig('SUB_PATH', '') + let url = PATH?.length + ? `${LINK}/${SUB_PATH}` + : LINK let image const router = useRouter() const meta = getSEOMeta(props, router, useGlobal()?.locale) @@ -40,7 +43,8 @@ const SEO = props => { }, []) // SEO关键词 - let keywords = meta?.tags || siteConfig('KEYWORDS') + const KEYWORDS = siteConfig('KEYWORDS') + let keywords = meta?.tags || KEYWORDS if (post?.tags && post?.tags?.length > 0) { keywords = post?.tags?.join(',') } @@ -48,11 +52,12 @@ const SEO = props => { url = `${url}/${meta.slug}` image = meta.image || '/bg_image.jpg' } - const title = meta?.title || siteConfig('TITLE') + const TITLE = siteConfig('TITLE') + const title = meta?.title || TITLE const description = meta?.description || `${siteInfo?.description}` const type = meta?.type || 'website' const lang = siteConfig('LANG').replace('-', '_') // Facebook OpenGraph 要 zh_CN 這樣的格式才抓得到語言 - const category = meta?.category || siteConfig('KEYWORDS') // section 主要是像是 category 這樣的分類,Facebook 用這個來抓連結的分類 + const category = meta?.category || KEYWORDS // section 主要是像是 category 這樣的分類,Facebook 用這個來抓連結的分類 const favicon = siteConfig('BLOG_FAVICON') const BACKGROUND_DARK = siteConfig('BACKGROUND_DARK', '', NOTION_CONFIG) @@ -94,6 +99,7 @@ const SEO = props => { const FACEBOOK_PAGE = siteConfig('FACEBOOK_PAGE', null, NOTION_CONFIG) + const AUTHOR = siteConfig('AUTHOR') return (
@@ -154,7 +160,7 @@ const SEO = props => { {meta?.type === 'Post' && ( <> - + > @@ -173,6 +179,7 @@ const getSEOMeta = (props, router, locale) => { const { post, siteInfo, tag, category, page } = props const keyword = router?.query?.s + const TITLE = siteConfig('TITLE') switch (router.route) { case '/': return { @@ -235,7 +242,7 @@ const getSEOMeta = (props, router, locale) => { case '/search/[keyword]/page/[page]': return { title: `${keyword || ''}${keyword ? ' | ' : ''}${locale.NAV.SEARCH} | ${siteInfo?.title}`, - description: siteConfig('TITLE'), + description: TITLE, image: `${siteInfo?.pageCover}`, slug: 'search/' + (keyword || ''), type: 'website' diff --git a/lib/config.js b/lib/config.js index 2d46b6fb476..bf03f95f0a2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -38,6 +38,9 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => { case 'TAG_SORT_BY_COUNT': case 'THEME': case 'LINK': + case 'NPM_CDN_BASE': + case 'CDNJS_CDN_BASE': + case 'JSDELIVR_CDN_BASE': // LINK比较特殊, if (key === 'LINK') { if (!extendConfig || Object.keys(extendConfig).length === 0) { diff --git a/lib/notion/convertInnerUrl.js b/lib/notion/convertInnerUrl.js index 0a8cfbce4f8..017bebd13f8 100644 --- a/lib/notion/convertInnerUrl.js +++ b/lib/notion/convertInnerUrl.js @@ -1,5 +1,6 @@ import { idToUuid } from 'notion-utils' import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils' +import { loadLangFromLocalStorage } from '@/lib/lang' /** * 处理页面内连接跳转: @@ -17,28 +18,26 @@ export const convertInnerUrl = allPages => { if (!allAnchorTags) { return } - - const currentURL = window.location.origin + window.location.pathname - // url替换成slug + const { origin, pathname } = window.location; + const currentURL = origin + pathname + const currentPathLang = pathname.split('/').filter(Boolean)[0] + const lang = loadLangFromLocalStorage().split(/[-_]/)[0] + const langPrefix = lang === currentPathLang ? '/' + lang : '' for (const anchorTag of allAnchorTags) { - // 检查url + // url替换成slug if (anchorTag?.href) { // 如果url是一个Notion_id,尝试匹配成博客的文章内链 const slug = getLastPartOfUrl(anchorTag.href) if (checkStrIsNotionId(slug)) { const slugPage = allPages?.find(page => { - const find = idToUuid(slug).indexOf(page.short_id) === 0 - return find + return idToUuid(slug).indexOf(page.short_id) === 14 }) if (slugPage) { - anchorTag.href = slugPage?.href + anchorTag.href = langPrefix + slugPage?.href } } } - } - - // 链接在当前页面打开 - for (const anchorTag of allAnchorTags) { + // 链接在当前页面打开 if (anchorTag?.target === '_blank') { const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0] const hrefWithRelativeHash = @@ -51,4 +50,4 @@ export const convertInnerUrl = allPages => { } } } -} +} \ No newline at end of file diff --git a/lib/notion/getPageTableOfContents.js b/lib/notion/getPageTableOfContents.js index a23be7cb30e..738c22031cf 100644 --- a/lib/notion/getPageTableOfContents.js +++ b/lib/notion/getPageTableOfContents.js @@ -12,7 +12,7 @@ const indentLevels = { * H1, H2, and H3 elements. */ export const getPageTableOfContents = (page, recordMap) => { - const contents = (page.content ?? []) + const contents = page.content ?? [] const toc = getBlockHeader(contents, recordMap) const indentLevelStack = [ { @@ -69,20 +69,28 @@ function getBlockHeader(contents, recordMap, toc) { continue } const { type } = block - if (type.indexOf('header') >= 0) { - const existed = toc.find(e => e.id === blockId) - if (!existed) { - toc.push({ - id: blockId, - type, - text: getTextContent(block.properties?.title), - indentLevel: indentLevels[type] - }) - } - } - if (block.content?.length > 0) { getBlockHeader(block.content, recordMap, toc) + } else { + if (type.indexOf('header') >= 0) { + const existed = toc.find(e => e.id === blockId) + if (!existed) { + toc.push({ + id: blockId, + type, + text: getTextContent(block.properties?.title), + indentLevel: indentLevels[type] + }) + } + } else if (type === 'transclusion_reference') { + getBlockHeader( + [block.format.transclusion_reference_pointer.id], + recordMap, + toc + ) + } else if (type === 'transclusion_container') { + getBlockHeader(block.content, recordMap, toc) + } } } diff --git a/lib/utils/pageId.js b/lib/utils/pageId.js index c963df28b62..6bd39885811 100644 --- a/lib/utils/pageId.js +++ b/lib/utils/pageId.js @@ -38,10 +38,7 @@ function getShortId(uuid) { if (!uuid || uuid.indexOf('-') < 0) { return uuid } - // 找到第一个 '-' 的位置 - const index = uuid.indexOf('-') - // 截取从开始到第一个 '-' 之前的部分 - return uuid.substring(0, index) + return uuid.substring(14) } module.exports = { extractLangPrefix, extractLangId, getShortId } diff --git a/package.json b/package.json index 4c579c78c3b..374d005a02c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "notion-next", - "version": "4.7.11", + "version": "4.7.12", "homepage": "https://github.com/tangly1024/NotionNext.git", "license": "MIT", "repository": { diff --git a/themes/example/index.js b/themes/example/index.js index d78e20a4561..412366e1add 100644 --- a/themes/example/index.js +++ b/themes/example/index.js @@ -156,6 +156,7 @@ const LayoutPostList = props => { const LayoutSlug = props => { const { post, lock, validPassword } = props const router = useRouter() + const waiting404 = siteConfig('POST_WAITING_TIME_FOR_404') * 1000 useEffect(() => { // 404 if (!post) { @@ -170,7 +171,7 @@ const LayoutSlug = props => { } } }, - siteConfig('POST_WAITING_TIME_FOR_404') * 1000 + waiting404 ) } }, [post]) diff --git a/themes/fukasawa/index.js b/themes/fukasawa/index.js index 0e2cf9d3623..d184f1f9141 100644 --- a/themes/fukasawa/index.js +++ b/themes/fukasawa/index.js @@ -137,6 +137,7 @@ const LayoutPostList = props => { const LayoutSlug = props => { const { post, lock, validPassword } = props const router = useRouter() + const waiting404 = siteConfig('POST_WAITING_TIME_FOR_404') * 1000 useEffect(() => { // 404 if (!post) { @@ -151,7 +152,7 @@ const LayoutSlug = props => { } } }, - siteConfig('POST_WAITING_TIME_FOR_404') * 1000 + waiting404 ) } }, [post]) diff --git a/themes/gitbook/index.js b/themes/gitbook/index.js index b737e9f88c7..dbe65ff9dc2 100644 --- a/themes/gitbook/index.js +++ b/themes/gitbook/index.js @@ -78,7 +78,7 @@ function getNavPagesWithLatest(allNavPages, latestPosts, post) { } // 属于最新文章通常6篇 && (无阅读记录 || 最近更新时间大于上次阅读时间) if ( - latestPosts.some(post => post?.id.indexOf(item?.short_id) === 0) && + latestPosts.some(post => post?.id.indexOf(item?.short_id) === 14) && (!postReadTime[item.short_id] || postReadTime[item.short_id] < new Date(item.lastEditedDate).getTime()) ) { @@ -316,6 +316,7 @@ const LayoutSlug = props => { ? `${post?.title} | ${siteInfo?.description}` : `${post?.title} | ${siteInfo?.title}` + const waiting404 = siteConfig('POST_WAITING_TIME_FOR_404') * 1000 useEffect(() => { // 404 if (!post) { @@ -332,7 +333,7 @@ const LayoutSlug = props => { } } }, - siteConfig('POST_WAITING_TIME_FOR_404') * 1000 + waiting404 ) } }, [post]) diff --git a/themes/heo/components/Footer.js b/themes/heo/components/Footer.js index 2d189ee7b45..222554680d5 100644 --- a/themes/heo/components/Footer.js +++ b/themes/heo/components/Footer.js @@ -8,6 +8,7 @@ import SocialButton from './SocialButton' * @returns */ const Footer = () => { + const BEI_AN = siteConfig('BEI_AN') return (