Skip to content

Commit a6bb38c

Browse files
committed
2 parents 0610d62 + 251aa21 commit a6bb38c

File tree

136 files changed

+3250
-2407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+3250
-2407
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.4.6
2+
NEXT_PUBLIC_VERSION=4.5.0
33

44

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

components/NotionPage.js

+64-32
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ const Equation = dynamic(
2626
{ ssr: false }
2727
)
2828

29-
const Pdf = dynamic(() => import('react-notion-x/build/third-party/pdf').then(m => m.Pdf), {
30-
ssr: false
31-
})
29+
const Pdf = dynamic(
30+
() => import('react-notion-x/build/third-party/pdf').then(m => m.Pdf),
31+
{
32+
ssr: false
33+
}
34+
)
3235

3336
// https://github.com/txs
3437
// import PrismMac from '@/components/PrismMac'
@@ -46,13 +49,25 @@ const TweetEmbed = dynamic(() => import('react-tweet-embed'), {
4649
/**
4750
* 文内google广告
4851
*/
49-
const AdEmbed = dynamic(() => import('@/components/GoogleAdsense').then(m => m.AdEmbed), { ssr: true })
52+
const AdEmbed = dynamic(
53+
() => import('@/components/GoogleAdsense').then(m => m.AdEmbed),
54+
{ ssr: true }
55+
)
5056

51-
const Collection = dynamic(() => import('react-notion-x/build/third-party/collection').then(m => m.Collection), {
52-
ssr: true
53-
})
57+
const Collection = dynamic(
58+
() =>
59+
import('react-notion-x/build/third-party/collection').then(
60+
m => m.Collection
61+
),
62+
{
63+
ssr: true
64+
}
65+
)
5466

55-
const Modal = dynamic(() => import('react-notion-x/build/third-party/modal').then(m => m.Modal), { ssr: false })
67+
const Modal = dynamic(
68+
() => import('react-notion-x/build/third-party/modal').then(m => m.Modal),
69+
{ ssr: false }
70+
)
5671

5772
const Tweet = ({ id }) => {
5873
return <TweetEmbed tweetId={id} />
@@ -83,19 +98,22 @@ const NotionPage = ({ post, className }) => {
8398
// 将相册gallery下的图片加入放大功能
8499
if (siteConfig('POST_DISABLE_GALLERY_CLICK')) {
85100
setTimeout(() => {
86-
if (isBrowser) {
87-
const imgList = document?.querySelectorAll('.notion-collection-card-cover img')
88-
if (imgList && zoomRef.current) {
89-
for (let i = 0; i < imgList.length; i++) {
90-
zoomRef.current.attach(imgList[i])
91-
}
92-
}
101+
const imgList = document?.querySelectorAll(
102+
'.notion-asset-wrapper-image img'
103+
)
104+
105+
console.log('放大', imgList)
93106

94-
const cards = document.getElementsByClassName('notion-collection-card')
95-
for (const e of cards) {
96-
e.removeAttribute('href')
107+
if (imgList && zoomRef.current) {
108+
for (let i = 0; i < imgList.length; i++) {
109+
zoomRef.current.attach(imgList[i])
97110
}
98111
}
112+
113+
const cards = document.getElementsByClassName('notion-collection-card')
114+
for (const e of cards) {
115+
e.removeAttribute('href')
116+
}
99117
}, 800)
100118
}
101119

@@ -108,10 +126,16 @@ const NotionPage = ({ post, className }) => {
108126
const allAnchorTags = document.getElementsByTagName('a') // 或者使用 document.querySelectorAll('a') 获取 NodeList
109127
for (const anchorTag of allAnchorTags) {
110128
if (anchorTag?.target === '_blank') {
111-
const hrefWithoutQueryHash = anchorTag.href.split('?')[0].split('#')[0]
112-
const hrefWithRelativeHash = currentURL.split('#')[0] + anchorTag.href.split('#')[1]
113-
114-
if (currentURL === hrefWithoutQueryHash || currentURL === hrefWithRelativeHash) {
129+
const hrefWithoutQueryHash = anchorTag.href
130+
.split('?')[0]
131+
.split('#')[0]
132+
const hrefWithRelativeHash =
133+
currentURL.split('#')[0] + anchorTag.href.split('#')[1]
134+
135+
if (
136+
currentURL === hrefWithoutQueryHash ||
137+
currentURL === hrefWithRelativeHash
138+
) {
115139
anchorTag.target = '_self'
116140
}
117141
}
@@ -121,36 +145,44 @@ const NotionPage = ({ post, className }) => {
121145
// 放大图片:调整图片质量
122146
const observer = new MutationObserver((mutationsList, observer) => {
123147
mutationsList.forEach(mutation => {
124-
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
148+
if (
149+
mutation.type === 'attributes' &&
150+
mutation.attributeName === 'class'
151+
) {
125152
if (mutation.target.classList.contains('medium-zoom-image--opened')) {
126153
// 等待动画完成后替换为更高清的图像
127154
setTimeout(() => {
128155
// 获取该元素的 src 属性
129156
const src = mutation?.target?.getAttribute('src')
130157
// 替换为更高清的图像
131-
mutation?.target?.setAttribute('src', compressImage(src, siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200)))
158+
mutation?.target?.setAttribute(
159+
'src',
160+
compressImage(src, siteConfig('IMAGE_ZOOM_IN_WIDTH', 1200))
161+
)
132162
}, 800)
133163
}
134164
}
135165
})
136166
})
137167

138168
// 监视整个文档中的元素和属性的变化
139-
observer.observe(document.body, { attributes: true, subtree: true, attributeFilter: ['class'] })
169+
observer.observe(document.body, {
170+
attributes: true,
171+
subtree: true,
172+
attributeFilter: ['class']
173+
})
140174

141175
return () => {
142176
observer.disconnect()
143177
}
144-
}, [])
145-
146-
if (!post || !post.blockMap) {
147-
return <>{post?.summary || ''}</>
148-
}
178+
}, [post])
149179

150180
return (
151-
<div id='notion-article' className={`mx-auto overflow-hidden ${className || ''}`}>
181+
<div
182+
id='notion-article'
183+
className={`mx-auto overflow-hidden ${className || ''}`}>
152184
<NotionRenderer
153-
recordMap={post.blockMap}
185+
recordMap={post?.blockMap}
154186
mapPageUrl={mapPageUrl}
155187
mapImageUrl={mapImgUrl}
156188
components={{

lib/db/getSiteData.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,11 @@ function getCustomNav({ allPages }) {
156156
if (allPages && allPages.length > 0) {
157157
allPages.forEach(p => {
158158
p.to = p.slug
159-
if (p?.slug?.indexOf('http') === 0) {
160-
p.target = '_blank'
161-
} else {
162-
p.target = '_self'
163-
if (p?.slug?.indexOf('/') !== 0) {
164-
p.to = '/' + p.slug
165-
}
166-
}
167159
customNav.push({
168160
icon: p.icon || null,
169161
name: p.title,
170-
to: p.slug,
171-
target: '_blank',
162+
href: p.href,
163+
target: p.target,
172164
show: true
173165
})
174166
})
@@ -192,15 +184,6 @@ function getCustomMenu({ collectionData, NOTION_CONFIG }) {
192184
if (menuPages && menuPages.length > 0) {
193185
menuPages.forEach(e => {
194186
e.show = true
195-
if (e?.slug?.indexOf('http') === 0) {
196-
e.target = '_blank'
197-
e.to = e.slug
198-
} else {
199-
e.target = '_self'
200-
if (e?.slug?.indexOf('http') !== 0 && e?.slug?.indexOf('/') !== 0) {
201-
e.to = '/' + e.slug
202-
}
203-
}
204187
if (e.type === BLOG.NOTION_PROPERTY_NAME.type_menu) {
205188
menus.push(e)
206189
} else if (e.type === BLOG.NOTION_PROPERTY_NAME.type_sub_menu) {
@@ -312,6 +295,7 @@ export function getNavPages({ allPages }) {
312295
tags: item.tags || null,
313296
summary: item.summary || null,
314297
slug: item.slug,
298+
href: item.href,
315299
pageIcon: item.pageIcon || '',
316300
lastEditedDate: item.lastEditedDate,
317301
publishDate: item.publishDate,

lib/notion/getPageProperties.js

+31-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getDateValue, getTextContent } from 'notion-utils'
44
import formatDate from '../utils/formatDate'
55
// import { createHash } from 'crypto'
66
import md5 from 'js-md5'
7+
import { checkContainHttp, sliceUrlFromHttp } from '../utils'
78
import { mapImgUrl } from './mapImage'
89

910
/**
@@ -106,7 +107,7 @@ export default async function getPageProperties(
106107
properties.pageCover = mapImgUrl(value?.format?.page_cover, value) ?? ''
107108
properties.pageCoverThumbnail =
108109
mapImgUrl(value?.format?.page_cover, value, 'block') ?? ''
109-
properties.ext = converToJSON(properties?.ext)
110+
properties.ext = convertToJSON(properties?.ext)
110111
properties.content = value.content ?? []
111112
properties.tagItems =
112113
properties?.tags?.map(tag => {
@@ -118,27 +119,43 @@ export default async function getPageProperties(
118119
delete properties.content
119120

120121
// 处理URL
122+
// 1.按照用户配置的URL_PREFIX 转换一下slug
123+
// 2.为文章添加一个href字段,存储最终调整的路径
121124
if (properties.type === 'Post') {
122-
properties.slug = BLOG.POST_URL_PREFIX
123-
? generateCustomizeUrl(properties)
124-
: properties.slug ?? properties.id
125+
if (BLOG.POST_URL_PREFIX) {
126+
properties.slug = generateCustomizeSlug(properties)
127+
}
128+
properties.href = properties.slug ?? properties.id
125129
} else if (properties.type === 'Page') {
126-
properties.slug = properties.slug ?? properties.id
130+
properties.href = properties.slug ?? properties.id
127131
} else if (properties.type === 'Menu' || properties.type === 'SubMenu') {
128132
// 菜单路径为空、作为可展开菜单使用
129-
properties.to = properties.slug ?? '#'
133+
properties.href = properties.slug ?? '#'
130134
properties.name = properties.title ?? ''
131135
}
132136

133137
// 开启伪静态路径
134138
if (JSON.parse(BLOG.PSEUDO_STATIC)) {
135139
if (
136-
!properties?.slug?.endsWith('.html') &&
137-
!properties?.slug?.startsWith('http')
140+
!properties?.href?.endsWith('.html') &&
141+
!properties?.href?.startsWith('http')
138142
) {
139-
properties.slug += '.html'
143+
properties.href += '.html'
140144
}
141145
}
146+
147+
// 最终检查超链接
148+
properties.href = checkContainHttp(properties?.href)
149+
? sliceUrlFromHttp(properties?.href)
150+
: `/${properties.href}`
151+
152+
// 设置链接在页内或新页面打开
153+
if (properties.href?.indexOf('http') === 0) {
154+
properties.target = '_blank'
155+
} else {
156+
properties.target = '_self'
157+
}
158+
142159
// 密码字段md5
143160
properties.password = properties.password
144161
? md5(properties.slug + properties.password)
@@ -151,7 +168,7 @@ export default async function getPageProperties(
151168
* @param {*} str
152169
* @returns
153170
*/
154-
function converToJSON(str) {
171+
function convertToJSON(str) {
155172
if (!str) {
156173
return {}
157174
}
@@ -188,11 +205,11 @@ function mapProperties(properties) {
188205
/**
189206
* 获取自定义URL
190207
* 可以根据变量生成URL
191-
* 支持:%year%/%month%/%day%/%slug%
208+
* 支持:%category%/%year%/%month%/%day%/%slug%
192209
* @param {*} postProperties
193210
* @returns
194211
*/
195-
function generateCustomizeUrl(postProperties) {
212+
function generateCustomizeSlug(postProperties) {
196213
let fullPrefix = ''
197214
const allSlugPatterns = BLOG.POST_URL_PREFIX.split('/')
198215
allSlugPatterns.forEach((pattern, idx) => {
@@ -210,6 +227,8 @@ function generateCustomizeUrl(postProperties) {
210227
fullPrefix += String(formatPostCreatedDate.getUTCDate()).padStart(2, 0)
211228
} else if (pattern === '%slug%') {
212229
fullPrefix += postProperties.slug ?? postProperties.id
230+
} else if (pattern === '%category%' && postProperties?.category) {
231+
fullPrefix += postProperties.category
213232
} else if (!pattern.includes('%')) {
214233
fullPrefix += pattern
215234
} else {

0 commit comments

Comments
 (0)