11import type { SidebarsConfig } from '@docusaurus/plugin-content-docs' ;
2- import type { SidebarItem , SidebarItemCategory , SidebarItemConfig , SidebarItemDoc } from '@docusaurus/plugin-content-docs/lib/sidebars/types.d.ts' ;
2+ import type {
3+ SidebarItem ,
4+ SidebarItemCategoryConfig ,
5+ SidebarItemConfig ,
6+ SidebarItemDoc ,
7+ } from '@docusaurus/plugin-content-docs/lib/sidebars/types.d.ts' ;
38import rawMillicastApiSidebar from './millicast/api/sidebar' ;
49import rawMillicastAdvancedReportingApiSidebar from './millicast/api/reporting/sidebar' ;
510import rawMillicastDirectorApiSidebar from './millicast/api/director/sidebar' ;
611
7- function isCategory ( item : SidebarItemConfig ) : item is SidebarItemCategory {
12+ function isCategory ( item : SidebarItemConfig ) : item is SidebarItemCategoryConfig {
813 return ( item as SidebarItem ) . type === 'category' ;
914}
1015
@@ -17,7 +22,7 @@ function removeHiddenItems(data: SidebarItemConfig[], hiddenIds: string[]): Side
1722 return data
1823 . map ( ( category ) => {
1924 // filter out items from other categories that match the hidden IDs
20- if ( isCategory ( category ) ) {
25+ if ( isCategory ( category ) && Array . isArray ( category . items ) ) {
2126 // filter out the items that match any of the hidden IDs
2227 const filteredItems = category . items . filter ( ( item ) => ! ( isDoc ( item ) && hiddenIdsSet . has ( item . id ) ) ) ;
2328
@@ -40,55 +45,59 @@ function removeHiddenItems(data: SidebarItemConfig[], hiddenIds: string[]): Side
4045
4146function fixLabels ( items : SidebarItemConfig [ ] , replacements : Record < string , string > = { } ) : SidebarItemConfig [ ] {
4247 return items . map ( ( item ) => {
43- if ( ! ( isCategory ( item ) || isDoc ( item ) ) ) {
44- return item ;
45- }
46- let label = item . label ;
47- if ( label ) {
48+ if ( ( isCategory ( item ) || isDoc ( item ) ) && item . label ) {
49+ let label = item . label ;
4850 if ( replacements [ label ] ) {
4951 // Replace label
5052 label = replacements [ label ] ;
53+ item = { ...item , label } ;
5154 } else if ( isCategory ( item ) ) {
5255 // Add spaces between capitalized words
5356 label = item . label . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1 $2' ) ;
57+ item = { ...item , label } ;
5458 }
5559 }
56- if ( isCategory ( item ) ) {
57- return { ...item , label, items : fixLabels ( item . items , replacements ) } ;
58- } else {
59- return { ...item , label } ;
60+ if ( isCategory ( item ) && Array . isArray ( item . items ) ) {
61+ const items = fixLabels ( item . items , replacements ) ;
62+ item = { ...item , items } ;
6063 }
64+ return item ;
6165 } ) ;
6266}
6367
6468function mergeCategories ( items : SidebarItemConfig [ ] ) : SidebarItemConfig [ ] {
6569 for ( let index = 0 ; index < items . length ; index ++ ) {
6670 const item = items [ index ] ;
67- if ( isCategory ( item ) ) {
68- // Merge categories with same label
69- const otherIndex = items . findIndex ( ( other , otherIndex ) => {
70- return otherIndex > index && isCategory ( other ) && other . label === item . label ;
71+ if ( ! isCategory ( item ) || ! Array . isArray ( item . items ) ) {
72+ continue ;
73+ }
74+ // Merge categories with same label
75+ const otherIndex = items . findIndex ( ( other , otherIndex ) => {
76+ return otherIndex > index && isCategory ( other ) && other . label === item . label ;
77+ } ) ;
78+ if ( otherIndex < 0 ) {
79+ continue ;
80+ }
81+ const otherItem = items [ otherIndex ] as SidebarItemCategoryConfig ;
82+ if ( ! Array . isArray ( otherItem . items ) ) {
83+ continue ;
84+ }
85+ const mergedItems = [ ...otherItem . items ] ;
86+ for ( const otherChild of item . items ) {
87+ // Insert before existing child with same label (if any).
88+ // Otherwise, insert at the end.
89+ const existingChildIndex = mergedItems . findIndex ( ( existingChild ) => {
90+ return isDoc ( otherChild ) && isDoc ( existingChild ) && existingChild . label === otherChild . label ;
7191 } ) ;
72- if ( otherIndex > 0 ) {
73- const otherItem = items [ otherIndex ] as SidebarItemCategory ;
74- const mergedItems = [ ...otherItem . items ] ;
75- for ( const otherChild of item . items ) {
76- // Insert before existing child with same label (if any).
77- // Otherwise, insert at the end.
78- const existingChildIndex = mergedItems . findIndex ( ( existingChild ) => {
79- return isDoc ( otherChild ) && isDoc ( existingChild ) && existingChild . label === otherChild . label ;
80- } ) ;
81- if ( existingChildIndex >= 0 ) {
82- mergedItems . splice ( existingChildIndex , 0 , otherChild ) ;
83- } else {
84- mergedItems . push ( otherChild ) ;
85- }
86- }
87- items [ index ] = { ...item , items : mergedItems } ;
88- items . splice ( otherIndex , 1 ) ;
89- index -- ;
92+ if ( existingChildIndex >= 0 ) {
93+ mergedItems . splice ( existingChildIndex , 0 , otherChild ) ;
94+ } else {
95+ mergedItems . push ( otherChild ) ;
9096 }
9197 }
98+ items [ index ] = { ...item , items : mergedItems } ;
99+ items . splice ( otherIndex , 1 ) ;
100+ index -- ;
92101 }
93102 return items ;
94103}
0 commit comments