Skip to content

Commit 3f80c4e

Browse files
committed
Merge remote-tracking branch 'origin/feat/notion-page-link-convert' into release/4.5.2
2 parents 9481b6a + 622594a commit 3f80c4e

File tree

4 files changed

+138
-88
lines changed

4 files changed

+138
-88
lines changed

components/ExternalPlugins.js

+69-61
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,12 @@ import TianLiGPT from './TianliGPT'
66
import WebWhiz from './Webwhiz'
77

88
import { CUSTOM_EXTERNAL_CSS, CUSTOM_EXTERNAL_JS } from '@/blog.config'
9+
import { mapPageUrl } from '@/lib/notion/mapPageUrl'
910
import { isBrowser, loadExternalResource } from '@/lib/utils'
11+
import { useRouter } from 'next/router'
1012
import { useEffect } from 'react'
1113
import { initGoogleAdsense } from './GoogleAdsense'
1214

13-
const TwikooCommentCounter = dynamic(
14-
() => import('@/components/TwikooCommentCounter'),
15-
{ ssr: false }
16-
)
17-
const DebugPanel = dynamic(() => import('@/components/DebugPanel'), {
18-
ssr: false
19-
})
20-
const ThemeSwitch = dynamic(() => import('@/components/ThemeSwitch'), {
21-
ssr: false
22-
})
23-
const Fireworks = dynamic(() => import('@/components/Fireworks'), {
24-
ssr: false
25-
})
26-
const MouseFollow = dynamic(() => import('@/components/MouseFollow'), {
27-
ssr: false
28-
})
29-
const Nest = dynamic(() => import('@/components/Nest'), { ssr: false })
30-
const FlutteringRibbon = dynamic(
31-
() => import('@/components/FlutteringRibbon'),
32-
{ ssr: false }
33-
)
34-
const Ribbon = dynamic(() => import('@/components/Ribbon'), { ssr: false })
35-
const Sakura = dynamic(() => import('@/components/Sakura'), { ssr: false })
36-
const StarrySky = dynamic(() => import('@/components/StarrySky'), {
37-
ssr: false
38-
})
39-
const DifyChatbot = dynamic(() => import('@/components/DifyChatbot'), {
40-
ssr: false
41-
})
42-
const Analytics = dynamic(
43-
() =>
44-
import('@vercel/analytics/react').then(async m => {
45-
return m.Analytics
46-
}),
47-
{ ssr: false }
48-
)
49-
const MusicPlayer = dynamic(() => import('@/components/Player'), { ssr: false })
50-
const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false })
51-
const Gtag = dynamic(() => import('@/components/Gtag'), { ssr: false })
52-
const Busuanzi = dynamic(() => import('@/components/Busuanzi'), { ssr: false })
53-
const Messenger = dynamic(() => import('@/components/FacebookMessenger'), {
54-
ssr: false
55-
})
56-
const VConsole = dynamic(() => import('@/components/VConsole'), { ssr: false })
57-
const CustomContextMenu = dynamic(
58-
() => import('@/components/CustomContextMenu'),
59-
{ ssr: false }
60-
)
61-
const DisableCopy = dynamic(() => import('@/components/DisableCopy'), {
62-
ssr: false
63-
})
64-
const AdBlockDetect = dynamic(() => import('@/components/AdBlockDetect'), {
65-
ssr: false
66-
})
67-
const LoadingProgress = dynamic(() => import('@/components/LoadingProgress'), {
68-
ssr: false
69-
})
70-
const AosAnimation = dynamic(() => import('@/components/AOSAnimation'), {
71-
ssr: false
72-
})
73-
7415
/**
7516
* 各种插件脚本
7617
* @param {*} props
@@ -155,6 +96,7 @@ const ExternalPlugin = props => {
15596
}
15697
}
15798

99+
const router = useRouter()
158100
useEffect(() => {
159101
// 异步渲染谷歌广告
160102
if (ADSENSE_GOOGLE_ID) {
@@ -163,6 +105,11 @@ const ExternalPlugin = props => {
163105
}, 1000)
164106
}
165107

108+
// 映射url
109+
mapPageUrl(props?.allNavPages)
110+
}, [router])
111+
112+
useEffect(() => {
166113
// 执行注入脚本
167114
// eslint-disable-next-line no-eval
168115
eval(GLOBAL_JS)
@@ -389,4 +336,65 @@ const ExternalPlugin = props => {
389336
)
390337
}
391338

339+
const TwikooCommentCounter = dynamic(
340+
() => import('@/components/TwikooCommentCounter'),
341+
{ ssr: false }
342+
)
343+
const DebugPanel = dynamic(() => import('@/components/DebugPanel'), {
344+
ssr: false
345+
})
346+
const ThemeSwitch = dynamic(() => import('@/components/ThemeSwitch'), {
347+
ssr: false
348+
})
349+
const Fireworks = dynamic(() => import('@/components/Fireworks'), {
350+
ssr: false
351+
})
352+
const MouseFollow = dynamic(() => import('@/components/MouseFollow'), {
353+
ssr: false
354+
})
355+
const Nest = dynamic(() => import('@/components/Nest'), { ssr: false })
356+
const FlutteringRibbon = dynamic(
357+
() => import('@/components/FlutteringRibbon'),
358+
{ ssr: false }
359+
)
360+
const Ribbon = dynamic(() => import('@/components/Ribbon'), { ssr: false })
361+
const Sakura = dynamic(() => import('@/components/Sakura'), { ssr: false })
362+
const StarrySky = dynamic(() => import('@/components/StarrySky'), {
363+
ssr: false
364+
})
365+
const DifyChatbot = dynamic(() => import('@/components/DifyChatbot'), {
366+
ssr: false
367+
})
368+
const Analytics = dynamic(
369+
() =>
370+
import('@vercel/analytics/react').then(async m => {
371+
return m.Analytics
372+
}),
373+
{ ssr: false }
374+
)
375+
const MusicPlayer = dynamic(() => import('@/components/Player'), { ssr: false })
376+
const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false })
377+
const Gtag = dynamic(() => import('@/components/Gtag'), { ssr: false })
378+
const Busuanzi = dynamic(() => import('@/components/Busuanzi'), { ssr: false })
379+
const Messenger = dynamic(() => import('@/components/FacebookMessenger'), {
380+
ssr: false
381+
})
382+
const VConsole = dynamic(() => import('@/components/VConsole'), { ssr: false })
383+
const CustomContextMenu = dynamic(
384+
() => import('@/components/CustomContextMenu'),
385+
{ ssr: false }
386+
)
387+
const DisableCopy = dynamic(() => import('@/components/DisableCopy'), {
388+
ssr: false
389+
})
390+
const AdBlockDetect = dynamic(() => import('@/components/AdBlockDetect'), {
391+
ssr: false
392+
})
393+
const LoadingProgress = dynamic(() => import('@/components/LoadingProgress'), {
394+
ssr: false
395+
})
396+
const AosAnimation = dynamic(() => import('@/components/AOSAnimation'), {
397+
ssr: false
398+
})
399+
392400
export default ExternalPlugin

components/NotionPage.js

-27
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ const NotionPage = ({ post, className }) => {
4747
processDisableDatabaseUrl()
4848
}
4949

50-
// 若url是本站域名,则之间在当前窗口打开、不开新窗口
51-
processPageUrl()
52-
5350
/**
5451
* 放大查看图片时替换成高清图像
5552
*/
@@ -146,30 +143,6 @@ const processGalleryImg = zoom => {
146143
}, 800)
147144
}
148145

