NEXT主题,分类标签调整

This commit is contained in:
tangly1024
2022-03-01 12:49:38 +08:00
parent 0b2046b658
commit b89177644f
10 changed files with 69 additions and 46 deletions

View File

@@ -3,11 +3,11 @@
* @param allPosts
* @returns {Promise<{}|*[]>}
*/
export async function getAllCategories (allPosts) {
if (!allPosts) {
export async function getAllCategories ({ allPosts, categoryOptions, sliceCount = 0 }) {
if (!allPosts || !categoryOptions) {
return []
}
// 计数
let categories = allPosts.map(p => p.category)
categories = [...categories.flat()]
const categoryObj = {}
@@ -18,5 +18,19 @@ export async function getAllCategories (allPosts) {
categoryObj[category] = 1
}
})
return categoryObj
const list = []
categoryOptions.forEach(c => {
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
}
}

View File

@@ -6,15 +6,13 @@
* @param tagOptions tags的下拉选项
* @returns {Promise<{}|*[]>}
*/
export async function getAllTags ({ allPosts, sliceCount = 16, tagOptions }) {
if (!allPosts) {
export async function getAllTags ({ allPosts, sliceCount = 0, tagOptions }) {
if (!allPosts || !tagOptions) {
return []
}
// 计数
let tags = allPosts.map(p => p.tags)
tags = [...tags.flat()]
// 标签计数
const tagObj = {}
tags.forEach(tag => {
if (tag in tagObj) {
@@ -23,13 +21,16 @@ export async function getAllTags ({ allPosts, sliceCount = 16, tagOptions }) {
tagObj[tag] = 1
}
})
// 按照标签数量排序
const list = Object.keys(tagObj).map((tag) => {
const color = tagOptions.find(option => option.value === tag)?.color || 'gray'
return { name: tag, count: tagObj[tag], color }
const list = []
tagOptions.forEach(c => {
const count = tagObj[c.value]
if (count) {
list.push({ id: c.id, name: c.value, color: c.color, count })
}
})
list.sort((a, b) => b.count - a.count)
// 按照数量排序
// list.sort((a, b) => b.count - a.count)
if (sliceCount && sliceCount > 0) {
return list.slice(0, sliceCount)
} else {

View File

@@ -11,6 +11,7 @@ import { getAllTags } from './getAllTags'
* @param {*} pageId
* @param {*} from
* @param latestPostCount 截取最新文章数量
* @param categoryCount
* @param tagsCount 截取标签数量
* @param pageType 过滤的文章类型,数组格式 ['Page','Post']
* @returns {
@@ -27,15 +28,17 @@ export async function getGlobalNotionData ({
pageId = BLOG.NOTION_PAGE_ID,
from,
latestPostCount = 5,
tagsCount = 16,
categoryCount = BLOG.PREVIEW_CATEGORY_COUNT,
tagsCount = BLOG.PREVIEW_TAG_COUNT,
pageType = ['Post']
}) {
const notionPageData = await getNotionPageData({ pageId, from })
const tagOptions = notionPageData.tagOptions
const categoryOptions = notionPageData.categoryOptions
const allPosts = await getAllPosts({ notionPageData, from, pageType })
const postCount = await getAllPostCount({ notionPageData, from })
const customNav = await getCustomNav({ notionPageData })
const categories = await getAllCategories(allPosts)
const categories = await getAllCategories({ allPosts, categoryOptions, sliceCount: categoryCount })
const tags = await getAllTags({ allPosts, tagOptions, sliceCount: tagsCount })
const latestPosts = await getLatestPosts({ notionPageData, from, latestPostCount })
return { allPosts, latestPosts, categories, postCount, customNav, tags }
@@ -110,6 +113,11 @@ function getTagOptions (schema) {
return tagSchema?.options || {}
}
function getCategoryOptions (schema) {
const categorySchema = Object.values(schema).find(e => e.name === 'category')
return categorySchema?.options || {}
}
/**
* 调用NotionAPI获取Page数据
* @returns {Promise<JSX.Element|null|*>}
@@ -119,7 +127,6 @@ async function getPageRecordMapByNotionAPI ({ pageId, from }) {
if (!pageRecordMap) {
return []
}
pageId = idToUuid(pageId)
const collection = Object.values(pageRecordMap.collection)[0]?.value
const collectionQuery = pageRecordMap.collection_query
@@ -127,6 +134,7 @@ async function getPageRecordMapByNotionAPI ({ pageId, from }) {
const schema = collection?.schema
const rawMetadata = block[pageId].value
const tagOptions = getTagOptions(schema)
const categoryOptions = getCategoryOptions(schema)
// Check Type Page-Database和Inline-Database
if (
@@ -143,6 +151,7 @@ async function getPageRecordMapByNotionAPI ({ pageId, from }) {
block,
schema,
tagOptions,
categoryOptions,
rawMetadata
}
}