@@ -39,7 +39,7 @@ const SLUG_TO_URL = {
3939} ;
4040
4141// Function to convert the TypeScript sidebar config to JSON
42- async function convertSidebarConfigToJson ( fileName ) {
42+ async function convertSidebarConfigToJson ( fileName : string ) {
4343 const inputFileContent = fs . readFileSync (
4444 path . join ( import . meta. dirname , '../website' , fileName ) ,
4545 'utf8'
@@ -70,8 +70,8 @@ async function convertSidebarConfigToJson(fileName) {
7070}
7171
7272// Function to extract URLs from sidebar config
73- function extractUrlsFromSidebar ( sidebarConfig , prefix ) {
74- const urls = [ ] ;
73+ function extractUrlsFromSidebar ( sidebarConfig : SidebarConfig , prefix : string ) {
74+ const urls : string [ ] = [ ] ;
7575
7676 // Process each section (docs, api, components)
7777 Object . entries ( sidebarConfig ) . forEach ( ( [ , categories ] ) => {
@@ -93,15 +93,19 @@ function extractUrlsFromSidebar(sidebarConfig, prefix) {
9393 return urls ;
9494}
9595
96+ type SidebarItem = string | { type : string ; id ?: string ; items ?: SidebarItem [ ] ; label ?: string } ;
97+ type Items = SidebarItem [ ] | { items : SidebarItem [ ] } ;
98+
99+ interface SidebarConfig {
100+ [ section : string ] : {
101+ [ category : string ] : Items ;
102+ } ;
103+ }
96104// Recursive function to process items and extract URLs
97- function processItemsForUrls ( items , urls , prefix ) {
98- if ( typeof items === 'object' && Array . isArray ( items . items ) ) {
99- processItemsForUrls ( items . items , urls , prefix ) ;
100- return ;
101- }
105+ function processItemsForUrls ( items : Items , urls : string [ ] , prefix : string ) {
106+ const itemsArray = getItemsArray ( items ) ;
102107
103- if ( Array . isArray ( items ) ) {
104- items . forEach ( item => {
108+ itemsArray . forEach ( item => {
105109 if ( typeof item === 'string' ) {
106110 urls . push ( `${ URL_PREFIX } ${ prefix } /${ item } ` ) ;
107111 } else if ( typeof item === 'object' ) {
@@ -112,11 +116,16 @@ function processItemsForUrls(items, urls, prefix) {
112116 }
113117 }
114118 } ) ;
115- }
119+ }
120+
121+ interface UrlCheckResult {
122+ url : string ;
123+ status : number | string ;
124+ error ?: string ;
116125}
117126
118127// Function to check URL status
119- function checkUrl ( urlString ) {
128+ function checkUrl ( urlString : string ) : Promise < UrlCheckResult > {
120129 return new Promise ( resolve => {
121130 const parsedUrl = url . parse ( urlString ) ;
122131
@@ -130,7 +139,7 @@ function checkUrl(urlString) {
130139 const req = https . request ( options , res => {
131140 resolve ( {
132141 url : urlString ,
133- status : res . statusCode ,
142+ status : res . statusCode || 0 ,
134143 } ) ;
135144 } ) ;
136145
@@ -155,8 +164,8 @@ function checkUrl(urlString) {
155164}
156165
157166// Process each URL
158- async function processUrls ( urls ) {
159- const unavailableUrls = [ ] ;
167+ async function processUrls ( urls : string [ ] ) {
168+ const unavailableUrls : UnavailableUrl [ ] = [ ] ;
160169
161170 for ( const urlToCheck of urls ) {
162171 const result = await checkUrl ( urlToCheck ) ;
@@ -176,7 +185,7 @@ async function processUrls(urls) {
176185}
177186
178187// Function to extract title from markdown frontmatter
179- function extractMetadataFromMarkdown ( filePath ) {
188+ function extractMetadataFromMarkdown ( filePath : string ) {
180189 try {
181190 const content = fs . readFileSync ( filePath , 'utf8' ) ;
182191 const frontmatterMatch = content . match ( / - - - \n ( [ \s \S ] * ?) \n - - - / ) ;
@@ -188,7 +197,7 @@ function extractMetadataFromMarkdown(filePath) {
188197 return {
189198 title : titleMatch
190199 ? titleMatch [ 1 ] . trim ( )
191- : filePath . split ( '/' ) . pop ( ) . replace ( '.md' , '' ) ,
200+ : filePath . split ( '/' ) . pop ( ) ? .replace ( '.md' , '' ) ,
192201 slug : slugMatch ? slugMatch [ 1 ] . trim ( ) . replace ( / ^ \/ / , '' ) : null ,
193202 } ;
194203 }
@@ -197,14 +206,18 @@ function extractMetadataFromMarkdown(filePath) {
197206 }
198207 // If no frontmatter found, on an error occurred use the filename
199208 return {
200- title : filePath . split ( '/' ) . pop ( ) . replace ( '.md' , '' ) ,
209+ title : filePath . split ( '/' ) . pop ( ) ? .replace ( '.md' , '' ) ,
201210 slug : null ,
202211 } ;
203212}
204213
205214// Function to map special cases for file names that don't match the sidebar
206- function mapDocPath ( item , prefix ) {
207- const specialCases = {
215+ interface SpecialCases {
216+ [ key : string ] : string ;
217+ }
218+
219+ function mapDocPath ( item : string | SidebarItem , prefix : string ) : string {
220+ const specialCases : SpecialCases = {
208221 'environment-setup' : 'getting-started.md' ,
209222 'native-platform' : 'native-platforms.md' ,
210223 'turbo-native-modules-introduction' : 'turbo-native-modules.md' ,
@@ -223,8 +236,19 @@ function mapDocPath(item, prefix) {
223236 return `${ item } .md` ;
224237}
225238
239+ type UnavailableUrl = { url : string ; status : number | string ; error : string | null ; } ;
240+
241+ // Helper function to extract items array from Items type
242+ function getItemsArray ( items : Items ) : SidebarItem [ ] {
243+ if ( Array . isArray ( items ) ) {
244+ return items ;
245+ } else {
246+ return items . items ;
247+ }
248+ }
249+
226250// Function to generate output for each sidebar
227- function generateMarkdown ( sidebarConfig , docPath , prefix , unavailableUrls ) {
251+ function generateMarkdown ( sidebarConfig : SidebarConfig , docPath : string , prefix : string , unavailableUrls : UnavailableUrl [ ] ) {
228252 let markdown = '' ;
229253
230254 // Process each section (docs, api, components)
@@ -235,12 +259,10 @@ function generateMarkdown(sidebarConfig, docPath, prefix, unavailableUrls) {
235259 Object . entries ( categories ) . forEach ( ( [ categoryName , items ] ) => {
236260 markdown += `### ${ categoryName === '0' ? 'General' : categoryName } \n\n` ;
237261
238- if ( typeof items === 'object' && Array . isArray ( items . items ) ) {
239- items = items . items ;
240- }
241- const reorderedArray = items . every ( item => typeof item === 'string' )
242- ? items
243- : [ ...items ] . sort ( ( a , b ) =>
262+ const itemsArray = getItemsArray ( items ) ;
263+ const reorderedArray = itemsArray . every ( item => typeof item === 'string' )
264+ ? itemsArray
265+ : [ ...itemsArray ] . sort ( ( a , b ) =>
244266 typeof a === 'string' && typeof b !== 'string'
245267 ? - 1
246268 : typeof a !== 'string' && typeof b === 'string'
@@ -268,7 +290,7 @@ function generateMarkdown(sidebarConfig, docPath, prefix, unavailableUrls) {
268290 } else if ( item . type === 'category' && Array . isArray ( item . items ) ) {
269291 // This is a category with nested items
270292 markdown += `#### ${ item . label } \n\n` ;
271- item . items . forEach ( nestedItem => {
293+ item . items . forEach ( ( nestedItem : SidebarItem ) => {
272294 if ( typeof nestedItem === 'string' ) {
273295 const fullDocPath = `${ docPath } ${ mapDocPath ( nestedItem , prefix ) } ` ;
274296 if ( ! isEntryUnavailable ( unavailableUrls , fullDocPath ) ) {
@@ -296,7 +318,7 @@ function generateMarkdown(sidebarConfig, docPath, prefix, unavailableUrls) {
296318}
297319
298320async function generateOutput ( ) {
299- const results = [ ] ;
321+ const results : { markdown : string ; prefix : string ; } [ ] = [ ] ;
300322 const promises = [ ] ;
301323
302324 let output = `# ${ TITLE } \n\n` ;
@@ -365,7 +387,7 @@ async function generateOutput() {
365387 } ) ;
366388}
367389
368- function isEntryUnavailable ( unavailableUrls , docPath ) {
390+ function isEntryUnavailable ( unavailableUrls : UnavailableUrl [ ] , docPath : string ) {
369391 return ! ! unavailableUrls . find ( entry =>
370392 entry . url . endsWith ( docPath . substring ( 1 ) )
371393 ) ;
0 commit comments