YUN-LOVE

Back

前言#

尽管我现在已经不再使用fuwari,但是我看到使用人数很多,相信会有人需要这篇文章的。
文章末尾已附上完整文件(仅针对我使用的版本,如果更新的话变化应该不大),如需要请复制粘贴。

第一步:向src/content/config.ts中添加一个 pinned 字段#

// src/content/config.ts
import { defineCollection, z } from 'astro:content';

const posts = defineCollection({
  schema: z.object({
    title: z.string(),
    published: z.date(),
    // ... 其他原有的字段 ...
    
    //  添加下面这行:
    pinned: z.boolean().optional().default(false), 
  }),
});

export const collections = { posts }
typescript

第二步:修改排序逻辑 (content-utils.ts)#

我们将排序逻辑改为:先比较置顶状态,再比较发布时间。

第三步:修改 UI 显示 (PostCard.astro)#

为了让置顶文章一目了然,我们要在标题旁边加一个“图钉”图标。
修改PostCard.astro,在显示{title}的位置前面添加图标判断逻辑。
找到代码中<a href={url} ...>包含{title}的部分,修改如下:

第四步:如何使用#

现在功能已经做好了,只要在你想要置顶的文章的Markdown头部(Frontmatter)添加pinned: true即可:

---
title: 我的置顶文章
published: 2024-01-01
description: 这是最重要的文章
tags: [重要]
pinned: true
---
markdown

注意事项#

如果pnpm build报错可以尝试执行

pnpm add -D @iconify-json/material-symbols
bash

完整文件#

1. src/content/config.ts#

2. src/utils/content-utils.ts#

主要变化:在getRawSortedPosts中添加了优先判断pinned字段的逻辑

3. src/components/PostCard.astro#

主要变化:在标题区域添加了图钉图标(material-symbols:push-pin)的显示逻辑

如何为你的fuwari博客添加置顶项
https://astro-pure.js.org/blog/add-pinned
Author YUN-LOVE
Published at June 7, 2026
Comment seems to stuck. Try to refresh?✨