From 4b653688a8532584c5d776c297d8bd3169f5f83b Mon Sep 17 00:00:00 2001 From: tangly Date: Sat, 12 Nov 2022 22:37:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=87=E7=AD=BE=E3=80=81=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getAllCategories.js | 49 ++++++++++++++++++++++++++++++++++ lib/notion/getNotionData.js | 47 +++----------------------------- pages/archive/index.js | 6 ++--- pages/category/index.js | 8 +++--- pages/tag/[tag]/page/[page].js | 2 +- pages/tag/index.js | 4 +++ themes/next/LayoutCategory.js | 9 +++++-- themes/next/LayoutTag.js | 9 +++++-- 8 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 lib/notion/getAllCategories.js diff --git a/lib/notion/getAllCategories.js b/lib/notion/getAllCategories.js new file mode 100644 index 00000000..2225b965 --- /dev/null +++ b/lib/notion/getAllCategories.js @@ -0,0 +1,49 @@ +import { isIterable } from '../utils' + +/** + * 获取所有文章的标签 + * @param allPosts + * @param sliceCount 默认截取数量为12,若为0则返回全部 + * @param tagOptions tags的下拉选项 + * @returns {Promise<{}|*[]>} + */ + +/** + * 获取所有文章的分类 + * @param allPosts + * @returns {Promise<{}|*[]>} + */ +export function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) { + const allPosts = allPages.filter(page => page.type === 'Post') + if (!allPosts || !categoryOptions) { + return [] + } + // 计数 + let categories = allPosts.map(p => p.category) + categories = [...categories.flat()] + const categoryObj = {} + categories.forEach(category => { + if (category in categoryObj) { + categoryObj[category]++ + } else { + categoryObj[category] = 1 + } + }) + const list = [] + if (isIterable(categoryOptions)) { + for (const c of categoryOptions) { + const count = categoryObj[c.value] + if (count) { + list.push({ id: c.id, name: c.value, color: c.color, count }) + } + } + } + + // 按照数量排序 + // list.sort((a, b) => b.count - a.count) + if (sliceCount && sliceCount > 0) { + return list.slice(0, sliceCount) + } else { + return list + } +} diff --git a/lib/notion/getNotionData.js b/lib/notion/getNotionData.js index e6ee99e7..490076a7 100644 --- a/lib/notion/getNotionData.js +++ b/lib/notion/getNotionData.js @@ -3,7 +3,8 @@ import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager' import { getPostBlocks } from '@/lib/notion/getPostBlocks' import { idToUuid } from 'notion-utils' import { defaultMapImageUrl } from 'react-notion-x' -import { deepClone, isIterable } from '../utils' +import { deepClone } from '../utils' +import { getAllCategories } from './getAllCategories' import getAllPageIds from './getAllPageIds' import { getAllTags } from './getAllTags' import getPageProperties from './getPageProperties' @@ -31,8 +32,6 @@ export async function getGlobalNotionData({ delete notionPageData.schema delete notionPageData.rawMetadata delete notionPageData.pageIds - delete notionPageData.tagOptions - delete notionPageData.categoryOptions return notionPageData } @@ -117,46 +116,6 @@ function getCategoryOptions(schema) { return categorySchema?.options || [] } -/** - * 获取所有文章的分类 - * @param allPosts - * @returns {Promise<{}|*[]>} - */ -function getAllCategories({ allPages, categoryOptions, sliceCount = 0 }) { - const allPosts = allPages.filter(page => page.type === 'Post') - if (!allPosts || !categoryOptions) { - return [] - } - // 计数 - let categories = allPosts.map(p => p.category) - categories = [...categories.flat()] - const categoryObj = {} - categories.forEach(category => { - if (category in categoryObj) { - categoryObj[category]++ - } else { - categoryObj[category] = 1 - } - }) - const list = [] - if (isIterable(categoryOptions)) { - for (const c of categoryOptions) { - const count = categoryObj[c.value] - if (count) { - list.push({ id: c.id, name: c.value, color: c.color, count }) - } - } - } - - // 按照数量排序 - // list.sort((a, b) => b.count - a.count) - if (sliceCount && sliceCount > 0) { - return list.slice(0, sliceCount) - } else { - return list - } -} - /** * 站点信息 * @param notionPageData @@ -279,7 +238,7 @@ async function getPageRecordMapByNotionAPI({ pageId, from }) { const customNav = getCustomNav({ allPages }) const categories = getAllCategories({ allPages, categoryOptions, sliceCount: BLOG.PREVIEW_CATEGORY_COUNT }) - const tags = getAllTags({ allPages, tagOptions }) + const tags = getAllTags({ allPages, sliceCount: BLOG.PREVIEW_TAG_COUNT, tagOptions }) const latestPosts = getLatestPosts({ allPages, from, latestPostCount: 5 }) return { diff --git a/pages/archive/index.js b/pages/archive/index.js index ed40b48a..6e8f115a 100644 --- a/pages/archive/index.js +++ b/pages/archive/index.js @@ -20,10 +20,10 @@ const ArchiveIndex = props => { export async function getStaticProps() { const props = await getGlobalNotionData({ from: 'archive-index' }) - const { allPages } = props - const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') // 处理分页 - props.posts = allPosts + props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') + delete props.allPages + return { props, revalidate: 1 diff --git a/pages/category/index.js b/pages/category/index.js index 9d13411a..bb219bb5 100644 --- a/pages/category/index.js +++ b/pages/category/index.js @@ -2,6 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import React from 'react' import { useGlobal } from '@/lib/global' import * as ThemeMap from '@/themes' +import { getAllCategories } from '@/lib/notion/getAllCategories' /** * 分类首页 @@ -24,10 +25,9 @@ export default function Category(props) { } export async function getStaticProps() { - const props = await getGlobalNotionData({ - from: 'category-index-props', - categoryCount: 0 - }) + const props = await getGlobalNotionData({ from: 'category-index-props' }) + props.categories = getAllCategories({ allPages: props.allPages, categoryOptions: props.categoryOptions, sliceCount: 0 }) + delete props.categoryOptions return { props, revalidate: 1 diff --git a/pages/tag/[tag]/page/[page].js b/pages/tag/[tag]/page/[page].js index e053e018..d9dd99fe 100644 --- a/pages/tag/[tag]/page/[page].js +++ b/pages/tag/[tag]/page/[page].js @@ -45,7 +45,7 @@ export async function getStaticProps({ params: { tag, page } }) { } export async function getStaticPaths() { - const from = 'tag-static-path' + const from = 'tag-page-static-path' const { tags } = await getGlobalNotionData({ from }) const tagNames = [] tags.forEach(tag => { diff --git a/pages/tag/index.js b/pages/tag/index.js index ba8c534c..042af0bc 100644 --- a/pages/tag/index.js +++ b/pages/tag/index.js @@ -2,6 +2,7 @@ import { getGlobalNotionData } from '@/lib/notion/getNotionData' import React from 'react' import { useGlobal } from '@/lib/global' import * as ThemeMap from '@/themes' +import { getAllTags } from '@/lib/notion' /** * 标签首页 @@ -26,6 +27,9 @@ const TagIndex = props => { export async function getStaticProps() { const from = 'tag-index-props' const props = await getGlobalNotionData({ from }) + console.log('获取所有标签', props) + props.tags = getAllTags({ allPages: props.allPages, sliceCount: 0, tagOptions: props.tagOptions }) + delete props.tagOptions return { props, revalidate: 1 diff --git a/themes/next/LayoutCategory.js b/themes/next/LayoutCategory.js index 81424716..063918d4 100644 --- a/themes/next/LayoutCategory.js +++ b/themes/next/LayoutCategory.js @@ -2,15 +2,20 @@ import LayoutBase from './LayoutBase' import StickyBar from './components/StickyBar' import CategoryList from './components/CategoryList' import BlogPostListScroll from './components/BlogPostListScroll' +import BlogPostListPage from './components/BlogPostListPage' +import BLOG from '@/blog.config' export const LayoutCategory = (props) => { - const { tags, posts, category, categories } = props + const { category, categories } = props return
- + {BLOG.POST_LIST_STYLE !== 'page' + ? + : + }
} diff --git a/themes/next/LayoutTag.js b/themes/next/LayoutTag.js index b50f801a..7594ba8c 100644 --- a/themes/next/LayoutTag.js +++ b/themes/next/LayoutTag.js @@ -2,16 +2,21 @@ import LayoutBase from './LayoutBase' import StickyBar from './components/StickyBar' import TagList from './components/TagList' import BlogPostListScroll from './components/BlogPostListScroll' +import BlogPostListPage from './components/BlogPostListPage' +import BLOG from '@/blog.config' export const LayoutTag = (props) => { - const { tags, posts, tag } = props + const { tags, tag } = props return
- + {BLOG.POST_LIST_STYLE !== 'page' + ? + : + }
}