Conf
Docusaurus is so popular because its configuration is simple, it can achieve powerful functions with simple configuration, and supports rich customization capabilities. Due To The Official Default Docs Style Configuration, I Found That Docusaurus Is Also Suitable As A Book Style. Of Course, You Can Also Customize The Development. I'm Just Using The Common Configuration Functions Provided By The Official To Demonstrate.
This article is about some common configurations in two styles. Of course, there are many official configurations. Here are only the common configurations. For more configurations, please refer to the official documents.
Sidebar
There are two ways to use the sidebar, one is manual configuration, and the other can be automatically generated according to the specified path. No matter which method is used, it requires two steps:
1. Definition sidebar.ts
There are many types when configuring the sidebar.
- Doc: link to a doc page, associating it with the sidebar
- Link: link to any internal or external page
- Category: creates a dropdown of sidebar items
- Autogenerated: generate a sidebar slice automatically
- HTML: renders pure HTML in the item's position
- *Ref: link to a doc page, without making the item take part in navigation generation
- Doc
- Link
- Category
- HTML
- Autogenerated
- Ref
export default {
mySidebar: [
// 常规语法:
{
type: 'doc',
id: 'doc1', // 文档 ID
label: 'Getting started', // 侧边栏标签
},
// 简写语法:
'doc2', // 文档 ID
],
};
export default {
myLinksSidebar: [
// 外部链接
{
type: 'link',
label: 'Facebook', // 链接标签
href: 'https://facebook.com', // 外部 URL
},
// 内部链接
{
type: 'link',
label: 'Home', // 链接标签
href: '/', // 内部路径
},
],
};
export default {
docs: [
{
type: 'category',
label: 'Guides',
link: {
// 快速生成介绍文档
type: 'generated-index',
// 侧边栏标签
title: 'Docusaurus 教程',
// Guides 目录介绍
description: '学习最重要的 Docusaurus 概念!',
// 自定义生成页面的路径
slug: '/category/docusaurus-guides',
keywords: ['guides'],
image: '/img/docusaurus.png',
},
// 侧边栏 Guides 目录列表
items: [
'creating-pages',
{
type: 'category',
label: 'Docs',
items: ['introduction', 'sidebar', 'markdown-features', 'versioning'],
},
],
},
],
};
Under the Category or Autogenerated type, the sidebar index page configuration can be configured using the 'category.yml' or 'category.json' file
- _category_.yml
- _category_.json
yml configuration, recommended use, when the link is empty, the index page will not be generated.
position: 2.5 # 支持浮点数序号
label: '教程' # 侧边栏标签
collapsible: true # 让类别可折叠
collapsed: false # 让类别默认开启
className: red # 标签类名定义
link:
type: generated-index
title: 教程总览
customProps:
description: 这个描述可以用在 swizzle 的 DocCard 里
json configuration, 'description' index page text content
{
"position": 2.5,
"label": "教程",
"collapsible": true,
"collapsed": false,
"className": "red",
"link": {
"type": "generated-index",
"title": "教程总览"
},
"customProps": {
"description": "这个描述可以用在 swizzle 的 DocCard 里"
}
}
export default {
myHtmlSidebar: [
{
type: 'html', // 要渲染的 HTML
value: '<img src="sponsor.png" alt="Sponsor" />',
defaultStyle: true, // 使用默认的菜单项目样式
},
],
};
export default {
myAutogeneratedSidebar: [
{
type: 'autogenerated',
dirName: '.', // '.' 即当前 docs 文件夹
},
],
};
export default {
tutorialSidebar: {
'Category A': [
'doc1',
'doc2',
{type: 'ref', id: 'commonDoc'},
'doc5',
],
},
apiSidebar: ['doc3', 'doc4', 'commonDoc'],
};
2. docusaurus.config.ts
直接将此对象传入 @docusaurus/plugin-docs
或通过 @docusaurus/preset-classic
传入
export default {
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
sidebarPath: './sidebars.ts',
},
},
],
],
};
Theme
Available by Infima generate theme colors
/* 默认颜色. */
:root {
--ifm-color-primary: #2e8555;
--ifm-color-primary-dark: #29784c;
--ifm-color-primary-darker: #277148;
--ifm-color-primary-darkest: #205d3b;
--ifm-color-primary-light: #33925d;
--ifm-color-primary-lighter: #359962;
--ifm-color-primary-lightest: #3cad6e;
--ifm-code-font-size: 95%;
--docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.1);
}
/* dark 模式颜色. */
[data-theme='dark'] {
--ifm-color-primary: #25c2a0;
--ifm-color-primary-dark: #21af90;
--ifm-color-primary-darker: #1fa588;
--ifm-color-primary-darkest: #1a8870;
--ifm-color-primary-light: #29d5b0;
--ifm-color-primary-lighter: #32d8b4;
--ifm-color-primary-lightest: #4fddbf;
--docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.3);
}
Code blocks
- Docusaurus: Default supported highlighted languages
- Prism: Configurable support language
The following shows the language configuration that Docusaurus does not support by default.
const config: Config = {
themeConfig: {
prism: {
additionalLanguages: [
'java',
'bash',
'json'
],
}
}
}
In 3.4.0 Version, Due to jiti version issues Add languages not supported by Docusaurus, It does not take effect. The version of jiti can be mandatory.
package.json"resolutions": {
"jiti": "1.21.0"},
📄️ Docs config
The document style referred to here refers to having a Home pag, which is very suitable as a comprehensive technical website, eg. showcase
📄️ Book config
The Book Style mentioned here refers to a website that does not have a Home page. This style is very suitable for the output of pure technical content. It is a replacement of hugo-book.