博客
博客功能使您可以快速部署一个功能齐全的博客。
查看 博客插件 API 参考文档 以获取所有选项的详尽列表。
初始设置
要设置站点的博客,首先创建一个 blog
目录。
然后,在 docusaurus.config.js
中添加指向博客的项目链接:
export default {
themeConfig: {
// ...
navbar: {
items: [
// ...
{to: 'blog', label: '博客', position: 'left'}, // 或 position: 'right'
],
},
},
};
添加文章
要在博客中发布文章,请在博客目录中创建一个 Markdown 文件。
例如,在 website/blog/2019-09-05-hello-docusaurus.md
创建一个文件:
---
title: 欢迎使用 Docusaurus
description: 这是我在 Docusaurus 上的第一篇文章。
slug: welcome-docusaurus-v2
authors:
- name: Joel Marcey
title: Docusaurus 1 的共同创建者
url: https://github.com/JoelMarcey
image_url: https://github.com/JoelMarcey.png
socials:
x: joelmarcey
github: JoelMarcey
- name: Sébastien Lorber
title: Docusaurus 维持者
url: https://sebastienlorber.com
image_url: https://github.com/slorber.png
socials:
x: sebastienlorber
github: slorber
tags: [hello, docusaurus-v2]
image: https://i.imgur.com/mErPwqL.png
hide_table_of_contents: false
---
欢迎来到这个博客。这个博客是用 [**Docusaurus 2**](https://docusaurus.io/) 创建的。
<!-- truncate -->
这是我在 Docusaurus 2 上的第一篇文章。
接下来是一系列的探索。
前置 matter 用于向博客文章添加更多元数据,例如作者信息,但 Docusaurus 能够在没有前置 matter 的情况下推断所有必要的元数据。有关所有可能的字段,请参阅 API 文档 。
博客列表
博客的索引页面(默认情况下,位于 /blog
)是_博客列表页面_,所有博客文章都集中显示在此页面上。
在您的博客文章中使用 <!--truncate-->
标记来表示在查看所有已发布的博客文章时将显示的内容摘要。 <!--truncate-->
上面的任何内容都将成为摘要的一部分。请注意,截断标记以上的部分必须是独立的可渲染 Markdown。例如:
---
title: Markdown 博客截断示例
---
所有这些都将成为博客文章摘要的一部分。
<!-- truncate -->
但从这里开始的任何内容都不会。
对于使用 .mdx
扩展名的文件,请改用 MDX 注释 {/* truncate */}
:
---
title: MDX 博客截断示例
---
所有这些都将成为博客文章摘要的一部分。
{/* truncate */}
但从这里开始的任何内容都不会。
默认情况下,每个博客列表页面显示 10 篇文章,但是您可以使用插件配置中的 postsPerPage
选项来控制分页。如果将 postsPerPage
设置为 'ALL'
,则将禁用分页,并且所有文章都将显示在第一页上。您还可以为博客列表页面添加元描述以改善 SEO:
export default {
// ...
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
blogTitle: 'Docusaurus 博客!',
blogDescription: '一个由 Docusaurus 提供支持的博客!',
postsPerPage: 'ALL',
},
},
],
],
};
博客侧边栏
博客侧边栏显示最近的博客文章。默认显示的项目数量为 5,但您可以使用插件配置中的 blogSidebarCount
选项进行自定义。通过设置 blogSidebarCount: 0
,侧边栏将被完全禁用,容器也将被移除。这将增加主容器的宽度。特别是,如果您设置了 blogSidebarCount: 'ALL'
,则将显示_所有_文章。
您还可以使用 blogSidebarTitle
选项更改侧边栏标题文本。例如,如果您设置了 blogSidebarCount: 'ALL'
,则您可以将其改为“所有文章”,而不是默认的“最近的文章”:
export default {
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
blogSidebarTitle: '所有文章',
blogSidebarCount: 'ALL',
},
},
],
],
};
博客文章日期
Docusaurus 将从许多模式(例如 YYYY-MM-DD-my-blog-post-title.md
或 YYYY/MM/DD/my-blog-post-title.md
)中提取 YYYY-MM-DD
日期。这使您可以轻松地按年份、月份对博客文章进行分组,或使用扁平结构。
支持的日期提取模式
模式 | 示例 |
---|---|
单个文件 | 2021-05-28-my-blog-post-title.md |
MDX 文件 | 2021-05-28-my-blog-post-title.mdx |
单个文件夹 + index.md | 2021-05-28-my-blog-post-title/index.md |
按日期命名的文件夹 | 2021-05-28/my-blog-post-title.md |
按日期嵌套的文件夹 | 2021/05/28/my-blog-post-title.md |
部分按日期嵌套的文件夹 | 2021/05-28-my-blog-post-title.md |
嵌套文件夹 + index.md | 2021/05/28/my-blog-post-title/index.md |
路径中间的日期 | category/2021/05-28-my-blog-post-title.md |
Docusaurus 可以使用上述任何命名模式从文章中提取日期。建议选择一种模式并将其应用于所有文章,以避免混淆。
使用文件夹可以方便地将博客文章图像与 Markdown 文件一起存放。
此命名约定是可选的,您也可以提供日期作为前置 matter。由于前置 matter 遵循支持日期时间表示法的 YAML 语法,因此如果您需要更精细的发布日期,可以使用前置 matter。例如,如果您在同一天发布了多篇文章,您可以根据一天中的时间对它们进行排序:
---
date: 2021-09-13T10:00
---
---
date: 2021-09-13T18:00
---
博客文章作者
使用 authors
前置 matter 字段来声明博客文章作者。作者至少应该有一个 name
或 image_url
。Docusaurus 使用 url
、email
和 title
等信息,但允许任何其他信息。
行内作者
博客文章作者可以直接在 front matter 中声明:
- 单个作者
- 多个作者
---
authors:
name: Joel Marcey
title: Docusaurus 1 的共同创建者
url: https://github.com/JoelMarcey
image_url: https://github.com/JoelMarcey.png
email: [email protected]
socials:
x: joelmarcey
github: JoelMarcey
---
---
authors:
- name: Joel Marcey
title: Docusaurus 1 的共同创建者
url: https://github.com/JoelMarcey
image_url: https://github.com/JoelMarcey.png
email: [email protected]
socials:
x: joelmarcey
github: JoelMarcey
- name: Sébastien Lorber
title: Docusaurus 维持者
url: https://sebastienlorber.com
image_url: https://github.com/slorber.png
socials:
x: sebastienlorber
github: slorber
---
此选项最适合入门或处理非正式的、不规律的作者。
建议使用 authors
前置 matter,但旧的 author_*
前置 matter 仍然受支持:
---
author: Joel Marcey
author_title: Docusaurus 1 的共同创建者
author_url: https://github.com/JoelMarcey
author_image_url: https://github.com/JoelMarcey.png
---
全局作者
对于经常撰写博客文章的作者,在每篇博客文章中维护作者信息可能会很繁琐。
可以在配置文件中全局声明这些作者:
jmarcey:
name: Joel Marcey
title: Docusaurus 1 的共同创建者
url: https://github.com/JoelMarcey
image_url: https://github.com/JoelMarcey.png
email: [email protected]
socials:
x: joelmarcey
github: JoelMarcey
slorber:
name: Sébastien Lorber
title: Docusaurus 维持者
url: https://sebastienlorber.com
image_url: https://github.com/slorber.png
socials:
x: sebastienlorber
github: slorber
使用 authorsMapPath
插件选项配置路径。也支持 JSON。
在博客文章的前置 matter 中,您可以引用在全局配置文件中声明的作者:
- 单个作者
- 多个作者
---
authors: jmarcey
---
---
authors: [jmarcey, slorber]
---
您可以大部分时间使用全局作者,并且仍然可以使用行内作者: 您可以根据每篇博客文章自定义全局作者的数据: 配置文件可以本地化,只需在以下位置创建一个本地化副本:authors
系统非常灵活,可以满足更高级的用例:混合使用行内作者和全局作者
---
authors:
- jmarcey
- slorber
- name: 行内作者姓名
title: 行内作者标题
url: https://github.com/inlineAuthor
image_url: https://github.com/inlineAuthor
---全局作者的本地覆盖
---
authors:
- key: jmarcey
title: Joel Marcey 的新标题
- key: slorber
name: Sébastien Lorber 的新姓名
---本地化作者配置文件
website/i18n/[locale]/docusaurus-plugin-content-blog/authors.yml
通过前置 matter 或作者映射声明的作者都需要具有姓名或头像,或两者兼有。如果文章的所有作者都没有姓名,Docusaurus 将紧凑地显示他们的头像。有关效果,请参见 这篇测试文章 。
RSS Feed 要求设置作者的电子邮件才能使作者出现在 Feed 中。
作者页面
作者页面功能是可选的,主要用于多作者博客。
您可以通过向 全局作者配置 添加 page: true
属性来独立为每个作者激活它:
slorber:
name: Sébastien Lorber
page: true # 启用此功能 - 路由将为 /authors/slorber
jmarcey:
name: Joel Marcey
page:
# 启用此功能 - 路由将为 /authors/custom-author-url
permalink: '/custom-author-url'
博客插件现在将生成:
博客文章标签
标签在 front matter 中声明,并引入另一个分类维度。
可以内联定义标签,也可以引用在 tags 文件
中声明的预定义标签(可选,通常为 blog/tags.yml
)。
在以下示例中:
docusaurus
引用在blog/tags.yml
中声明的预定义标签键。Releases
是一个内联标签,因为它不存在于blog/tags.yml
中。
---
title: '我的博客文章'
tags:
- Releases
- docusaurus
---
内容
docusaurus:
label: 'Docusaurus'
permalink: '/docusaurus'
description: '与 Docusaurus 框架相关的博客文章'
阅读时间
Docusaurus 基于字数为每篇博客文章生成阅读时间估算。我们提供了一个自定义此选项的选项。
export default {
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
showReadingTime: true, // 设置为 false 时,不会显示“x 分钟阅读”
readingTime: ({content, frontMatter, defaultReadingTime}) =>
defaultReadingTime({content, options: {wordsPerMinute: 300}}),
},
},
],
],
};
readingTime
回调函数接收三个参数:博客内容文本(字符串)、front matter(字符串键及其值的记录)和默认阅读时间函数。它返回一个数字(以分钟为单位的阅读时间)或 undefined
(禁用此页面的阅读时间)。
默认阅读时间能够接受其他选项:wordsPerMinute
为数字(默认值:300),wordBound
为从字符串到布尔值的函数。如果传递给 wordBound
的字符串应该是单词边界(默认情况下为空格、制表符和换行符),则该函数应返回 true
。
将回调函数用于所有自定义需求:
- 按文章禁用
- 传递选项
- 使用自定义算法
禁用一页的阅读时间:
export default {
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
showReadingTime: true,
readingTime: ({content, frontMatter, defaultReadingTime}) =>
frontMatter.hide_reading_time
? undefined
: defaultReadingTime({content}),
},
},
],
],
};
用法:
---
hide_reading_time: true
---
此页面将不再显示阅读时间统计信息!
将选项传递给默认阅读时间函数:
export default {
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
readingTime: ({content, defaultReadingTime}) =>
defaultReadingTime({content, options: {wordsPerMinute: 100}}),
},
},
],
],
};
使用阅读时间的自定义实现:
import myReadingTime from './myReadingTime';
export default {
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
readingTime: ({content}) => myReadingTime(content),
},
},
],
],
};
Feed
您可以通过传递 feedOptions
来生成 RSS/Atom/JSON Feed。默认情况下,会生成 RSS 和 Atom Feed。要禁用 Feed 生成,请将 feedOptions.type
设置为 null
。
type FeedType = 'rss' | 'atom' | 'json';
type BlogOptions = {
feedOptions?: {
type?: FeedType | 'all' | FeedType[] | null;
title?: string;
description?: string;
copyright: string;
language?: string; // 可能的值:http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
limit?: number | false | null; // 默认值为 20
// XSLT 允许浏览器很好地设置样式并呈现 Feed XML 文件
xslt?:
| boolean
| {
//
rss?: string | boolean;
atom?: string | boolean;
};
// 允许控制 BlogFeedItems 的构建
createFeedItems?: (params: {
blogPosts: BlogPost[];
siteConfig: DocusaurusConfig;
outDir: string;
defaultCreateFeedItems: (params: {
blogPosts: BlogPost[];
siteConfig: DocusaurusConfig;
outDir: string;
}) => Promise<BlogFeedItem[]>;
}) => Promise<BlogFeedItem[]>;
};
};
示例用法:
export default {
// ...
presets: [
[
'@docusaurus/preset-classic',
{
blog: {
feedOptions: {
type: 'all',
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
createFeedItems: async (params) => {
const {blogPosts, defaultCreateFeedItems, ...rest} = params;
return defaultCreateFeedItems({
// 在 Feed 中仅保留最近的 10 篇博客文章
blogPosts: blogPosts.filter((item, index) => index < 10),
...rest,
});
},
},
},
},
],
],
};
可以在以下位置找到 Feed:
- RSS
- Atom
- JSON
https://example.com/blog/rss.xml
https://example.com/blog/atom.xml
https://example.com/blog/feed.json
高级主题
只使用博客模式
您可以运行您的 Docusaurus 站点,而无需专用的登录页面,而是将博客的文章列表页面作为索引页面。将 routeBasePath
设置为 '/'
,以便通过根路由 example.com/
(而不是子路由 example.com/blog/
)提供博客服务。
export default {
// ...
presets: [
[
'@docusaurus/preset-classic',
{
docs: false, // 可选:禁用文档插件
blog: {
routeBasePath: '/', // 在站点的根目录提供博客服务
/* 其他博客选项 */
},
},
],
],
};
不要忘记删除位于 ./src/pages/index.js
的现有主页,否则将有两个文件映射到相同的路由!
如果您禁用了文档插件,请不要忘记删除配置文件中其他地方对文档插件的引用。尤其要注意删除与文档相关的导航栏项目。
对于只想使用文档的用户,还有一个“仅文档模式”。阅读 仅文档模式 以获取详细说明或更详细的 routeBasePath
解释。
多个博客
默认情况下,经典主题假定每个网站只有一个博客,因此只包含一个博客插件实例。如果您想在一个网站上拥有多个博客,这也是可能的!您可以在 docusaurus.config.js
的 plugins
选项中指定另一个博客插件来添加另一个博客。
将 routeBasePath
设置为您希望访问第二个博客的 URL 路由。请注意,此处的 routeBasePath
必须与第一个博客不同,否则可能会发生路径冲突!此外,将 path
设置为包含第二个博客条目的目录的路径。
如 多实例插件 中所述,您需要为插件分配一个唯一的 ID。
export default {
// ...
plugins: [
[
'@docusaurus/plugin-content-blog',
{
/**
* 任何多实例插件都需要此项
*/
id: 'second-blog',
/**
* 网站博客部分的 URL 路由。
* *不要*包含尾部斜杠。
*/
routeBasePath: 'my-second-blog',
/**
* 相对于站点目录的文件系统上的数据路径。
*/
path: './my-second-blog',
},
],
],
};
例如,我们在这里托管第二个博客 此处 。