@@ -16,6 +16,7 @@ import fs from "fs-extra";
16
16
import cloneDeep from "lodash/cloneDeep" ;
17
17
import kebabCase from "lodash/kebabCase" ;
18
18
import unionBy from "lodash/unionBy" ;
19
+ import uniq from "lodash/uniq" ;
19
20
20
21
import { isURL } from "../index" ;
21
22
import {
@@ -85,41 +86,6 @@ function createItems(
85
86
let items : PartialPage < ApiMetadata > [ ] = [ ] ;
86
87
const infoId = kebabCase ( openapiData . info . title ) ;
87
88
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
-
123
89
if ( openapiData . info . description ) {
124
90
// Only create an info page if we have a description.
125
91
const infoDescription = openapiData . info ?. description ;
@@ -270,6 +236,52 @@ function createItems(
270
236
}
271
237
}
272
238
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
+
273
285
return items as ApiMetadata [ ] ;
274
286
}
275
287
0 commit comments