dsh
|
8e08fc5094
|
内容 schema 兜住 YAML 空值,修掉手写偶得触发的构建失败
问题
新增的 src/content/thoughts/20260622-夏至已至.md 让 build 直接失败:
InvalidContentEntryDataError: thoughts → 20260622-夏至已至
tags: Expected type "array", received "object"
images: Expected type "array", received "object"
报错里那个 "object" 很误导 —— 它其实是 null(JS 里 typeof null === "object")。
根因
该文件的 frontmatter 写了两个空字段:
images:
tags:
YAML 把「只写 key、后面什么都不跟」解析成 null,而不是 undefined。
而 schema 用的是 z.array(z.string()).optional().default([]) ——
.optional() 只放行 undefined,.default() 也只对 undefined 生效,
所以 null 会直接被判为类型不符。
这不是那一个文件的问题,而是一类写法的问题:手写 frontmatter 时把不要的字段
留空太自然了。而且这类报错信息完全指不出真正的原因,很浪费时间。
改法
在 content.config.ts 顶部加三个 helper,把 null 当作「没填」处理:
text(fallback = "") 文本,留空 → fallback
optionalText() 可选文本,留空 → undefined(保持原类型)
textList() 字符串数组,留空 → []
然后套用到 posts / thoughts / studies 里所有「留空应该算默认值」的字段
(description / image / tags / category / lang / password / prev* / next*、
thoughts 的 title / tags / location / weather / images、studies 的三个可选文本)。
注意:只改了「留空该走默认」的字段。像 posts.title、published、
studies.title / status / started 这些必填项保持原样 —— 它们留空本来就该报错。
验证
- 清内容缓存重建:pnpm build 退出码 0,不再报 InvalidContentEntryDataError
- 那条偶得正常产出(tags / images 都成了空数组,location / weather 保留)
- 抽查其他条目:树莓派那篇的标签与任务列表、首页的偶得列表均正常
|
2026-09-17 20:35:56 +08:00 |
|
TimeCrackbot
|
738fe3175e
|
chore: 升级 Astro 5.16.5 → 7.3.3
依赖
- astro 5 → 7.3.3,@astrojs/{mdx,react,rss,sitemap,check}、astro-expressive-code
与 @swup/astro 同步升到最新
- 新增 @astrojs/markdown-remark(Astro 7 默认改用 Sätteri 处理器,
本项目依赖大量 remark/rehype 插件,故显式固定回 unified 管线)
内容集合迁移到 Content Layer API(Astro 6 已移除旧 API)
- src/content/config.ts → src/content.config.ts,三个集合改用 glob() loader
- z 改从 astro/zod 导入
- 所有 entry.slug → entry.id;entry.render() → render(entry)
Astro 7 兼容
- 修复 bookshelf-content.astro 里多行 {/* */} 注释导致的 Rust 编译器
"Unterminated regular expression"
- 删除该文件里一处多余的 </div>(旧 Go 编译器会静默纠正,新编译器不会)
- global.css: @import "tailwindcss" → "tailwindcss/index.css"
(Vite 8 不再解析裸包名的 style 条件)
- astro.config.mjs 显式设置 compressHTML: true,保持 v6 的空白处理行为
验证:构建 41 页通过;与线上(Astro 5 构建)对比 39 个 URL 无一丢失,
仅新增新文章 DIY-computer 的 2 个 URL;逐页渲染文本与线上一致;
pnpm build 与 dev server 均正常。
|
2026-09-17 17:14:08 +08:00 |
|