Skip to content

Commit d53ee68

Browse files
committed
refactor: notes collection schema, /notes routes, draft gating
1 parent d14dc01 commit d53ee68

27 files changed

Lines changed: 410 additions & 394 deletions

‎src/assets/blog-placeholder-2.jpg‎

-32.4 KB
Binary file not shown.

‎src/assets/blog-placeholder-3.jpg‎

-28 KB
Binary file not shown.

‎src/assets/blog-placeholder-4.jpg‎

-37.8 KB
Binary file not shown.

‎src/assets/blog-placeholder-5.jpg‎

-34.1 KB
Binary file not shown.
-21.1 KB
Binary file not shown.

‎src/components/BaseHead.astro‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import 'katex/dist/katex.min.css';
77
// all pages through the use of the <BaseHead /> component.
88
import '../styles/global.css';
99
import type { ImageMetadata } from 'astro';
10-
import FallbackImage from '../assets/blog-placeholder-1.jpg';
11-
import { SITE_TITLE } from '../consts';
10+
import FallbackImage from '../assets/og-default.jpg';
11+
import { SITE_DESCRIPTION, SITE_TITLE } from '../consts';
1212
import { Font } from 'astro:assets';
1313
1414
interface Props {
1515
title: string;
16-
description: string;
16+
description?: string;
1717
image?: ImageMetadata;
1818
}
1919
2020
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
2121
22-
const { title, description, image = FallbackImage } = Astro.props;
22+
// description 在 schema 里是选填的,缺省时回落到站点描述,
23+
// 避免 meta description 和 og:description 变成空字符串
24+
const { title, description = SITE_DESCRIPTION, image = FallbackImage } = Astro.props;
2325
---
2426

2527
<!-- Global Metadata -->

‎src/components/Header.astro‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import HeaderLink from './HeaderLink.astro';
88
<a href="/" class="font-semibold no-underline">{SITE_TITLE}</a>
99
<nav class="flex gap-6 text-[0.9375rem]">
1010
<HeaderLink href="/">首页</HeaderLink>
11-
<HeaderLink href="/blog">笔记</HeaderLink>
11+
<HeaderLink href="/notes">笔记</HeaderLink>
1212
<HeaderLink href="/search">搜索</HeaderLink>
1313
<HeaderLink href="/about">关于</HeaderLink>
1414
</nav>

‎src/content.config.ts‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@ import { defineCollection } from 'astro:content';
22
import { glob } from 'astro/loaders';
33
import { z } from 'astro/zod';
44

5-
const blog = defineCollection({
6-
// Load Markdown and MDX files in the `src/content/blog/` directory.
7-
loader: glob({ base: './src/content/blog', pattern: '**/*.{md,mdx}' }),
8-
// Type-check frontmatter using a schema
9-
schema: ({ image }) =>
10-
z.object({
11-
title: z.string(),
12-
description: z.string(),
13-
// Transform string to Date object
14-
pubDate: z.coerce.date(),
15-
updatedDate: z.coerce.date().optional(),
16-
heroImage: z.optional(image()),
17-
}),
5+
/**
6+
* 日期一律写成 YYYY-MM-DD,并按 UTC 零点解析。
7+
*
8+
* 不用 z.coerce.date() 是因为它什么都收:'Jul 08 2022' 也能过,但那种写法会按
9+
* 运行机器的本地时区解析。本地是 +08、CI 是 UTC,同一篇文章渲染出的日期会差一天
10+
* (FormattedDate 用 getUTC* 取值)。这里写死格式,写错就构建失败,
11+
* 比上线后发现日期少一天要好。
12+
*/
13+
const isoDate = z
14+
.string()
15+
.regex(/^\d{4}-\d{2}-\d{2}$/, '日期必须写成 YYYY-MM-DD,例如 2026-07-25')
16+
.transform((value) => new Date(`${value}T00:00:00Z`));
17+
18+
const notes = defineCollection({
19+
loader: glob({ base: './src/content/notes', pattern: '**/*.{md,mdx}' }),
20+
schema: z.object({
21+
title: z.string().min(1),
22+
date: isoDate,
23+
// 内容有实质修订时再填,纯错别字不必
24+
updated: isoDate.optional(),
25+
// 选填。用于列表页摘要、meta description 和 RSS;留空则回落到站点默认描述
26+
description: z.string().optional(),
27+
// 暂不做白名单校验,先自由填。等标签页做出来能看到词频,
28+
// 届时若发现「机器学习 / ML」这类漂移再收紧。
29+
tags: z.array(z.string().min(1)).default([]),
30+
// true 时本地 dev 可预览,生产构建完全不生成页面
31+
draft: z.boolean().default(false),
32+
}),
1833
});
1934

20-
export const collections = { blog };
35+
export const collections = { notes };

‎src/content/blog/first-post.md‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)