cover

走马

陈粒

实现今日一言功能

615 字
3 分钟
实现今日一言功能
AI 摘要

创建数据文件#

文件路径:src/content/ziyuan/ 目录并添加文件:quote.md

---
title: "每日一言"
quotes:
- text: "生活不是等待暴风雨过去,而是学会在雨中跳舞。"
author: "塞维涅夫人"
- text: "世界上只有一种英雄主义,就是在认清生活真相之后依然热爱生活。"
author: "罗曼·罗兰"
---

创建组件文件#

文件路径:src/components/widget/QuoteOfTheDay.astro

---
import WidgetLayout from "@/components/common/WidgetLayout.astro";
import { getCollection } from "astro:content";
// 从 content/ziyuan/quote.md 读取名言数据
const ziyuanEntries = await getCollection("ziyuan");
const quoteEntry = ziyuanEntries.find((e) => e.id === "quote");
const quoteData = quoteEntry?.data as any;
const quotes = quoteData?.quotes || [];
interface Props {
class?: string;
style?: string;
}
const { class: className, style } = Astro.props;
// 使用日期作为随机种子,确保同一天显示相同的名言
const today = new Date();
const dateStr = today.toISOString().split("T")[0];
const seed = dateStr.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0);
const randomIndex = seed % quotes.length;
const quote = quotes[randomIndex] || { text: "暂无名言", author: "" };
---
<WidgetLayout name="✨ 今日一言" id="quote-of-the-day" class={className} style={style}>
<div class="quote-content">
<blockquote class="quote-text">
<span class="quote-mark">"</span>
{quote.text}
<span class="quote-mark">"</span>
</blockquote>
<cite class="quote-author">—— {quote.author}</cite>
</div>
</WidgetLayout>
<style>
.quote-content {
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 0.5rem 0;
}
.quote-text {
position: relative;
font-style: italic;
font-size: 0.9rem;
line-height: 1.6;
color: var(--text-color);
padding: 0 0.5rem;
margin: 0;
}
.quote-mark {
font-size: 1.5rem;
line-height: 0;
vertical-align: middle;
color: var(--primary);
opacity: 0.6;
font-family: Georgia, serif;
}
.quote-author {
display: block;
text-align: right;
font-size: 0.75rem;
color: var(--content-meta);
font-style: normal;
margin-top: 0.25rem;
}
/* 深色模式优化 */
:global(html.dark) .quote-text {
color: var(--text-color);
}
/* 悬停效果 */
:global(.quote-content:hover .quote-text) {
color: var(--primary);
transition: color 0.2s ease;
}
</style>

配置数据加载方式#

src/content.config.ts 配置数据加载方式:

const ziyuanCollection = defineCollection({
loader: glob({ pattern: "**/*.md", base: "./src/content/ziyuan" }),
schema: z.union([
z.object({
title: z.string(),
content: z.string(),
closable: z.boolean().optional().default(true),
link: z
.object({
enable: z.boolean().optional().default(true),
text: z.string(),
url: z.string(),
external: z.boolean().optional().default(false),
})
.optional(),
quotes: z.undefined().optional(),
}),
z.object({
title: z.string(),
quotes: z.array(
z.object({
text: z.string(),
author: z.string(),
})
),
content: z.undefined().optional(),
closable: z.undefined().optional(),
link: z.undefined().optional(),
}),
]),
});
export const collections = {
ziyuan: ziyuanCollection,
};

注册组件#

文件路径:src/components/layout/SideBar.astro

import QuoteOfTheDay from "@/components/widget/QuoteOfTheDay.astro";

添加到组件映射表:

const componentMap = {
quoteOfTheDay: QuoteOfTheDay, // 添加这一行
};

侧边栏配置#

文件路径:src/config/sidebarConfig.ts

  • 添加到右侧边栏
rightComponents: [
{
type: "quoteOfTheDay", // 必须与 componentMap 的 key 一致
enable: true, // true = 启用,false = 禁用
position: "sticky", // "top" = 固定顶部,"sticky" = 粘性定位
showOnPostPage: true, // 是否在文章详情页显示
},
// ... 其他组件
],
  • 添加到移动端底部
mobileBottomComponents: [
{
// 组件类型:今日一言
type: "quoteOfTheDay",
// 是否启用该组件
enable: true,
// 是否在文章详情页显示
showOnPostPage: true,
},
// ... 其他组件
],

类型定义#

文件路径:src/types/config.ts

export type WidgetComponentType =
| "profile"
| "announcement"
// ... 其他类型
| "quoteOfTheDay"; // 确保这一行存在

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
实现今日一言功能
https://my-firefly-blog-dpzi92pofgva.edgeone.cool/blog/quoteoftheday/
作者
R1ng13
发布于
2026-05-30
许可协议
CC BY-NC-SA 4.0
相关文章 智能推荐
1
集成朋友圈与笔记本功能
Firefly 本教程详细介绍如何将朋友圈(Moments)和笔记本(Notebooks)功能集成到项目中,数据存储在 GitHub Gist 中。
2
隐藏封面图
Firefly 最近在折腾博客的封面图显示逻辑,加了个「隐藏封面图」的开关功能——从类型定义、配置项、工具函数到 UI 控件和样式,一步步把整个流程补全了。代码分散在多个文件里,但核心就一件事:让用户能一键收起或展开文章封面,既保持页面简洁,又不丢失视觉层次。顺手还做了多语言支持,中文、英文切换也一起配上了。
3
归档统计
Firefly 最近在折腾博客的归档统计功能,从获取归档列表开始,一路写到更新内容的工具函数、类型定义,再到多语言配置的拆分与管理——每一处改动都为了让归档页更清晰、更易维护,也顺便理清了自己代码里的逻辑脉络。
4
给博客增加单个和全部分类页面
Firefly 最近给博客加了个小功能:点击分类名就能跳转到该分类下的所有文章,还能一键查看全部分类列表——整个过程其实挺顺的,从创建页面、写 URL 工具函数,到更新导航栏和多语言支持,一步步配下来,连分类链接都自动带上了本地化路径,现在逛自己博客像逛图书馆一样清爽 😄
5
恋爱计时组件
Firefly 最近给博客加了个小彩蛋——「恋爱计时组件」,从零撸了一个记录纪念日的小工具。写了组件代码、配了全局注册、还兼顾了桌面端侧边栏和移动端的显示逻辑,连 TypeScript 类型定义和双方昵称/日期的配置项都琢磨得挺细。虽然只是个轻量小功能,但每次看到倒计时跳动,心里都悄悄甜一下 😊
随机文章 随机推荐

评论区

Profile Image of the Author
R1ng13
Hello, I'm R1ng13.
📢 欢迎来访者
👋🏻 Hi,我是R1ng13,欢迎您!
分类
标签
站点统计
文章
11
分类
2
标签
5
总字数
4,867
运行时长
0
最后活动
0 天前
站点信息
构建平台
EdgeOne
博客版本
Firefly v6.12.3
文章许可
CC BY-NC-SA 4.0
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
我和宝宝在一起已经
---------TSH ❤️ CXY---------
---------TSH
❤️
CXY---------
0 0 0 0 0 00
✨ 今日一言
" 山重水复疑无路,柳暗花明又一村。 "
—— 陆游
天气预报
统计

文章目录

✨️ 复制成功,转载请标注本文地址