Skip to content

Commit ccbbbe5

Browse files
authored
Include global tags and tag docs only if referenced by operation (#340)
1 parent f4a166e commit ccbbbe5

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import fs from "fs-extra";
1616
import cloneDeep from "lodash/cloneDeep";
1717
import kebabCase from "lodash/kebabCase";
1818
import unionBy from "lodash/unionBy";
19+
import uniq from "lodash/uniq";
1920

2021
import { isURL } from "../index";
2122
import {
@@ -85,41 +86,6 @@ function createItems(
8586
let items: PartialPage<ApiMetadata>[] = [];
8687
const infoId = kebabCase(openapiData.info.title);
8788

88-
if (sidebarOptions?.categoryLinkSource === "tag") {
89-
// Only create an tag pages if categoryLinkSource set to tag.
90-
const tags: TagObject[] = openapiData.tags ?? [];
91-
// eslint-disable-next-line array-callback-return
92-
tags
93-
.filter((tag) => !tag.description?.includes("SchemaDefinition"))
94-
// eslint-disable-next-line array-callback-return
95-
.map((tag) => {
96-
const description = getTagDisplayName(
97-
tag.name!,
98-
openapiData.tags ?? []
99-
);
100-
const tagId = kebabCase(tag.name);
101-
const splitDescription = description.match(/[^\r\n]+/g);
102-
const tagPage: PartialPage<TagPageMetadata> = {
103-
type: "tag",
104-
id: tagId,
105-
unversionedId: tagId,
106-
title: description ?? "",
107-
description: description ?? "",
108-
frontMatter: {
109-
description: splitDescription
110-
? splitDescription[0]
111-
.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
112-
.replace(/\s+$/, "")
113-
: "",
114-
},
115-
tag: {
116-
...tag,
117-
},
118-
};
119-
items.push(tagPage);
120-
});
121-
}
122-
12389
if (openapiData.info.description) {
12490
// Only create an info page if we have a description.
12591
const infoDescription = openapiData.info?.description;
@@ -270,6 +236,52 @@ function createItems(
270236
}
271237
}
272238

239+
if (sidebarOptions?.categoryLinkSource === "tag") {
240+
// Get global tags
241+
const tags: TagObject[] = openapiData.tags ?? [];
242+
243+
// Get operation tags
244+
const apiItems = items.filter((item) => {
245+
return item.type === "api";
246+
}) as ApiPageMetadata[];
247+
const operationTags = uniq(
248+
apiItems
249+
.flatMap((item) => item.api.tags)
250+
.filter((item): item is string => !!item)
251+
);
252+
253+
// eslint-disable-next-line array-callback-return
254+
tags
255+
.filter((tag) => operationTags.includes(tag.name!)) // include only tags referenced by operation
256+
// eslint-disable-next-line array-callback-return
257+
.map((tag) => {
258+
const description = getTagDisplayName(
259+
tag.name!,
260+
openapiData.tags ?? []
261+
);
262+
const tagId = kebabCase(tag.name);
263+
const splitDescription = description.match(/[^\r\n]+/g);
264+
const tagPage: PartialPage<TagPageMetadata> = {
265+
type: "tag",
266+
id: tagId,
267+
unversionedId: tagId,
268+
title: description ?? "",
269+
description: description ?? "",
270+
frontMatter: {
271+
description: splitDescription
272+
? splitDescription[0]
273+
.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
274+
.replace(/\s+$/, "")
275+
: "",
276+
},
277+
tag: {
278+
...tag,
279+
},
280+
};
281+
items.push(tagPage);
282+
});
283+
}
284+
273285
return items as ApiMetadata[];
274286
}
275287

packages/docusaurus-plugin-openapi-docs/src/sidebars/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ function groupByTags(
6868
);
6969

7070
// Combine globally defined tags with operation tags
71+
// Only include global tag if referenced in operation tags
7172
let apiTags: string[] = [];
7273
tags.flat().forEach((tag) => {
73-
apiTags.push(tag.name!);
74+
// Should we also check x-displayName?
75+
if (operationTags.includes(tag.name!)) {
76+
apiTags.push(tag.name!);
77+
}
7478
});
7579
apiTags = uniq(apiTags.concat(operationTags));
7680

0 commit comments

Comments
 (0)