Skip to content

Commit

Permalink
fix(vitepress): fix sidebar links when "out" and "docsRoot" are the s…
Browse files Browse the repository at this point in the history
…ame value (#695)
  • Loading branch information
tgreyuk committed Sep 30, 2024
1 parent 974a60d commit bf5c175
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-spies-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typedoc-vitepress-theme': patch
---

- Fix sidebar links when "out" and "docsRoot" are the same value (#695)
5 changes: 2 additions & 3 deletions devtools/examples/vitepress/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": [
"../../../packages/typedoc-plugin-markdown/test/fixtures/typedoc.cjs"
],
"extends": ["../../packages/fixtures/typedoc.cjs"],
"tsconfig": "../../../packages/typedoc-plugin-markdown/test/fixtures/tsconfig.json",
"entryPoints": [
"../../../packages/typedoc-plugin-markdown/test/fixtures/src/groups/**/*.ts"
],
Expand Down
15 changes: 13 additions & 2 deletions packages/typedoc-vitepress-theme/src/sidebars/sidebar.vitepress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ function getNavigationItem(
) {
const hasChildren = navigationItem?.children?.length;

const linkParts: string[] = [];

if (navigationItem?.path) {
if (basePath.length) {
linkParts.push(basePath);
}
linkParts.push(
getParsedUrl(navigationItem.path as string).replace(/\\/g, '/'),
);
}

return {
text: navigationItem.title,
...(Boolean(navigationItem?.path) && {
link: `/${basePath}/${getParsedUrl(navigationItem.path as string).replace(/\\/g, '/')}`,
...(linkParts.length && {
link: `/${linkParts.join('/')}`,
}),
...(hasChildren && { collapsed: options.collapsed }),
...(hasChildren && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fs.removeSync(`./test/out`);
const fixtures = [
{ options: 'typedoc.base.json', outDir: 'default' },
{ options: 'typedoc.sidebar-options.json', outDir: 'sidebar-options' },
{ options: 'typedoc.sidebar-options-2.json', outDir: 'sidebar-options-2' },
];

// write fixtures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`VitePress should generate typedoc sidebar 1`] = `"[{"text":"module-1","link":"/test/out/default/module-1/","collapsed":true,"items":[{"text":"Classes","collapsed":true,"items":[{"text":"ClassA","link":"/test/out/default/module-1/classes/ClassA.md"},{"text":"ClassB","link":"/test/out/default/module-1/classes/ClassB.md"}]},{"text":"Interfaces","collapsed":true,"items":[{"text":"InterfaceA","link":"/test/out/default/module-1/interfaces/InterfaceA.md"},{"text":"InterfaceB","link":"/test/out/default/module-1/interfaces/InterfaceB.md"}]}]},{"text":"module-2","link":"/test/out/default/module-2/","collapsed":true,"items":[{"text":"Classes","collapsed":true,"items":[{"text":"ClassA","link":"/test/out/default/module-2/classes/ClassA.md"},{"text":"ClassB","link":"/test/out/default/module-2/classes/ClassB.md"}]},{"text":"Interfaces","collapsed":true,"items":[{"text":"InterfaceA","link":"/test/out/default/module-2/interfaces/InterfaceA.md"},{"text":"InterfaceB","link":"/test/out/default/module-2/interfaces/InterfaceB.md"}]}]}]"`;

exports[`VitePress should generate typedoc sidebar with duplicate out and docsRoots 1`] = `"[{"text":"module-1","link":"/module-1/","collapsed":true,"items":[{"text":"Classes","collapsed":true,"items":[{"text":"ClassA","link":"/module-1/classes/ClassA.md"},{"text":"ClassB","link":"/module-1/classes/ClassB.md"}]},{"text":"Interfaces","collapsed":true,"items":[{"text":"InterfaceA","link":"/module-1/interfaces/InterfaceA.md"},{"text":"InterfaceB","link":"/module-1/interfaces/InterfaceB.md"}]}]},{"text":"module-2","link":"/module-2/","collapsed":true,"items":[{"text":"Classes","collapsed":true,"items":[{"text":"ClassA","link":"/module-2/classes/ClassA.md"},{"text":"ClassB","link":"/module-2/classes/ClassB.md"}]},{"text":"Interfaces","collapsed":true,"items":[{"text":"InterfaceA","link":"/module-2/interfaces/InterfaceA.md"},{"text":"InterfaceB","link":"/module-2/interfaces/InterfaceB.md"}]}]}]"`;

exports[`VitePress should generate typedoc sidebar with options 1`] = `
"[
{
Expand Down
9 changes: 9 additions & 0 deletions packages/typedoc-vitepress-theme/test/specs/vitepress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ describe(`VitePress`, () => {
.toString();
expect(contents).toMatchSnapshot();
});

test(`should generate typedoc sidebar with duplicate out and docsRoots`, async () => {
const contents = fs
.readFileSync(
path.join(__dirname, '../out/sidebar-options-2/typedoc-sidebar.json'),
)
.toString();
expect(contents).toMatchSnapshot();
});
});
1 change: 0 additions & 1 deletion packages/typedoc-vitepress-theme/test/typedoc.base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"entryPoints": ["./stubs/*.ts"],
"tsconfig": "./stubs/tsconfig.json",
"out": "./out",
"plugin": ["typedoc-plugin-markdown", "typedoc-vitepress-theme"],
"readme": "none",
"disableSources": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./typedoc.base.json",
"docsRoot": "./out/sidebar-options-2"
}

0 comments on commit bf5c175

Please sign in to comment.