Skip to content

Commit a737b81

Browse files
authored
docs(sidebar): added the expansion sidebar first level directory implementation (#371)
* feat(sidebar):Added the expansion sidebar first level directory implementation * docs(sidebar):origin main * docs(sidebar):Added the expansion sidebar first level directory implementation * docs(sidebar): added the expansion sidebar first level directory implementation
1 parent a586f32 commit a737b81

2 files changed

Lines changed: 224 additions & 0 deletions

File tree

  • docs/pages
    • en/integrations/vitepress-plugin-sidebar
    • zh-CN/integrations/vitepress-plugin-sidebar

docs/pages/en/integrations/vitepress-plugin-sidebar/index.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,115 @@ calculateSidebar([
9494
```
9595

9696
Then the first-level ignore rule will no longer be in effect, and instead `'Notes'` and `'Tweets'` will appear as directory names on pages accessed under `/`, and `'Articles'` will appear as a separate directory on pages accessed under `/articles/`.
97+
98+
## Optional Configuration
99+
100+
The above configuration enables the automatic generation of sidebars. If you wish to achieve the **automatic expansion of the top-level folder in a specified path** using the [`collapse:false` option](https://vitepress.dev/zh/reference/default-theme-sidebar#collapsible-sidebar-groups) available in the native configuration, you can further try the following setup.
101+
102+
### Integrate with VitePress
103+
104+
In the VitePress configuration file (usually `docs/.vitepress/config.ts`, the file path and extension may be different).
105+
106+
```ts [config.ts]
107+
import { calculateSidebar } from '@nolebase/vitepress-plugin-sidebar' // [!code --]
108+
import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar" // [!code ++]
109+
//...
110+
function calculateSidebarWithDefaultOpen(targets, base) { // [!code ++]
111+
const result = originalCalculateSidebar(targets, base) // [!code ++]
112+
if (Array.isArray(result)) { // [!code ++]
113+
result.forEach(item => { // [!code ++]
114+
item.collapsed = false // [!code ++]
115+
}) // [!code ++]
116+
} else { // [!code ++]
117+
Object.values(result).forEach(items => { // [!code ++]
118+
items.forEach(item => { // [!code ++]
119+
item.collapsed = false // [!code ++]
120+
}) // [!code ++]
121+
}) // [!code ++]
122+
} // [!code ++]
123+
return result // [!code ++]
124+
} // [!code ++]
125+
//...
126+
export default defineConfig({
127+
//...
128+
})
129+
```
130+
131+
### Update Sidebar Configuration
132+
133+
```ts [config.ts]
134+
export default defineConfig({
135+
//...
136+
themeConfig: {
137+
//...
138+
sidebar: calculateSidebarWithDefaultOpen([ // [!code focus]
139+
{ folderName: "A", separate: true },
140+
{ folderName: "B", separate: true },
141+
//...
142+
],''), //The base parameter should be set according to your specific configuration // [!code focus]
143+
//...
144+
}
145+
}
146+
```
147+
148+
::: details What is `base` ?
149+
150+
In the `config.ts` file, locate the line where you import the function: `import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar";`.
151+
152+
Hover over `calculateSidebar`, then click to navigate to the `index.d.ts` file. You will see something like the following:
153+
154+
```ts{11-14} [index.d.ts]
155+
interface ArticleTree {
156+
index: string;
157+
text: string;
158+
link?: string;
159+
lastUpdated?: number;
160+
collapsible?: boolean;
161+
collapsed?: boolean;
162+
items?: ArticleTree[];
163+
category?: string;
164+
}
165+
declare function calculateSidebar(targets?: Array<string | {
166+
folderName: string;
167+
separate: boolean;
168+
}>, base?: string): ArticleTree[] | Record<string, ArticleTree[]>;
169+
170+
export { calculateSidebar };
171+
```
172+
173+
From this, we can observe that `calculateSidebar()` accepts two parameters `(target, base)`.
174+
175+
`target` is the string or object parameter passed in the configuration file.
176+
177+
`base` refers to the base path of your VitePress project, typically set as `' '`.
178+
:::
179+
180+
The sidebar will display **the contents within the folder names specified in the configuration** and the top-level folders will be expanded.
181+
182+
:::details Want to expand all levels of folders to their deepest files?
183+
184+
You can try modifying the function defined in the VitePress configuration file as follows:
185+
186+
```ts [config.ts]
187+
function calculateSidebarWithDefaultOpen(targets, base) {
188+
const result = originalCalculateSidebar(targets, base)
189+
function setAllCollapsedFalse(items) {
190+
items.forEach(item => {
191+
item.collapsible = true
192+
item.collapsed = false
193+
if (item.items) {
194+
setAllCollapsedFalse(item.items)
195+
}
196+
})
197+
}
198+
if (Array.isArray(result)) {
199+
setAllCollapsedFalse(result)
200+
} else {
201+
Object.values(result).forEach(items => {
202+
setAllCollapsedFalse(items)
203+
})
204+
}
205+
return result
206+
}
207+
```
208+
:::

docs/pages/zh-CN/integrations/vitepress-plugin-sidebar/index.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,115 @@ calculateSidebar([
9292
```
9393

9494
那么,首级忽略 的规则将不再生效,取而代之的是,`'笔记'``'短文'` 会被作为目录的名称出现在访问路径为 `/` 下的页面,而 `文章` 会被作为一个独立的目录出现在访问路径为 `/文章/` 下的页面。
95+
96+
## 可选性配置
97+
98+
上述配置完成了自动生成侧边栏,考虑到您可能想要原生配置的[`collapse:false` 选项](https://vitepress.dev/zh/reference/default-theme-sidebar#collapsible-sidebar-groups)实现的**指定路径下首级文件夹自动展开**效果,可以继续尝试进行下述配置。
99+
100+
### 为 VitePress 配置
101+
102+
在 VitePress 的配置文件中(通常为 `docs/.vitepress/config.ts`,文件路径和拓展名也许会有区别)。
103+
104+
```ts [config.ts]
105+
import { calculateSidebar } from '@nolebase/vitepress-plugin-sidebar' // [!code --]
106+
import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar" // [!code ++]
107+
//...
108+
function calculateSidebarWithDefaultOpen(targets, base) { // [!code ++]
109+
const result = originalCalculateSidebar(targets, base) // [!code ++]
110+
if (Array.isArray(result)) { // [!code ++]
111+
result.forEach(item => { // [!code ++]
112+
item.collapsed = false // [!code ++]
113+
}) // [!code ++]
114+
} else { // [!code ++]
115+
Object.values(result).forEach(items => { // [!code ++]
116+
items.forEach(item => { // [!code ++]
117+
item.collapsed = false // [!code ++]
118+
}) // [!code ++]
119+
}) // [!code ++]
120+
} // [!code ++]
121+
return result // [!code ++]
122+
} // [!code ++]
123+
//...
124+
export default defineConfig({
125+
//...
126+
})
127+
```
128+
129+
### 修改sidebar配置
130+
131+
```ts [config.ts]
132+
export default defineConfig({
133+
//...
134+
themeConfig: {
135+
//...
136+
sidebar: calculateSidebarWithDefaultOpen([ // [!code focus]
137+
{ folderName: "A", separate: true },
138+
{ folderName: "B", separate: true },
139+
//...
140+
],''), //base参数根据自身具体配置 // [!code focus]
141+
//...
142+
}
143+
}
144+
```
145+
146+
::: details `base`是什么?
147+
148+
找到先前在`config.ts`文件中的引入`import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar";`
149+
150+
鼠标置于`calculateSidebar`上,左键单击进入`index.d.ts`文件,如下:
151+
152+
```ts{11-14} [index.d.ts]
153+
interface ArticleTree {
154+
index: string;
155+
text: string;
156+
link?: string;
157+
lastUpdated?: number;
158+
collapsible?: boolean;
159+
collapsed?: boolean;
160+
items?: ArticleTree[];
161+
category?: string;
162+
}
163+
declare function calculateSidebar(targets?: Array<string | {
164+
folderName: string;
165+
separate: boolean;
166+
}>, base?: string): ArticleTree[] | Record<string, ArticleTree[]>;
167+
168+
export { calculateSidebar };
169+
```
170+
171+
观察到`calculateSidebar()`有两个参数`(target, base)`
172+
173+
`targe`是在配置文件中传入的 字符串参数 或 对象参数。
174+
175+
`base`是你的vitepress项目配置的基路径,通常情况下为`' '`即可。
176+
:::
177+
178+
注意到,侧边栏显示结果为**当前所配置的文件夹名路径下的内容**,并且路径下首级文件夹已经展开。
179+
180+
:::details 想要展开所有层级的文件夹至最末端文件?
181+
182+
可以尝试把 VitePress 的配置文件定义的函数修改如下:
183+
184+
```ts [config.ts]
185+
function calculateSidebarWithDefaultOpen(targets, base) {
186+
const result = originalCalculateSidebar(targets, base)
187+
function setAllCollapsedFalse(items) {
188+
items.forEach(item => {
189+
item.collapsible = true
190+
item.collapsed = false
191+
if (item.items) {
192+
setAllCollapsedFalse(item.items)
193+
}
194+
})
195+
}
196+
if (Array.isArray(result)) {
197+
setAllCollapsedFalse(result)
198+
} else {
199+
Object.values(result).forEach(items => {
200+
setAllCollapsedFalse(items)
201+
})
202+
}
203+
return result
204+
}
205+
```
206+
:::

0 commit comments

Comments
 (0)