实现今日一言功能
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"; // 确保这一行存在支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
AI 摘要实现
Firefly最近给博客折腾了个 AI 摘要功能,效果挺有意思。今天就把我在 Firefly 项目里摸爬滚打总结的实战经验全盘托出。从挑大模型、写构建脚本到搞定打字机动画,顺便分享几个我踩过的坑,希望能帮想给博客加 AI 元素的你少走点弯路。
2
集成朋友圈与笔记本功能
Firefly最近折腾博客,参考大佬方案把朋友圈和笔记本功能安排上了。这篇算是我的实操复盘,补充了不少配置细节。从前端组件到后台管理,一步步带你用 GitHub Gist 零成本搞定这两个模块,想给博客加点料的朋友直接来抄作业啦。
3
自助友链申请
Firefly最近折腾了一下,终于把站里的自助友链申请功能搞定了!对比一圈后,我选了 Supabase 搭配 EdgeOne 边缘函数。从数据库配置、前端改造到函数部署,完整流程都整理好了。想给博客加个友链入口的朋友,直接来抄作业吧!
4
隐藏封面图
Firefly最近在折腾博客的封面图显示逻辑,加了个「隐藏封面图」的开关功能——从类型定义、配置项、工具函数到 UI 控件和样式,一步步把整个流程补全了。代码分散在多个文件里,但核心就一件事:让用户能一键收起或展开文章封面,既保持页面简洁,又不丢失视觉层次。顺手还做了多语言支持,中文、英文切换也一起配上了。
5
归档统计
Firefly最近在折腾博客的归档统计功能,从获取归档列表开始,一路写到更新内容的工具函数、类型定义,再到多语言配置的拆分与管理——每一处改动都为了让归档页更清晰、更易维护,也顺便理清了自己代码里的逻辑脉络。
随机文章随机推荐










