以下是在 astro-minimax 博客主题中创建新文章的一些规则/建议、技巧和提示。
Table of contents
Open Table of contents
创建博客文章
要撰写新的博客文章,请在 src/data/blog/ 目录中创建一个 markdown 文件。
在 astro-minimax v5.1.0 之前,所有博客文章都必须放在
src/data/blog/中,这意味着你无法将它们组织到子目录中。
从 astro-minimax 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-postbash💡 提示:你也可以在 frontmatter 中覆盖博客文章的 slug。详见下一节。
如果子目录 URL 没有出现在构建输出中,请删除 node_modules,重新安装包,然后重新构建。
Frontmatter
Frontmatter 是存储博客文章(文章)重要信息的主要位置。Frontmatter 位于文章顶部,以 YAML 格式编写。在 astro 文档中阅读更多关于 frontmatter 及其用法的信息。
以下是每篇文章的 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 |
| category | 文章分类,支持层级格式如 教程/配置。用于内容组织和导航。 | optional |
| tags | 此文章的相关关键词。以 yaml 数组格式编写。 | default = others |
| ogImage | 文章的 OG 图片。用于社交媒体分享和 SEO。可以是远程 URL 或相对于当前文件夹的图片路径。 | default = SITE.ogImage 或生成的 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)很重要,因此 astro-minimax 鼓励在博客文章中包含这些内容。
slug 是 URL 的唯一标识符。因此,slug 必须唯一且与其他文章不同。slug 的空格应用 - 或 _ 分隔,但推荐使用 -。Slug 使用博客文章文件名自动生成。但是,你可以在博客文章的 frontmatter 中定义你的 slug。
例如,如果博客文件名是 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
// ...
});tsFrontmatter 示例
以下是文章的 frontmatter 示例。
---
title: The title of the post
author: your name
pubDatetime: 2022-09-21T05:17:19Z
slug: the-title-of-the-post
featured: true
draft: false
category: 教程/博客
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
---yaml添加目录
默认情况下,文章不包含任何目录(toc)。要包含 toc,你需要以特定方式指定它。
以 h2 格式(markdown 中的 ##)编写 Table of contents,并将其放置在你希望它出现在文章中的位置。
例如,如果你想把目录放在介绍段落下面(像我通常做的那样),可以按以下方式操作。
---
# frontmatter
---
Here are some recommendations, tips & ticks for creating new posts in astro-minimax blog theme.
## Table of contents
<!-- the rest of the post -->md标题
关于标题有一点需要注意。astro-minimax 博客文章使用标题(frontmatter 中的 title)作为文章的主标题。因此,文章中的其余标题应使用 h2 ~ h6。
这条规则不是强制性的,但为了视觉效果、可访问性和 SEO,强烈建议这样做。
语法高亮
astro-minimax 使用 Shiki 作为默认语法高亮。从 astro-minimax v5.4 开始,使用 @shikijs/transformers 来增强代码块。如果你不想使用它,可以简单地像这样移除它
pnpm remove @shikijs/transformersbash// ...
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" }),
],
},
},
// ...
}js存储博客内容的图片
以下是在 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 -->md从技术上讲,你可以在
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">md额外内容
图片压缩
当你在博客文章中放置图片时(尤其是 public 目录下的图片),建议对图片进行压缩。这会影响网站的整体性能。
我推荐的图片压缩网站。
OG 图片
如果文章未指定 OG 图片,将使用默认 OG 图片。虽然不是必须的,但应在 frontmatter 中指定与文章相关的 OG 图片。OG 图片的推荐尺寸为 1200 X 640 px。
从 astro-minimax v1.4.0 开始,如果未指定,OG 图片将自动生成。查看公告。
评论区
文明评论,共建和谐社区