Skip to content

Commit 59025e8

Browse files
committed
gitbook 主题fix最近阅读
1 parent 71701fa commit 59025e8

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

lib/db/getSiteData.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import getAllPageIds from '@/lib/notion/getAllPageIds'
55
import { getAllTags } from '@/lib/notion/getAllTags'
66
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
77
import getPageProperties, {
8-
adjustPageProperties
8+
adjustPageProperties
99
} from '@/lib/notion/getPageProperties'
1010
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
1111
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
1212
import { deepClone } from '@/lib/utils'
1313
import { idToUuid } from 'notion-utils'
1414
import { siteConfig } from '../config'
15-
import { extractLangId, extractLangPrefix } from '../utils/pageId'
15+
import { extractLangId, extractLangPrefix, getShortId } from '../utils/pageId'
1616

1717
export { getAllTags } from '../notion/getAllTags'
1818
export { getPost } from '../notion/getNotionPost'
@@ -100,16 +100,14 @@ function compressData(db) {
100100
// 清理多余的块
101101
if (db?.notice) {
102102
db.notice = cleanBlock(db?.notice)
103-
}
104-
if (db?.post) {
105-
db.post = cleanBlock(db?.post)
103+
delete db.notice?.id
106104
}
107105

108106
db.tagOptions = cleanIds(db?.tagOptions)
109107
db.categoryOptions = cleanIds(db?.categoryOptions)
110108
db.customMenu = cleanIds(db?.customMenu)
111109

112-
db.latestPosts = cleanIds(db?.latestPosts)
110+
db.latestPosts = shortenIds(db?.latestPosts)
113111
db.allNavPages = shortenIds(db?.allNavPages)
114112
// db.allPages = cleanBlocks(db?.allPages)
115113

@@ -125,7 +123,7 @@ function shortenIds(items) {
125123
if (items && Array.isArray(items)) {
126124
return deepClone(
127125
items.map(item => {
128-
item.short_id = getFirstPart(item.id)
126+
item.short_id = getShortId(item.id)
129127
delete item.id
130128
return item
131129
})
@@ -134,16 +132,6 @@ function shortenIds(items) {
134132
return items
135133
}
136134

137-
function getFirstPart(uuid) {
138-
if (!uuid || uuid.indexOf('-') < 0) {
139-
return uuid
140-
}
141-
// 找到第一个 '-' 的位置
142-
const index = uuid.indexOf('-')
143-
// 截取从开始到第一个 '-' 之前的部分
144-
return uuid.substring(0, index)
145-
}
146-
147135
/**
148136
* 清理一组数据的id
149137
* @param {*} items
@@ -167,7 +155,7 @@ function cleanIds(items) {
167155
function cleanBlock(item) {
168156
const post = deepClone(item)
169157
const pageBlock = post?.blockMap?.block
170-
delete post?.id
158+
// delete post?.id
171159
// delete post?.blockMap?.collection
172160

173161
if (pageBlock) {

lib/utils/pageId.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ function extractLangId(str) {
3030
}
3131
}
3232

33-
module.exports = { extractLangPrefix, extractLangId }
33+
/**
34+
* 列表中用过来区分page只需要端的id足够
35+
*/
36+
37+
function getShortId(uuid) {
38+
if (!uuid || uuid.indexOf('-') < 0) {
39+
return uuid
40+
}
41+
// 找到第一个 '-' 的位置
42+
const index = uuid.indexOf('-')
43+
// 截取从开始到第一个 '-' 之前的部分
44+
return uuid.substring(0, index)
45+
}
46+
47+
module.exports = { extractLangPrefix, extractLangId, getShortId }

pages/[prefix]/[slug]/[...suffix].js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export async function getStaticProps({
9292
}
9393

9494
// 文章内容加载
95-
if (!props?.posts?.blockMap) {
95+
if (!props?.post?.blockMap) {
9696
props.post.blockMap = await getPostBlocks(props.post.id, from)
9797
}
9898
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)

pages/[prefix]/[slug]/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
8181
}
8282

8383
// 文章内容加载
84-
if (!props?.posts?.blockMap) {
84+
if (!props?.post?.blockMap) {
8585
props.post.blockMap = await getPostBlocks(props.post.id, from)
8686
}
8787
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)

pages/[prefix]/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export async function getStaticProps({ params: { prefix }, locale }) {
145145
}
146146

147147
// 文章内容加载
148-
if (!props?.posts?.blockMap) {
148+
if (!props?.post?.blockMap) {
149149
props.post.blockMap = await getPostBlocks(props.post.id, from)
150150
}
151151

themes/gitbook/index.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ShareBar from '@/components/ShareBar'
99
import { siteConfig } from '@/lib/config'
1010
import { useGlobal } from '@/lib/global'
1111
import { isBrowser } from '@/lib/utils'
12+
import { getShortId } from '@/lib/utils/pageId'
1213
import { Transition } from '@headlessui/react'
1314
import dynamic from 'next/dynamic'
1415
import Link from 'next/link'
@@ -54,14 +55,14 @@ function getNavPagesWithLatest(allNavPages, latestPosts, post) {
5455
localStorage.getItem('post_read_time') || '{}'
5556
)
5657
if (post) {
57-
postReadTime[post.id] = new Date().getTime()
58+
postReadTime[getShortId(post.id)] = new Date().getTime()
5859
}
5960
// 更新
6061
localStorage.setItem('post_read_time', JSON.stringify(postReadTime))
6162

6263
return allNavPages?.map(item => {
6364
const res = {
64-
id: item.id,
65+
short_id: item.short_id,
6566
title: item.title || '',
6667
pageCoverThumbnail: item.pageCoverThumbnail || '',
6768
category: item.category || null,
@@ -74,9 +75,9 @@ function getNavPagesWithLatest(allNavPages, latestPosts, post) {
7475
}
7576
// 属于最新文章通常6篇 && (无阅读记录 || 最近更新时间大于上次阅读时间)
7677
if (
77-
latestPosts.some(post => post.id === item.id) &&
78-
(!postReadTime[item.id] ||
79-
postReadTime[item.id] < new Date(item.lastEditedDate).getTime())
78+
latestPosts.some(post => item?.short_id === post?.short_id) &&
79+
(!postReadTime[item.short_id] ||
80+
postReadTime[item.short_id] < new Date(item.lastEditedDate).getTime())
8081
) {
8182
return { ...res, isLatest: true }
8283
} else {
@@ -412,11 +413,9 @@ const LayoutArchive = props => {
412413
*/
413414
const Layout404 = props => {
414415
return (
415-
<>
416-
<div className='w-full h-96 py-80 flex justify-center items-center'>
417-
404 Not found.
418-
</div>
419-
</>
416+
<div className='w-full h-96 py-80 flex justify-center items-center'>
417+
404 Not found.
418+
</div>
420419
)
421420
}
422421

0 commit comments

Comments
 (0)