Skip to content

Commit 054f99c

Browse files
committed
✨ feat: Support for custom sidebar configuration
Signed-off-by: sqrtthree <[email protected]>
1 parent 5cd9b0b commit 054f99c

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

docs/zh/configurations/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,153 @@ footer: Open Source on [GitHub](https://github.com/sqrthree/vuepress-theme-api),
8888
- 检索该目录下的其他文件,作为该分组的剩余导航项;
8989
- 分析文件中的最 `top` 级标题,做为该文件对应的导航项中的子导航项(默认折叠)。
9090

91+
### 自定义侧边栏
92+
93+
如果不满意默认的侧边栏,你也可以通过配置 `themeConfig.sidebar` 来控制侧边栏。
94+
95+
配置时 `themeConfig.sidebar` 的值请参考以下结构:
96+
97+
```js
98+
themeConfig: {
99+
sidebar: {
100+
'分组名称': {
101+
title: '基本配置', // 导航项文字。
102+
to: '/zh/configurations/', // 导航项链接地址。
103+
104+
// 该分组下的其他导航项,默认展示。
105+
children: [
106+
{
107+
title: '内置组件',
108+
to: '/zh/configurations/components.html',
109+
110+
// 该导航项所包含的子导航项,默认被折叠。用于展示某个页面的所有标题导航。
111+
headers: [
112+
{
113+
title: 'Block 组件',
114+
slug: 'block-组件', // 当前页面的 id 锚点。
115+
},
116+
],
117+
},
118+
],
119+
},
120+
},
121+
},
122+
```
123+
124+
如果有多语言配置需求,可以配置 `themeConfig.locales[LANG].sidebar` 项:
125+
126+
```js
127+
themeConfig: {
128+
locales: {
129+
'/': {
130+
sidebar: {
131+
'getting-started': {
132+
title: 'Getting Started',
133+
to: '/getting-started/',
134+
children: [],
135+
},
136+
},
137+
},
138+
'/zh/': {
139+
'分组名称': {
140+
title: '基本配置', // 导航项文字。
141+
to: '/zh/configurations/', // 导航项链接地址。
142+
143+
// 该分组下的其他导航项,默认展示。
144+
children: [
145+
{
146+
title: '内置组件',
147+
to: '/zh/configurations/components.html',
148+
149+
// 该导航项所包含的子导航项,默认被折叠。用于展示某个页面的所有标题导航。
150+
headers: [
151+
{
152+
title: 'Block 组件',
153+
slug: 'block-组件', // 当前页面的 id 锚点。
154+
},
155+
],
156+
},
157+
],
158+
},
159+
},
160+
},
161+
},
162+
```
163+
164+
<Example>
165+
166+
自定义导航:
167+
168+
```js
169+
// .vuepress/config.js
170+
themeConfig: {
171+
sidebar: {
172+
"configurations": {
173+
"title": "基本配置",
174+
"to": "/zh/configurations/",
175+
"children": [
176+
{
177+
"title": "内置组件",
178+
"to": "/zh/configurations/components.html",
179+
"headers": [
180+
{
181+
"title": "Block 组件",
182+
"slug": "block-组件"
183+
},
184+
]
185+
},
186+
{
187+
"title": "文件模板",
188+
"to": "/zh/configurations/template.html",
189+
"headers": []
190+
}
191+
],
192+
},
193+
}
194+
}
195+
```
196+
197+
多语言下的自定义导航:
198+
199+
```js
200+
// .vuepress/config.js
201+
themeConfig: {
202+
locales: {
203+
'/': {
204+
sidebar: {
205+
'getting-started': {
206+
title: 'Getting Started',
207+
to: '/getting-started/',
208+
children: [],
209+
},
210+
},
211+
},
212+
'/zh/': {
213+
sidebar: {
214+
'分组名称': {
215+
title: '基本配置',
216+
to: '/zh/configurations/',
217+
children: [
218+
{
219+
title: '内置组件',
220+
to: '/zh/configurations/components.html',
221+
headers: [
222+
{
223+
title: 'Block 组件',
224+
slug: 'block-组件',
225+
},
226+
],
227+
},
228+
],
229+
},
230+
},
231+
},
232+
},
233+
},
234+
```
235+
236+
</Example>
237+
91238
</Block>
92239

93240
<Block>

utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function matchLocalePathFromPath(path, locales) {
6868
}
6969

7070
export function resolveSidebarItems($page, $site, $localePath) {
71+
const { themeConfig } = $site
7172
const sidebars = {}
7273

7374
if ($site.locales) {
@@ -98,6 +99,17 @@ export function resolveSidebarItems($page, $site, $localePath) {
9899
}
99100
}
100101

102+
const sidebarConfig =
103+
themeConfig.locales &&
104+
themeConfig.locales[$localePath] &&
105+
themeConfig.locales[$localePath].sidebar
106+
? themeConfig.locales[$localePath].sidebar
107+
: themeConfig.sidebar
108+
109+
if (sidebarConfig) {
110+
return Object.assign(sidebars, sidebarConfig)
111+
}
112+
101113
$site.pages
102114
.filter(item => {
103115
// Only show current locales in sidebar

0 commit comments

Comments
 (0)