Skip to content

Commit 741528e

Browse files
authored
Merge pull request tangly1024#2409 from tangly1024/release/4.5.2
Release/4.5.2
2 parents 04cb62a + 3f80c4e commit 741528e

File tree

7 files changed

+180
-94
lines changed

7 files changed

+180
-94
lines changed

.env.local

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables
2-
NEXT_PUBLIC_VERSION=4.5.1
2+
NEXT_PUBLIC_VERSION=4.5.2
33

44

55
# 可在此添加环境变量,去掉最左边的(# )注释即可

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/getPageProperties.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import formatDate from '../utils/formatDate'
55
// import { createHash } from 'crypto'
66
import md5 from 'js-md5'
77
import { siteConfig } from '../config'
8-
import { checkContainHttp, sliceUrlFromHttp } from '../utils'
8+
import {
9+
checkContainHttp,
10+
convertUrlStartWithOneSlash,
11+
sliceUrlFromHttp
12+
} from '../utils'
913
import { mapImgUrl } from './mapImage'
1014

1115
/**
@@ -191,10 +195,10 @@ export function adjustPageProperties(properties, NOTION_CONFIG) {
191195
}
192196
}
193197

194-
// 最终检查超链接
198+
// 检查处理外链
195199
properties.href = checkContainHttp(properties?.href)
196200
? sliceUrlFromHttp(properties?.href)
197-
: `/${properties.href}`
201+
: convertUrlStartWithOneSlash(properties?.href)
198202

199203
// 设置链接在页内或新页面打开
200204
if (properties.href?.indexOf('http') === 0) {
@@ -224,6 +228,12 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
224228
NOTION_CONFIG
225229
).split('/')
226230

231+
const POST_URL_PREFIX_MAPPING_CATEGORY = siteConfig(
232+
'POST_URL_PREFIX_MAPPING_CATEGORY',
233+
{},
234+
NOTION_CONFIG
235+
)
236+
227237
allSlugPatterns.forEach((pattern, idx) => {
228238
if (pattern === '%year%' && postProperties?.publishDay) {
229239
const formatPostCreatedDate = new Date(postProperties?.publishDay)
@@ -240,7 +250,13 @@ function generateCustomizeSlug(postProperties, NOTION_CONFIG) {
240250
} else if (pattern === '%slug%') {
241251
fullPrefix += postProperties.slug ?? postProperties.id
242252
} else if (pattern === '%category%' && postProperties?.category) {
243-
fullPrefix += postProperties.category
253+
let categoryPrefix = postProperties.category
254+
// 允许映射分类名,通常用来将中文分类映射成英文,美化url.
255+
if (POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]) {
256+
categoryPrefix =
257+
POST_URL_PREFIX_MAPPING_CATEGORY[postProperties?.category]
258+
}
259+
fullPrefix += categoryPrefix
244260
} else if (!pattern.includes('%')) {
245261
fullPrefix += pattern
246262
} else {

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

+49
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ export function sliceUrlFromHttp(str) {
6161
}
6262
}
6363

64+
/**
65+
* 将相对路径的url test 转为绝对路径 /test
66+
* 判断url如果不是以 /开头,则拼接一个 /
67+
* 同时如果开头有重复的多个 // ,则只保留一个
68+
* @param {*} str
69+
*/
70+
export function convertUrlStartWithOneSlash(str) {
71+
if (!str) {
72+
return '#'
73+
}
74+
// 判断url是否以 / 开头
75+
if (!str.startsWith('/')) {
76+
// 如果不是,则在前面拼接一个 /
77+
str = '/' + str
78+
}
79+
// 移除开头的多个连续斜杠,只保留一个
80+
str = str.replace(/\/+/g, '/')
81+
return str
82+
}
83+
6484
// 检查是否外链
6585
export function checkContainHttp(str) {
6686
// 检查字符串是否包含http
@@ -73,6 +93,35 @@ export function checkContainHttp(str) {
7393
}
7494
}
7595

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+
76125
/**
77126
* 加载外部资源
78127
* @param url 地址 例如 https://xx.com/xx.js

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notion-next",
3-
"version": "4.5.1",
3+
"version": "4.5.2",
44
"homepage": "https://github.com/tangly1024/NotionNext.git",
55
"license": "MIT",
66
"repository": {

0 commit comments

Comments
 (0)