Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 19 additions & 11 deletions packages/docusaurus-plugin-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,25 @@ custom_edit_url: null
let infoBasePath = `${outputDir}/${item.infoId}`;
if (docRouteBasePath) {
// Safely extract path segment, handling cases where docPath may not be in outputDir
const outputSegment =
docPath && outputDir.includes(docPath)
? (outputDir.split(docPath)[1]?.replace(/^\/+/g, "") ?? "")
: outputDir
.slice(outputDir.indexOf("/", 1))
.replace(/^\/+/g, "");
infoBasePath =
`${docRouteBasePath}/${outputSegment}/${item.infoId}`.replace(
/^\/+/g,
""
);
const normalize = (p: string): string =>
p.replace(/^\.\//, "").replace(/^\/+|\/+$/g, "");
const outputSegments = normalize(outputDir).split("/").filter(Boolean);
let outputSegment = outputSegments.slice(1).join("/"); // fallback
if (docPath) {
const docSegments = normalize(docPath).split("/").filter(Boolean);
for (let i = 0; i <= outputSegments.length - docSegments.length; i++) {
const match = docSegments.every(
(seg, j) => outputSegments[i + j] === seg
);
if (match) {
outputSegment = outputSegments.slice(i + docSegments.length).join("/");
break;
}
}
}
infoBasePath = `${docRouteBasePath}/${outputSegment}/${item.infoId}`
.replace(/^\/+/g, "")
.replace(/\/+/g, "/");
}
if (item.infoId) item.infoPath = infoBasePath;
}
Expand Down
28 changes: 22 additions & 6 deletions packages/docusaurus-plugin-openapi-docs/src/sidebars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,29 @@ function groupByTags(
output: string,
doc: string | undefined
): string => {
if (doc && output.includes(doc)) {
return output.split(doc)[1]?.replace(/^\/+/g, "") ?? "";
// Normalize path: remove leading ./ and leading/trailing slashes
const normalize = (p: string): string =>
p.replace(/^\.\//, "").replace(/^\/+|\/+$/g, "");

const outputSegments = normalize(output).split("/").filter(Boolean);

if (doc) {
const docSegments = normalize(doc).split("/").filter(Boolean);

// Find where docSegments sequence appears in outputSegments
for (let i = 0; i <= outputSegments.length - docSegments.length; i++) {
const match = docSegments.every(
(seg, j) => outputSegments[i + j] === seg
);
if (match) {
// Return everything after the matched sequence
return outputSegments.slice(i + docSegments.length).join("/");
}
}
}
const slashIndex = output.indexOf("/", 1);
return slashIndex === -1
? ""
: output.slice(slashIndex).replace(/^\/+/g, "");

// Fallback: return everything after first segment
return outputSegments.slice(1).join("/");
};

const basePath = getBasePathFromOutput(outputDir, docPath);
Expand Down