From 1614a6ed03c6f9b136be6b23d1370acea3ba3a9e Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 28 May 2024 11:53:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=A9=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E6=8C=89=E7=85=A7=E6=96=87=E7=AB=A0=E6=95=B0=E9=87=8F=E5=80=92?= =?UTF-8?q?=E5=BA=8F=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 1 + lib/db/getSiteData.js | 20 ++++++++++++++------ lib/notion/getAllCategories.js | 12 +++++++++--- lib/notion/getAllTags.js | 25 ++++++++++++++++++++----- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/blog.config.js b/blog.config.js index 7c5faf6c..12663fcf 100644 --- a/blog.config.js +++ b/blog.config.js @@ -13,6 +13,7 @@ const BLOG = { APPEARANCE: process.env.NEXT_PUBLIC_APPEARANCE || 'light', // ['light', 'dark', 'auto'], // light 日间模式 , dark夜间模式, auto根据时间和主题自动夜间模式 APPEARANCE_DARK_TIME: process.env.NEXT_PUBLIC_APPEARANCE_DARK_TIME || [18, 6], // 夜间模式起至时间,false时关闭根据时间自动切换夜间模式 + TAG_SORT_BY_COUNT: true, // 标签是否按照文章数量倒序排列,文章多的标签排在前。 IS_TAG_COLOR_DISTINGUISHED: process.env.NEXT_PUBLIC_IS_TAG_COLOR_DISTINGUISHED === 'true' || true, // 对于名称相同的tag是否区分tag的颜色 diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index 6bbe4bc9..fb916374 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -5,7 +5,7 @@ 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' @@ -77,15 +77,17 @@ export async function getNotionPageData({ pageId, from }) { } // 返回给前端的数据做处理 - return compressData(deepClone(data)) + return handleDataBeforeReturn(deepClone(data)) } /** - * 减少返回给前端的数据 - * 并脱敏 + * 返回给浏览器前端的数据处理 + * 适当脱敏 + * 减少体积 + * 其它处理 * @param {*} db */ -function compressData(db) { +function handleDataBeforeReturn(db) { // 清理多余数据 delete db.block delete db.schema @@ -545,11 +547,17 @@ async function getDataBaseInfoByNotionAPI({ pageId, from }) { ) })?.[0] ) + // 所有分类 const categoryOptions = getAllCategories({ allPages, categoryOptions: getCategoryOptions(schema) }) - const tagOptions = getAllTags({ allPages, tagOptions: getTagOptions(schema) }) + // 所有标签 + const tagOptions = getAllTags({ + allPages, + tagOptions: getTagOptions(schema), + NOTION_CONFIG + }) // 旧的菜单 const customNav = getCustomNav({ allPages: collectionData.filter( diff --git a/lib/notion/getAllCategories.js b/lib/notion/getAllCategories.js index 97c8ab3a..c7e66ba2 100644 --- a/lib/notion/getAllCategories.js +++ b/lib/notion/getAllCategories.js @@ -4,7 +4,7 @@ import { isIterable } from '../utils' * 获取所有文章的标签 * @param allPosts * @param sliceCount 默认截取数量为12,若为0则返回全部 - * @param tagOptions tags的下拉选项 + * @param categoryOptions categories的下拉选项 * @returns {Promise<{}|*[]>} */ @@ -13,8 +13,14 @@ import { isIterable } from '../utils' * @param allPosts * @returns {Promise<{}|*[]>} */ -export function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) { - const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') +export function getAllCategories({ + allPages, + categoryOptions, + sliceCount = 0 +}) { + const allPosts = allPages?.filter( + page => page.type === 'Post' && page.status === 'Published' + ) if (!allPosts || !categoryOptions) { return [] } diff --git a/lib/notion/getAllTags.js b/lib/notion/getAllTags.js index 4c7559b9..cac8dac7 100644 --- a/lib/notion/getAllTags.js +++ b/lib/notion/getAllTags.js @@ -1,5 +1,5 @@ +import { siteConfig } from '../config' import { isIterable } from '../utils' -import BLOG from '@/blog.config' /** * 获取所有文章的标签 @@ -8,8 +8,15 @@ import BLOG from '@/blog.config' * @param tagOptions tags的下拉选项 * @returns {Promise<{}|*[]>} */ -export function getAllTags({ allPages, sliceCount = 0, tagOptions }) { - const allPosts = allPages?.filter(page => page.type === 'Post' && page.status === 'Published') +export function getAllTags({ + allPages, + sliceCount = 0, + tagOptions, + NOTION_CONFIG +}) { + const allPosts = allPages?.filter( + page => page.type === 'Post' && page.status === 'Published' + ) if (!allPosts || !tagOptions) { return [] @@ -27,7 +34,12 @@ export function getAllTags({ allPages, sliceCount = 0, tagOptions }) { }) const list = [] - const { IS_TAG_COLOR_DISTINGUISHED } = BLOG + const IS_TAG_COLOR_DISTINGUISHED = siteConfig( + 'IS_TAG_COLOR_DISTINGUISHED', + false, + NOTION_CONFIG + ) + const TAG_SORT_BY_COUNT = siteConfig('TAG_SORT_BY_COUNT', true, NOTION_CONFIG) if (isIterable(tagOptions)) { if (!IS_TAG_COLOR_DISTINGUISHED) { // 如果不区分颜色, 那么不同颜色相同名称的tag当做同一种tag @@ -52,7 +64,10 @@ export function getAllTags({ allPages, sliceCount = 0, tagOptions }) { } // 按照数量排序 - // list.sort((a, b) => b.count - a.count) + if (TAG_SORT_BY_COUNT) { + list.sort((a, b) => b.count - a.count) + } + if (sliceCount && sliceCount > 0) { return list.slice(0, sliceCount) } else {