以下是在 AstroPaper 博客主题中创建新文章的一些规则/建议、技巧和窍门。
目录
创建博客文章
要编写一篇新的博客文章,请在 src/data/blog/ 目录下创建一个 markdown 文件。
在 AstroPaper v5.1.0 之前,所有博客文章必须放在
src/data/blog/中,这意味着你无法将它们组织到子目录中。
从 AstroPaper v5.1.0 开始,你现在可以将博客文章组织到子目录中,从而使内容管理更加便捷。
例如,如果你想将文章归入 2025 组,可以将它们放在 src/data/blog/2025/ 中。这也会影响文章的 URL,因此 src/data/blog/2025/example-post.md 将对应 /posts/2025/example-post。
如果你不希望子目录影响文章 URL,只需在文件夹名称前加下划线 _ 即可。
# Example: blog post structure and URLs
src/data/blog/very-first-post.md -> mysite.com/posts/very-first-post
src/data/blog/2025/example-post.md -> mysite.com/posts/2025/example-post
src/data/blog/_2026/another-post.md -> mysite.com/posts/another-post
src/data/blog/docs/_legacy/how-to.md -> mysite.com/posts/docs/how-to
src/data/blog/Example Dir/Dummy Post.md -> mysite.com/posts/example-dir/dummy-post
💡 提示:你也可以在 frontmatter 中覆盖文章的 slug。详见下一节。
如果子目录 URL 在构建输出中没有出现,请移除 node_modules,重新安装依赖包,然后重新构建。
Frontmatter
Frontmatter 是存储博客文章(文章)重要信息的主要位置。Frontmatter 位于文章顶部,采用 YAML 格式编写。有关 frontmatter 及其用法的更多信息,请参阅 Astro 文档。
以下是每篇文章的 frontmatter 属性列表。
| Property | Description | Remark |
|---|---|---|
| title | 文章标题。(h1) | required* |
| description | 文章描述。用于文章摘要和文章站点描述。 | required* |
| pubDatetime | 发布日期时间,ISO 8601 格式。 | required* |
| modDatetime | 修改日期时间,ISO 8601 格式。(仅在文章被修改时添加此属性) | optional |
| author | 文章作者。 | default = SITE.author |
| slug | 文章的 Slug。此字段为可选。 | default = slugified file name |
| featured | 是否在首页的精选区域展示此文章。 | default = false |
| draft | 将此文章标记为”未发布”。 | default = false |
| tags | 此文章的相关关键词。以 yaml 数组格式编写。 | default = others |
| ogImage | 文章的 OG 图片。用于社交媒体分享和 SEO。可以是远程 URL 或相对于当前文件夹的图片路径。 | default = SITE.ogImage or generated OG image |
| canonicalURL | 规范 URL(绝对路径),适用于文章已在其他来源存在的情况。 | default = Astro.site + Astro.url.pathname |
| hideEditPost | 隐藏文章标题下方的 editPost 按钮。此设置仅对当前文章生效。 | default = false |
| timezone | 为当前文章指定 IANA 格式的时区。这将覆盖当前文章的 SITE.timezone 配置。 | default = SITE.timezone |
提示!你可以在控制台中运行
new Date().toISOString()来获取 ISO 8601 格式的日期时间。请确保移除引号。
只有 frontmatter 中的 title、description 和 pubDatetime 字段是必须指定的。
标题和描述(摘要)对搜索引擎优化(SEO)非常重要,因此 AstroPaper 建议在博客文章中包含这些内容。
slug 是 URL 的唯一标识符。因此,slug 必须是唯一的,不能与其他文章相同。slug 中的空格应使用 - 或 _ 分隔,但推荐使用 -。Slug 会根据博客文章文件名自动生成。不过,你也可以在博客文章中将 slug 定义为 frontmatter 属性。
例如,如果博客文件名为 adding-new-post.md,且你没有在 frontmatter 中指定 slug,Astro 将使用文件名自动创建文章的 slug。因此,slug 将是 adding-new-post。但如果你在 frontmatter 中指定了 slug,它将覆盖默认的 slug。你可以在 Astro 文档中了解更多相关信息。
如果你在博客文章中省略了 tags(换句话说,如果没有指定标签),默认标签 others 将被用作该文章的标签。你可以在 content.config.ts 文件中设置默认标签。
export const blogSchema = z.object({
// ...
draft: z.boolean().optional(),
tags: z.array(z.string()).default(["others"]), // replace "others" with whatever you want
// ...
});src/content.config.ts
示例 Frontmatter
以下是一篇文章的示例 frontmatter。
---
title: The title of the post
author: your name
pubDatetime: 2022-09-21T05:17:19Z
featured: true
draft: false
tags:
- some
- example
- tags
ogImage: ../../assets/images/example.png # src/assets/images/example.png
# ogImage: "https://example.org/remote-image.png" # remote URL
description: This is the example description of the example post.
canonicalURL: https://example.org/my-article-was-already-posted-here
---src/data/blog/sample-post.md
添加目录
默认情况下,文章不包含任何目录(toc)。要包含目录,你需要以特定方式指定它。
在需要目录出现的位置,以 h2 格式(markdown 中的 ##)写上 目录。
例如,如果你想将目录放在简介段落下方(就像我通常做的那样),可以按以下方式操作。
---
# frontmatter
---
Here are some recommendations, tips & ticks for creating new posts in AstroPaper blog theme.
## Table of contents
<!-- the rest of the post -->
标题
关于标题有一点需要注意。AstroPaper 博客文章使用 title(frontmatter 中的 title)作为文章的主标题。因此,文章中的其余标题应使用 h2 ~ h6。
此规则并非强制要求,但出于视觉、可访问性和 SEO 目的强烈推荐遵守。
语法高亮
AstroPaper 使用 Shiki 作为默认的语法高亮方案。从 AstroPaper v5.4 开始,使用 @shikijs/transformers 来增强代码块的效果。如果你不想使用它,可以像下面这样简单地移除它:
pnpm remove @shikijs/transformers
// ...
import {
transformerNotationDiff,
transformerNotationHighlight,
transformerNotationWordHighlight,
} from "@shikijs/transformers";
export default defineConfig({
// ...
markdown: {
remarkPlugins: [remarkToc, [remarkCollapse, { test: "Table of contents" }]],
shikiConfig: {
// For more themes, visit https://shiki.style/themes
themes: { light: "min-light", dark: "night-owl" },
defaultColor: false,
wrap: false,
transformers: [
transformerFileName(),
transformerNotationHighlight(),
transformerNotationWordHighlight(),
transformerNotationDiff({ matchAlgorithm: "v3" }),
],
},
},
// ...
}astro.config.ts
存储博客内容图片
以下是两种存储图片并在 markdown 文件中显示它们的方法。
注意!如果需要在 markdown 中对优化后的图片进行样式设置,你应该使用 MDX。
放在 src/assets/ 目录中(推荐)
你可以将图片存储在 src/assets/ 目录中。这些图片将通过 Image Service API 由 Astro 自动优化。
你可以使用相对路径或别名路径(@/assets/)来引用这些图片。
示例:假设你想显示路径为 /src/assets/images/example.jpg 的图片 example.jpg。

<!-- OR -->

<!-- Using img tag or Image component won't work ❌ -->
<img src="@/assets/images/example.jpg" alt="something">
<!-- ^^ This is wrong -->
从技术上讲,你可以将图片存储在
src下的任何目录中。这里推荐的src/assets只是其中一个选择。
放在 public 目录中
你可以将图片存储在 public 目录中。请注意,存储在 public 目录中的图片不会被 Astro 处理,这意味着它们将不会被优化,你需要自行处理图片优化。
对于这些图片,你应该使用绝对路径;这些图片可以通过 markdown 标注或 HTML img 标签来显示。
示例:假设 example.jpg 位于 /public/assets/images/example.jpg。

<!-- OR -->
<img src="/assets/images/example.jpg" alt="something">
附加提示
图片压缩
当你在博客文章中放入图片时(尤其是 public 目录下的图片),建议对图片进行压缩。这将影响网站的整体性能。
我推荐的图片压缩网站:
OG 图片
如果文章没有指定 OG 图片,将使用默认的 OG 图片。虽然不是强制要求,但建议在 frontmatter 中指定与文章相关的 OG 图片。推荐的 OG 图片尺寸为 1200 X 640 px。
自 AstroPaper v1.4.0 起,如果未指定 OG 图片,将会自动生成。查看公告。