Skip to content

Commit

Permalink
gitbook 主题fix最近阅读
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 16, 2024
1 parent 71701fa commit 59025e8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
24 changes: 6 additions & 18 deletions lib/db/getSiteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import getAllPageIds from '@/lib/notion/getAllPageIds'
import { getAllTags } from '@/lib/notion/getAllTags'
import { getConfigMapFromConfigPage } from '@/lib/notion/getNotionConfig'
import getPageProperties, {
adjustPageProperties
adjustPageProperties
} from '@/lib/notion/getPageProperties'
import { fetchInBatches, getPage } from '@/lib/notion/getPostBlocks'
import { compressImage, mapImgUrl } from '@/lib/notion/mapImage'
import { deepClone } from '@/lib/utils'
import { idToUuid } from 'notion-utils'
import { siteConfig } from '../config'
import { extractLangId, extractLangPrefix } from '../utils/pageId'
import { extractLangId, extractLangPrefix, getShortId } from '../utils/pageId'

export { getAllTags } from '../notion/getAllTags'
export { getPost } from '../notion/getNotionPost'
Expand Down Expand Up @@ -100,16 +100,14 @@ function compressData(db) {
// 清理多余的块
if (db?.notice) {
db.notice = cleanBlock(db?.notice)
}
if (db?.post) {
db.post = cleanBlock(db?.post)
delete db.notice?.id
}

db.tagOptions = cleanIds(db?.tagOptions)
db.categoryOptions = cleanIds(db?.categoryOptions)
db.customMenu = cleanIds(db?.customMenu)

db.latestPosts = cleanIds(db?.latestPosts)
db.latestPosts = shortenIds(db?.latestPosts)
db.allNavPages = shortenIds(db?.allNavPages)
// db.allPages = cleanBlocks(db?.allPages)

Expand All @@ -125,7 +123,7 @@ function shortenIds(items) {
if (items && Array.isArray(items)) {
return deepClone(
items.map(item => {
item.short_id = getFirstPart(item.id)
item.short_id = getShortId(item.id)
delete item.id
return item
})
Expand All @@ -134,16 +132,6 @@ function shortenIds(items) {
return items
}

function getFirstPart(uuid) {
if (!uuid || uuid.indexOf('-') < 0) {
return uuid
}
// 找到第一个 '-' 的位置
const index = uuid.indexOf('-')
// 截取从开始到第一个 '-' 之前的部分
return uuid.substring(0, index)
}

/**
* 清理一组数据的id
* @param {*} items
Expand All @@ -167,7 +155,7 @@ function cleanIds(items) {
function cleanBlock(item) {
const post = deepClone(item)
const pageBlock = post?.blockMap?.block
delete post?.id
// delete post?.id
// delete post?.blockMap?.collection

if (pageBlock) {
Expand Down
16 changes: 15 additions & 1 deletion lib/utils/pageId.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ function extractLangId(str) {
}
}

module.exports = { extractLangPrefix, extractLangId }
/**
* 列表中用过来区分page只需要端的id足够
*/

function getShortId(uuid) {
if (!uuid || uuid.indexOf('-') < 0) {
return uuid
}
// 找到第一个 '-' 的位置
const index = uuid.indexOf('-')
// 截取从开始到第一个 '-' 之前的部分
return uuid.substring(0, index)
}

module.exports = { extractLangPrefix, extractLangId, getShortId }
2 changes: 1 addition & 1 deletion pages/[prefix]/[slug]/[...suffix].js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function getStaticProps({
}

// 文章内容加载
if (!props?.posts?.blockMap) {
if (!props?.post?.blockMap) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
Expand Down
2 changes: 1 addition & 1 deletion pages/[prefix]/[slug]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function getStaticProps({ params: { prefix, slug }, locale }) {
}

// 文章内容加载
if (!props?.posts?.blockMap) {
if (!props?.post?.blockMap) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}
// 生成全文索引 && JSON.parse(BLOG.ALGOLIA_RECREATE_DATA)
Expand Down
2 changes: 1 addition & 1 deletion pages/[prefix]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function getStaticProps({ params: { prefix }, locale }) {
}

// 文章内容加载
if (!props?.posts?.blockMap) {
if (!props?.post?.blockMap) {
props.post.blockMap = await getPostBlocks(props.post.id, from)
}

Expand Down
19 changes: 9 additions & 10 deletions themes/gitbook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ShareBar from '@/components/ShareBar'
import { siteConfig } from '@/lib/config'
import { useGlobal } from '@/lib/global'
import { isBrowser } from '@/lib/utils'
import { getShortId } from '@/lib/utils/pageId'
import { Transition } from '@headlessui/react'
import dynamic from 'next/dynamic'
import Link from 'next/link'
Expand Down Expand Up @@ -54,14 +55,14 @@ function getNavPagesWithLatest(allNavPages, latestPosts, post) {
localStorage.getItem('post_read_time') || '{}'
)
if (post) {
postReadTime[post.id] = new Date().getTime()
postReadTime[getShortId(post.id)] = new Date().getTime()
}
// 更新
localStorage.setItem('post_read_time', JSON.stringify(postReadTime))

return allNavPages?.map(item => {
const res = {
id: item.id,
short_id: item.short_id,
title: item.title || '',
pageCoverThumbnail: item.pageCoverThumbnail || '',
category: item.category || null,
Expand All @@ -74,9 +75,9 @@ function getNavPagesWithLatest(allNavPages, latestPosts, post) {
}
// 属于最新文章通常6篇 && (无阅读记录 || 最近更新时间大于上次阅读时间)
if (
latestPosts.some(post => post.id === item.id) &&
(!postReadTime[item.id] ||
postReadTime[item.id] < new Date(item.lastEditedDate).getTime())
latestPosts.some(post => item?.short_id === post?.short_id) &&
(!postReadTime[item.short_id] ||
postReadTime[item.short_id] < new Date(item.lastEditedDate).getTime())
) {
return { ...res, isLatest: true }
} else {
Expand Down Expand Up @@ -412,11 +413,9 @@ const LayoutArchive = props => {
*/
const Layout404 = props => {
return (
<>
<div className='w-full h-96 py-80 flex justify-center items-center'>
404 Not found.
</div>
</>
<div className='w-full h-96 py-80 flex justify-center items-center'>
404 Not found.
</div>
)
}

Expand Down

0 comments on commit 59025e8

Please sign in to comment.