Skip to content

Commit

Permalink
fix(docusaurus): correctly handle sidebar ids with custom docs paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Jul 9, 2024
1 parent 337e060 commit 513f569
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-waves-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'docusaurus-plugin-typedoc': patch
---

- Correctly handle sidebar ids with custom docs paths (#648)
10 changes: 9 additions & 1 deletion packages/docusaurus-plugin-typedoc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PluginOptions } from './models';
import { getPluginOptions } from './options';
import * as options from './options/declarations';
import { getSidebar } from './sidebar';
import { adjustBaseDirectory } from './utils/adjust-basedir';

// store list of plugin ids when running multiple instances
const apps: string[] = [];
Expand Down Expand Up @@ -75,12 +76,19 @@ async function generateTypedoc(context: any, opts: Partial<PluginOptions>) {
app.renderer.postRenderAsyncJobs.push(async (output) => {
if (output.navigation) {
const sidebarPath = path.resolve(outputDir, 'typedoc-sidebar.cjs');
const baseDir = path

let baseDir = path
.relative(siteDir, outputDir)
.split(path.sep)
.slice(1)
.join('/');

const docsPresetPath = docsPreset ? docsPreset[1]?.docs?.path : null;

if (Boolean(docsPresetPath)) {
baseDir = adjustBaseDirectory(baseDir, docsPresetPath);
}

const sidebarJson = getSidebar(
output.navigation,
baseDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { adjustBaseDirectory } from './adjust-basedir';

describe('adjustPath', () => {
it('should handle paths', () => {
const originalPath = 'folder/api-reference';
const subPath = 'folder';
const expected = 'api-reference';
expect(adjustBaseDirectory(originalPath, subPath)).toBe(expected);
});

it('should handle relative paths', () => {
const originalPath = 'docs/api-reference';
const subPath = '../docs';
const expected = 'api-reference';
expect(adjustBaseDirectory(originalPath, subPath)).toBe(expected);
});
});
36 changes: 36 additions & 0 deletions packages/docusaurus-plugin-typedoc/src/utils/adjust-basedir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as path from 'path';

/**
* This method is designed to resolve the base directory paths for documentation presets.
*/
export function adjustBaseDirectory(originalPath: string, subPath: string) {
// Normalize the paths to handle different path formats and OS differences
originalPath = path.normalize(originalPath);
subPath = path.normalize(subPath);

// Split the original path into an array of segments
const segments = originalPath.split(path.sep);

// Split the sub path into an array of segments and filter out ".." to handle relative paths
const subSegments = subPath
.split(path.sep)
.filter((segment) => segment !== '..');

// Find the index of the first sub path segment in the original path segments
const startIndex = segments.indexOf(subSegments[0]);

// Remove the sub path segments from the original path segments if found
if (startIndex !== -1) {
segments.splice(startIndex, subSegments.length);
}

// Join the segments back into a path and remove the leading slash if present
let newPath = segments.join(path.sep);

// Ensure there is no leading slash
if (newPath.startsWith(path.sep)) {
newPath = newPath.slice(1);
}

return newPath;
}

0 comments on commit 513f569

Please sign in to comment.