Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 22, 2024
2 parents a1b1e79 + 1c4cb7b commit df3a81e
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 92 deletions.
17 changes: 8 additions & 9 deletions components/GoogleAdsense.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function requestAd(ads) {
// 获取节点或其子节点中包含 adsbygoogle 类的节点
function getNodesWithAdsByGoogleClass(node) {
const adsNodes = []

// 检查节点及其子节点是否包含 adsbygoogle 类
function checkNodeForAds(node) {
if (
Expand All @@ -53,6 +54,7 @@ function getNodesWithAdsByGoogleClass(node) {
}
}
}

checkNodeForAds(node)
return adsNodes
}
Expand Down Expand Up @@ -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 且内容为 '<ins/>' 的 div 元素
Expand All @@ -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')

Expand Down
4 changes: 2 additions & 2 deletions components/NotionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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并自动滚动到对应目标
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 15 additions & 8 deletions components/SEO.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,19 +43,21 @@ 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(',')
}
if (meta) {
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)

Expand Down Expand Up @@ -94,6 +99,7 @@ const SEO = props => {

const FACEBOOK_PAGE = siteConfig('FACEBOOK_PAGE', null, NOTION_CONFIG)

const AUTHOR = siteConfig('AUTHOR')
return (
<Head>
<link rel='icon' href={favicon} />
Expand Down Expand Up @@ -154,7 +160,7 @@ const SEO = props => {
{meta?.type === 'Post' && (
<>
<meta property='article:published_time' content={meta.publishDay} />
<meta property='article:author' content={siteConfig('AUTHOR')} />
<meta property='article:author' content={AUTHOR} />
<meta property='article:section' content={category} />
<meta property='article:publisher' content={FACEBOOK_PAGE} />
</>
Expand All @@ -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 {
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 11 additions & 12 deletions lib/notion/convertInnerUrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { idToUuid } from 'notion-utils'
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
import { loadLangFromLocalStorage } from '@/lib/lang'

/**
* 处理页面内连接跳转:
Expand All @@ -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 =
Expand All @@ -51,4 +50,4 @@ export const convertInnerUrl = allPages => {
}
}
}
}
}
34 changes: 21 additions & 13 deletions lib/notion/getPageTableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
5 changes: 1 addition & 4 deletions lib/utils/pageId.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion themes/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -170,7 +171,7 @@ const LayoutSlug = props => {
}
}
},
siteConfig('POST_WAITING_TIME_FOR_404') * 1000
waiting404
)
}
}, [post])
Expand Down
3 changes: 2 additions & 1 deletion themes/fukasawa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -151,7 +152,7 @@ const LayoutSlug = props => {
}
}
},
siteConfig('POST_WAITING_TIME_FOR_404') * 1000
waiting404
)
}
}, [post])
Expand Down
5 changes: 3 additions & 2 deletions themes/gitbook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
) {
Expand Down Expand Up @@ -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) {
Expand All @@ -332,7 +333,7 @@ const LayoutSlug = props => {
}
}
},
siteConfig('POST_WAITING_TIME_FOR_404') * 1000
waiting404
)
}
}, [post])
Expand Down
3 changes: 2 additions & 1 deletion themes/heo/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SocialButton from './SocialButton'
* @returns
*/
const Footer = () => {
const BEI_AN = siteConfig('BEI_AN')
return (
<footer className='relative flex-shrink-0 bg-white dark:bg-[#1a191d] justify-center text-center m-auto w-full leading-6 text-gray-600 dark:text-gray-100 text-sm'>
{/* 颜色过度区 */}
Expand All @@ -33,7 +34,7 @@ const Footer = () => {
</div>

<div id='footer-bottom-right'>
{siteConfig('BEI_AN') && (
{BEI_AN && (
<>
<i className='fas fa-shield-alt' />{' '}
<a href='https://beian.miit.gov.cn/' className='mr-2'>
Expand Down
11 changes: 4 additions & 7 deletions themes/heo/components/LatestPostsGroupMini.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ export default function LatestPostsGroupMini({ latestPosts, siteInfo }) {
// 获取当前路径
const currentPath = useRouter().asPath
const { locale } = useGlobal()
const SUB_PATH = siteConfig('SUB_PATH', '')

if (!latestPosts) {
return <></>
}

return (
return latestPosts ? (
<>
<div className=' mb-2 px-1 flex flex-nowrap justify-between'>
<div>
Expand All @@ -30,7 +27,7 @@ export default function LatestPostsGroupMini({ latestPosts, siteInfo }) {
</div>
{latestPosts.map(post => {
const selected =
currentPath === `${siteConfig('SUB_PATH', '')}/${post.slug}`
currentPath === `${SUB_PATH}/${post.slug}`
const headerImage = post?.pageCoverThumbnail
? post.pageCoverThumbnail
: siteInfo?.pageCover
Expand Down Expand Up @@ -63,5 +60,5 @@ export default function LatestPostsGroupMini({ latestPosts, siteInfo }) {
)
})}
</>
)
) : null
}
Loading

0 comments on commit df3a81e

Please sign in to comment.