适配无标签文章
This commit is contained in:
tangly
2021-12-06 20:39:26 +08:00
parent 22c421a0c3
commit 97266b130f
7 changed files with 12 additions and 8 deletions

View File

@@ -2,8 +2,13 @@ import { faTag } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import Link from 'next/link'
import React from 'react'
import { useGlobal } from '@/lib/global'
const TagItem = ({ tag, selected }) => {
const { locale } = useGlobal()
if (!tag) {
<>{locale.COMMON.NOTAG}</>
}
return (
<Link href={selected ? '/' : `/tag/${encodeURIComponent(tag.name)}`} passHref>
<li

View File

@@ -2,7 +2,7 @@ import { getCacheFromFile, setCacheToFile } from '@/lib/cache/local_file_cache'
import { getCacheFromMemory, setCacheToMemory } from '@/lib/cache/memory_cache'
import BLOG from '@/blog.config'
// 关闭本地缓存
const enableCache = true
const enableCache = false
/**
* 为减少频繁接口请求notion数据将被缓存

View File

@@ -1,4 +1,3 @@
import BLOG from '@/blog.config'
import lang from './lang'
import { useContext, createContext, useState, useEffect } from 'react'
import cookie from 'react-cookies'

View File

@@ -11,6 +11,7 @@ export default {
MORE: 'More',
LATEST_POSTS: 'Latest posts',
TAGS: 'Tags',
NO_TAG: 'NoTag',
CATEGORY: 'Category',
SHARE: 'Share',
SCAN_QR_CODE: 'Scan QRCode',

View File

@@ -12,6 +12,7 @@ export default {
MORE: '更多',
LATEST_POSTS: '最新文章',
TAGS: '标签',
NO_TAG: 'NoTag',
CATEGORY: '分类',
SHARE: '分享',
SCAN_QR_CODE: '扫一扫二维码',

View File

@@ -3,8 +3,6 @@ import getAllPageIds from './getAllPageIds'
import getPageProperties from './getPageProperties'
import { defaultMapImageUrl } from 'react-notion-x'
import { getNotionPageData } from '@/lib/notion/getNotionData'
import TagItemMini from '@/components/TagItemMini'
import React from 'react'
/**
* 获取所有文章列表
@@ -26,19 +24,19 @@ export async function getAllPosts ({ notionPageData, from, includePage = false }
const tagOptions = notionPageData.tagOptions
const collectionQuery = notionPageData.collectionQuery
// 获取每篇文章信息
const data = []
const pageIds = getAllPageIds(collectionQuery)
for (let i = 0; i < pageIds.length; i++) {
const id = pageIds[i]
const properties = (await getPageProperties(id, pageBlock, schema)) || null
const tagItems = properties?.tags?.map(tag => { return { name: tag, color: tagOptions.find(t => t.value === tag).color } }) || ['默认']
properties.createdTime = new Date(pageBlock[id].value?.created_time).toString()
properties.lastEditedTime = new Date(pageBlock[id].value?.last_edited_time).toString()
properties.fullWidth = pageBlock[id].value?.format?.page_full_width ?? false
properties.page_cover = getPostCover(id, pageBlock) ?? BLOG.defaultImgCover
properties.content = pageBlock[id].value?.content ?? []
properties.tagItems = tagItems
properties.tagItems = properties?.tags?.map(tag => {
return { name: tag, color: tagOptions.find(t => t.value === tag)?.color || 'gray' }
}) || []
data.push(properties)
}

View File

@@ -26,7 +26,7 @@ export async function getAllTags ({ allPosts, sliceCount = 12, tagOptions }) {
// 按照标签数量排序
const list = Object.keys(tagObj).map((tag) => {
const color = tagOptions.find(option => option.value === tag).color
const color = tagOptions.find(option => option.value === tag)?.color || 'gray'
return { name: tag, count: tagObj[tag], color }
})
list.sort((a, b) => b.count - a.count)