Skip to content

Commit

Permalink
Make sidebar config compatible with Vuepress 2
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenImp committed Jul 6, 2023
1 parent 6ddb19f commit 592a7b5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/__tests__/sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ describe('test sidebar', () => {

const result = {
[`/${codeFolder}/`]: [
{ title, collapsable: false, children: [['', '::vuepress-jsdoc-title::'], 'file1', 'file2'] },
{
title: 'lib',
text: title,
collapsable: false,
children: ['src/lib/file3', 'src/lib/_index']
children: [
{ link: `/${codeFolder}/`, text: '::vuepress-jsdoc-title::' },
`/${codeFolder}/file1`,
`/${codeFolder}/file2`
]
},
{
text: 'lib',
collapsable: false,
children: [`/${codeFolder}/src/lib/file3`, `/${codeFolder}/src/lib/_index`]
}
]
};
Expand Down
24 changes: 14 additions & 10 deletions src/lib/vue-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { FileTree } from '../interfaces';

/**
* Runs through the given tree structure and creates a vuepress config
* @param {object} data Informations to build config
* @param {array} data.fileTree tree strcture
* @param {object} data Information to build config
* @param {array} data.fileTree tree structure
* @param {string} data.codeFolder ./code/ folder
* @param {string} data.srcFolder ./src/ folder
* @param {string} data.docsFolder ./documentation/ folder
* @param {string} data.title title string
* @returns {object} returns the vuepress menu strcture
* @returns {object} returns the vuepress menu structure
*/
export const generateVueSidebar = ({
fileTree,
Expand All @@ -32,20 +32,24 @@ export const generateVueSidebar = ({
docsFolder: string;
title: string;
}) => {
let rootFiles = [['', '::vuepress-jsdoc-title::']];
rootFiles = rootFiles.concat(fileTree.filter((file: FileTree) => !file.children).map((file: FileTree) => file.name));
let rootFiles = [{ link: `/${codeFolder}/`, text: '::vuepress-jsdoc-title::' }];
rootFiles = rootFiles.concat(
fileTree.filter((file: FileTree) => !file.children).map((file: FileTree) => `/${join(codeFolder, file.name)}`)
);

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

const buildChildren = (children: any[], name: string, depth: number) => {
const buildChildren = (children: FileTree[], name: string, depth: number) => {
let newChildren: any[] = [];

for (const child of children) {
if (child.children && child.children.length > 0) {
newChildren = newChildren.concat(buildChildren(child.children, child.name, depth + 1));
} else if (child.fullPath) {
if (fs.existsSync(join(docsFolder, child.fullPath.replace(srcFolder, '')) + '.md')) {
newChildren.push(child.fullPath.replace(`${srcFolder}/`, ''));
const path = child.fullPath.replace(srcFolder, '');

if (fs.existsSync(`${join(docsFolder, path)}.md`)) {
newChildren.push(`/${join(codeFolder, path)}`);
}
}
}
Expand All @@ -54,15 +58,15 @@ export const generateVueSidebar = ({
};

const tree = rootFolder.map((folder: FileTree) => ({
title: folder.name,
text: folder.name,
collapsable: false,
children: buildChildren(folder.children!, folder.name, 0)
}));

return {
[`/${codeFolder}/`]: [
{
title,
text: title,
collapsable: false,
children: rootFiles
}
Expand Down

0 comments on commit 592a7b5

Please sign in to comment.