@@ -16,6 +16,7 @@ import fs from "fs-extra";
1616import cloneDeep from "lodash/cloneDeep" ;
1717import kebabCase from "lodash/kebabCase" ;
1818import unionBy from "lodash/unionBy" ;
19+ import uniq from "lodash/uniq" ;
1920
2021import { isURL } from "../index" ;
2122import {
@@ -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
0 commit comments