149-
/**
150-
* 处理页面内连接跳转
151-
* 如果链接就是当网站,则不打开新窗口访问
152-
*/
153-
const processPageUrl = () => {
154-
if (isBrowser) {
155-
const currentURL = window.location.origin + window.location.pathname
156-
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
157-
for (const anchorTag of allAnchorTags) {
158-
if (anchorTag?.target === '_blank') {
159-
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
160-
const hrefWithRelativeHash =
161-
currentURL.split('#')[0] + anchorTag.href.split('#')[1]
162-
163-
if (
164-
currentURL === hrefWithoutQueryHash ||
165-
currentURL === hrefWithRelativeHash
166-
) {
167-
anchorTag.target = '_self'
168-
}
169-
}
170-
}
171-
}
172-
}
173146
/**
174147
* 根据url参数自动滚动到指定区域
175148
*/

lib/notion/mapPageUrl.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { uuidToId } from 'notion-utils'
2+
import { checkStrIsNotionId, getLastPartOfUrl, isBrowser } from '../utils'
3+
4+
/**
5+
* 处理页面内连接跳转:
6+
* 1. 若是本站域名,则在当前窗口打开、不开新窗口
7+
* 2. 若是Notion笔记中的内链,尝试转换成博客中现有的文章地址
8+
*/
9+
export const mapPageUrl = allPages => {
10+
if (isBrowser) {
11+
const currentURL = window.location.origin + window.location.pathname
12+
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
13+
for (const anchorTag of allAnchorTags) {
14+
// 检查url
15+
if (anchorTag?.href) {
16+
// 如果url是一个Notion_id,尝试匹配成博客的文章内链
17+
const slug = getLastPartOfUrl(anchorTag.href)
18+
if (checkStrIsNotionId(slug)) {
19+
const slugPage = allPages?.find(page => uuidToId(page.id) === slug)
20+
if (slugPage) {
21+
anchorTag.href = slugPage?.href
22+
}
23+
}
24+
}
25+
26+
if (anchorTag?.target === '_blank') {
27+
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
28+
const hrefWithRelativeHash =
29+
currentURL.split('#')[0] + anchorTag.href.split('#')[1]
30+
31+
if (
32+
currentURL === hrefWithoutQueryHash ||
33+
currentURL === hrefWithRelativeHash
34+
) {
35+
anchorTag.target = '_self'
36+
}
37+
}
38+
}
39+
}
40+
}

lib/utils/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ export function checkContainHttp(str) {
9393
}
9494
}
9595

96+
// 检查一个字符串是否notionid : 32位,仅由数字英文构成
97+
export function checkStrIsNotionId(str) {
98+
if (!str) {
99+
return false
100+
}
101+
// 使用正则表达式进行匹配
102+
const regex = /^[a-zA-Z0-9]{32}$/
103+
return regex.test(str)
104+
}
105+
106+
// 截取url中最后一个 / 后面的内容
107+
export function getLastPartOfUrl(url) {
108+
if (!url) {
109+
return ''
110+
}
111+
// 找到最后一个斜杠的位置
112+
const lastSlashIndex = url.lastIndexOf('/')
113+
114+
// 如果找不到斜杠,则返回整个字符串
115+
if (lastSlashIndex === -1) {
116+
return url
117+
}
118+
119+
// 截取最后一个斜杠后面的内容
120+
const lastPart = url.substring(lastSlashIndex + 1)
121+
122+
return lastPart
123+
}
124+
96125
/**
97126
* 加载外部资源
98127
* @param url 地址 例如 https://xx.com/xx.js

0 commit comments

Comments
 (0)