Skip to content

Commit 592a7b5

Browse files
committed
Make sidebar config compatible with Vuepress 2
1 parent 6ddb19f commit 592a7b5

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/__tests__/sidebar.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ describe('test sidebar', () => {
2727

2828
const result = {
2929
[`/${codeFolder}/`]: [
30-
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'file1', 'file2'] },
3130
{
32-
title: 'lib',
31+
text: title,
3332
collapsable: false,
34-
children: ['src/lib/file3', 'src/lib/_index']
33+
children: [
34+
{ link: `/${codeFolder}/`, text: '::vuepress-jsdoc-title::' },
35+
`/${codeFolder}/file1`,
36+
`/${codeFolder}/file2`
37+
]
38+
},
39+
{
40+
text: 'lib',
41+
collapsable: false,
42+
children: [`/${codeFolder}/src/lib/file3`, `/${codeFolder}/src/lib/_index`]
3543
}
3644
]
3745
};

src/lib/vue-sidebar.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import { FileTree } from '../interfaces';
1111

1212
/**
1313
* Runs through the given tree structure and creates a vuepress config
14-
* @param {object} data Informations to build config
15-
* @param {array} data.fileTree tree strcture
14+
* @param {object} data Information to build config
15+
* @param {array} data.fileTree tree structure
1616
* @param {string} data.codeFolder ./code/ folder
1717
* @param {string} data.srcFolder ./src/ folder
1818
* @param {string} data.docsFolder ./documentation/ folder
1919
* @param {string} data.title title string
20-
* @returns {object} returns the vuepress menu strcture
20+
* @returns {object} returns the vuepress menu structure
2121
*/
2222
export const generateVueSidebar = ({
2323
fileTree,
@@ -32,20 +32,24 @@ export const generateVueSidebar = ({
3232
docsFolder: string;
3333
title: string;
3434
}) => {
35-
let rootFiles = [['', '::vuepress-jsdoc-title::']];
36-
rootFiles = rootFiles.concat(fileTree.filter((file: FileTree) => !file.children).map((file: FileTree) => file.name));
35+
let rootFiles = [{ link: `/${codeFolder}/`, text: '::vuepress-jsdoc-title::' }];
36+
rootFiles = rootFiles.concat(
37+
fileTree.filter((file: FileTree) => !file.children).map((file: FileTree) => `/${join(codeFolder, file.name)}`)
38+
);
3739

3840
const rootFolder = fileTree.filter((file: FileTree) => file.children && file.children.length > 0);
3941

40-
const buildChildren = (children: any[], name: string, depth: number) => {
42+
const buildChildren = (children: FileTree[], name: string, depth: number) => {
4143
let newChildren: any[] = [];
4244

4345
for (const child of children) {
4446
if (child.children && child.children.length > 0) {
4547
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
4648
} else if (child.fullPath) {
47-
if (fs.existsSync(join(docsFolder, child.fullPath.replace(srcFolder, '')) + '.md')) {
48-
newChildren.push(child.fullPath.replace(`${srcFolder}/`, ''));
49+
const path = child.fullPath.replace(srcFolder, '');
50+
51+
if (fs.existsSync(`${join(docsFolder, path)}.md`)) {
52+
newChildren.push(`/${join(codeFolder, path)}`);
4953
}
5054
}
5155
}
@@ -54,15 +58,15 @@ export const generateVueSidebar = ({
5458
};
5559

5660
const tree = rootFolder.map((folder: FileTree) => ({
57-
title: folder.name,
61+
text: folder.name,
5862
collapsable: false,
5963
children: buildChildren(folder.children!, folder.name, 0)
6064
}));
6165

6266
return {
6367
[`/${codeFolder}/`]: [
6468
{
65-
title,
69+
text: title,
6670
collapsable: false,
6771
children: rootFiles
6872
}

0 commit comments

Comments
 (0)