From 59025e801847aebc643613dbbfe4a9d1fac0de6a Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Thu, 16 May 2024 15:29:02 +0800 Subject: [PATCH] =?UTF-8?q?gitbook=20=E4=B8=BB=E9=A2=98fix=E6=9C=80?= =?UTF-8?q?=E8=BF=91=E9=98=85=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/db/getSiteData.js | 24 ++++++------------------ lib/utils/pageId.js | 16 +++++++++++++++- pages/[prefix]/[slug]/[...suffix].js | 2 +- pages/[prefix]/[slug]/index.js | 2 +- pages/[prefix]/index.js | 2 +- themes/gitbook/index.js | 19 +++++++++---------- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index a4f042bd549..f113eb20593 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -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' @@ -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) @@ -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 }) @@ -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 @@ -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) { diff --git a/lib/utils/pageId.js b/lib/utils/pageId.js index e3c648e1649..c963df28b62 100644 --- a/lib/utils/pageId.js +++ b/lib/utils/pageId.js @@ -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 } diff --git a/pages/[prefix]/[slug]/[...suffix].js b/pages/[prefix]/[slug]/[...suffix].js index 2050e81d8d0..475372666ca 100644 --- a/pages/[prefix]/[slug]/[...suffix].js +++ b/pages/[prefix]/[slug]/[...suffix].js @@ -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) diff --git a/pages/[prefix]/[slug]/index.js b/pages/[prefix]/[slug]/index.js index 2c844ebabe5..2b414ce4e49 100644 --- a/pages/[prefix]/[slug]/index.js +++ b/pages/[prefix]/[slug]/index.js @@ -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) diff --git a/pages/[prefix]/index.js b/pages/[prefix]/index.js index 469ca617cdd..973ce32153c 100644 --- a/pages/[prefix]/index.js +++ b/pages/[prefix]/index.js @@ -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) } diff --git a/themes/gitbook/index.js b/themes/gitbook/index.js index d70e152e003..c81110b0ce7 100644 --- a/themes/gitbook/index.js +++ b/themes/gitbook/index.js @@ -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' @@ -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, @@ -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 { @@ -412,11 +413,9 @@ const LayoutArchive = props => { */ const Layout404 = props => { return ( - <> -
- 404 Not found. -
- +
+ 404 Not found. +
) }