Use YAML frontmatter 为 document 元数据
Frontmatter 是 YAML 元数据在 顶部的 Markdown file, delimited 通过 ---. 常见在 静态 site generators (Astro, Hugo, Jekyll) 为 page 元数据.
- 1
Frontmatter 是 YAML 阻止在 顶部的 file, delimited 通过 --- 开第一行 和之前内容. 它 contains 元数据 (title, 日期, tags, 布局) 该是 parsed separately 从 Markdown 主体. 静态 site generators use 它到 drive templates, routing, 和 collections.
- 2 YAML 转换器 →
解析 the frontmatter YAML to verify valid syntax. Common fields: title (string), date (ISO 8601), tags (array), draft (boolean), layout (string). YAML errors in frontmatter will prevent the page from building — validate early.
- 3 Markdown 转 HTML 转换器 →
解析 the Markdown body (after the frontmatter) to HTML. The frontmatter and body are processed separately: YAML metadata for templates, Markdown for content. This separation enables data-driven page generation.
- 4
Best practices: use consistent 字段 names across 所有 pages, use ISO 8601 dates (2024-01-15 或 2024-01-15T10:30:00Z), use arrays 为 tags 和 categories, use booleans 为 draft/featured flags. Define 模式 (Zod, JSON 模式) 和验证在 构建时间到 捕获 missing 或无效 fields.
常见问题
- YAML vs TOML vs JSON frontmatter?
- YAML 是最多常见 (Hugo, Jekyll, Astro) — human-readable, supports comments. TOML 是 used 通过一些 tools (Zola) — stricter, 更少 ambiguous. JSON 是更少常见但 universally parseable. Use YAML 为最多 cases; use TOML 如果你 want strict parsing.
- 能够 frontmatter 包含 Markdown?
- 是, 但它 必须是 quoted 或 use YAML 阻止 scalars (| 为字面量, > 为 folded). 为示例: description: | 此是 **长** description 具有 _Markdown_. Parse 此字段 as Markdown 在你的模板.