Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(sidebar): added the expansion sidebar first level directory implementation #371

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions docs/pages/en/integrations/vitepress-plugin-sidebar/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,99 @@ calculateSidebar([
```

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/`.

## Optional configuration
The above configuration completes the automatic generation of a sidebar that displays all Level 1 folders. Considering that you might want the `collapse:false` that is, the **automatically expands** mode, do as follows.
### Modify the source code of the plugin
Follow the path of `node_modules/@nolebase/vitepress-plugin-sidebar/dist/index.d.ts` to find the file and modify the code as follows.
get1024 marked this conversation as resolved.
Show resolved Hide resolved
```typescript
interface ArticleTree {
index: string;
text: string;
link?: string;
lastUpdated?: number;
collapsible?: true;
collapsed?: boolean; //true-->boolean // [!code focus]
items?: ArticleTree[];
category?: string;
}
declare function calculateSidebar(targets?: Array<string | {
folderName: string;
separate: boolean;
}>, base?: string): ArticleTree[] | Record<string, ArticleTree[]>;

export { calculateSidebar };
```
### Integrate with VitePress: Expand the Level 1 folder
In the VitePress configuration file (usually `docs/.vitepress/config.ts`, the file path and extension may be different), add **the highlight content** to the file.
```ts{1,5-19}
get1024 marked this conversation as resolved.
Show resolved Hide resolved
import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar";

...

function calculateSidebarWithDefaultOpen(targets, base) {
const result = originalCalculateSidebar(targets, base);
if (Array.isArray(result)) {
result.forEach(item => {
item.collapsed = false;
});
} else {
Object.values(result).forEach(items => {
items.forEach(item => {
item.collapsed = false;
});
});
}
return result;
}

...

export default defineConfig({
...
})
get1024 marked this conversation as resolved.
Show resolved Hide resolved
```

:::details Want to expand folders at all levels?
Try modifying the functions defined in the VitePress configuration file as follows
```ts
function calculateSidebarWithDefaultOpen(targets, base) {
const result = originalCalculateSidebar(targets, base);
function setAllCollapsedFalse(items) {
items.forEach(item => {
item.collapsible = true;
item.collapsed = false;

if (item.items) {
setAllCollapsedFalse(item.items);
}
});
}
if (Array.isArray(result)) {
setAllCollapsedFalse(result);
} else {
Object.values(result).forEach(items => {
setAllCollapsedFalse(items);
});
}
return result;
}
```
:::
get1024 marked this conversation as resolved.
Show resolved Hide resolved

### Modify the sidebar configuration
```ts
export default defineConfig({
...
themeConfig: {
...
sidebar: calculateSidebarWithDefaultOpen([ // [!code focus]
{ folderName: "folderName", separate: true },
{ folderName: "folderName", separate: true },
...
],''), //Set the 'base' parameter based on your requirements // [!code focus]
...
}
}
```
Refresh the front page and see that all the Level 1 directories have been expanded.
get1024 marked this conversation as resolved.
Show resolved Hide resolved
96 changes: 96 additions & 0 deletions docs/pages/zh-CN/integrations/vitepress-plugin-sidebar/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,99 @@ calculateSidebar([
```

那么,首级忽略 的规则将不再生效,取而代之的是,`'笔记'` 和 `'短文'` 会被作为目录的名称出现在访问路径为 `/` 下的页面,而 `文章` 会被作为一个独立的目录出现在访问路径为 `/文章/` 下的页面。

## 可选性配置
上述配置完成了自动生成侧边栏,效果为显示所有第一层级文件夹。考虑到你可能想要原生配置的`collapse:false`即**自动展开**模式,不妨按照如下操作。
### 修改插件源代码
按照`node_modules/@nolebase/vitepress-plugin-sidebar/dist/index.d.ts`路径找到文件,修改代码如下。
```typescript
interface ArticleTree {
index: string;
text: string;
link?: string;
lastUpdated?: number;
collapsible?: true;
collapsed?: boolean; //true-->boolean // [!code focus]
items?: ArticleTree[];
category?: string;
}
declare function calculateSidebar(targets?: Array<string | {
folderName: string;
separate: boolean;
}>, base?: string): ArticleTree[] | Record<string, ArticleTree[]>;

export { calculateSidebar };
```
### 为 VitePress 配置展开第一层级文件夹
在 VitePress 的配置文件中(通常为 `docs/.vitepress/config.ts`,文件路径和拓展名也许会有区别),将**高亮内容**添加到文件中。
```ts{1,5-19}
import { calculateSidebar as originalCalculateSidebar } from "@nolebase/vitepress-plugin-sidebar";

...

function calculateSidebarWithDefaultOpen(targets, base) {
const result = originalCalculateSidebar(targets, base);
if (Array.isArray(result)) {
result.forEach(item => {
item.collapsed = false;
});
} else {
Object.values(result).forEach(items => {
items.forEach(item => {
item.collapsed = false;
});
});
}
return result;
}

...

export default defineConfig({
...
})
```
get1024 marked this conversation as resolved.
Show resolved Hide resolved

:::details 想要展开所有层级的文件夹?
可以尝试把 VitePress 的配置文件定义的函数修改如下
```ts
function calculateSidebarWithDefaultOpen(targets, base) {
const result = originalCalculateSidebar(targets, base);
function setAllCollapsedFalse(items) {
items.forEach(item => {
item.collapsible = true;
item.collapsed = false;

if (item.items) {
setAllCollapsedFalse(item.items);
}
});
}
if (Array.isArray(result)) {
setAllCollapsedFalse(result);
} else {
Object.values(result).forEach(items => {
setAllCollapsedFalse(items);
});
}
return result;
}
```
:::

### 修改sidebar配置
```ts
export default defineConfig({
...
themeConfig: {
...
sidebar: calculateSidebarWithDefaultOpen([ // [!code focus]
{ folderName: "folderName", separate: true },
{ folderName: "folderName", separate: true },
...
],''), //base参数根据自身具体配置 // [!code focus]
...
}
}
```
get1024 marked this conversation as resolved.
Show resolved Hide resolved
刷新前端页面,可以看到所有第一层级目录已经展开。
Loading