优化 tags 不存在问题

This commit is contained in:
Han 2025-04-07 13:47:47 +08:00
parent ffebcb1099
commit a2d19f5045
5 changed files with 7 additions and 7 deletions

View File

@ -10,7 +10,7 @@ const blog = defineCollection({
date: z.coerce.date(),
updated: z.coerce.date().optional(),
categories: z.string(),
tags: z.array(z.union([z.string(), z.number()])),
tags: z.array(z.union([z.string(), z.number()])).optional(),
id: z.union([z.string(), z.number()]),
cover: z.string().optional(),
recommend: z.boolean().optional(),

View File

@ -56,9 +56,9 @@ import "@/styles/ArticleBase.less";
</header>
<main>
<Content />
<nav class="tag-list">
<section class="tag-list">
{
post.data.tags.map((i: any) => (
(post.data.tags || []).map((i: any) => (
<a href={`/tag/${i}`}>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
@ -69,7 +69,7 @@ import "@/styles/ArticleBase.less";
</a>
))
}
</nav>
</section>
</main>
<footer>
<!-- 打赏组件 -->

View File

@ -4,7 +4,7 @@ import { getCollection } from "astro:content";
export async function getStaticPaths(): Promise<any> {
const posts = await getCollection("blog");
let tagList: any = [];
posts.forEach(post => (tagList = [...tagList, ...post.data.tags]));
posts.forEach(post => (tagList = [...tagList, ...(post.data.tags || [])]));
return [...new Set(tagList)].map(tags => ({ params: { tags } }));
}
// 获取分类列表

View File

@ -25,7 +25,7 @@ const getCategoriesList = async (categories: string) => {
// 获取标签下的文章列表
const getTagsList = async (tags: string) => {
const posts = await getCollection("blog");
const articleList = posts.filter((i: any) => i.data.tags.includes(tags)).sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf());;
const articleList = posts.filter((i: any) => (i.data.tags || []).includes(tags)).sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf());;
return fmtArticleList(articleList);
}

View File

@ -17,7 +17,7 @@ const getCountInfo = () => {
// 获取文章标签
const getTags = () => {
const tagList = posts.reduce((acc: any, i: any) => {
i.data.tags.forEach((tag: string) => {
(i.data.tags || []).forEach((tag: string) => {
acc[tag] = (acc[tag] || 0) + 1;
});
return acc;