@@ -2,7 +2,7 @@ import * as fs from 'fs'
22import path from 'path'
33import { fileURLToPath } from 'url'
44import type { MenuItemsStructure } from '../src/types'
5- import { parseRawContent } from '../src/utils'
5+ import { isDefined , parseRawContent } from '../src/utils'
66
77const __filename = fileURLToPath ( import . meta. url )
88const __dirname = path . dirname ( __filename )
@@ -28,67 +28,71 @@ interface ParsedDoc {
2828 links : ExtractedLink [ ]
2929}
3030
31- const isRecord = ( value : unknown ) : value is Record < string , unknown > =>
32- typeof value === 'object' && value !== null
31+ interface MarkdownNode {
32+ type ?: string
33+ id ?: string
34+ href ?: string
35+ text ?: string
36+ title ?: string
37+ linkUrl ?: string | null
38+ imageUrl ?: string
39+ tokens ?: MarkdownNode [ ]
40+ items ?: MarkdownNode [ ]
41+ header ?: MarkdownNode [ ]
42+ rows ?: MarkdownNode [ ] [ ]
43+ }
3344
3445const collectSectionsAndLinks = (
35- nodes : unknown [ ] ,
46+ nodes : MarkdownNode [ ] ,
3647 sectionIds : Set < string > ,
3748 links : ExtractedLink [ ] ,
3849) => {
3950 for ( const node of nodes ) {
40- if ( ! isRecord ( node ) ) {
41- continue
42- }
43-
44- const type = typeof node . type === 'string' ? node . type : ''
51+ const type = node . type ?? ''
4552
4653 if ( type === 'code' || type === 'codespan' ) {
4754 continue
4855 }
4956
50- if ( type === 'section' && typeof node . id === 'string' && node . id ) {
57+ if ( type === 'section' && node . id ) {
5158 sectionIds . add ( node . id )
5259 }
5360
54- if ( type === 'link' && typeof node . href === 'string' ) {
61+ if ( type === 'link' && isDefined ( node . href ) ) {
5562 links . push ( {
5663 href : node . href ,
57- text : typeof node . text === 'string' ? node . text : '' ,
64+ text : node . text ?? '' ,
5865 kind : 'link' ,
5966 } )
6067 }
6168
62- if (
63- ( type === 'image' || type === 'image-static' ) &&
64- typeof node . href === 'string'
65- ) {
69+ if ( ( type === 'image' || type === 'image-static' ) && isDefined ( node . href ) ) {
6670 links . push ( {
6771 href : node . href ,
68- text : typeof node . text === 'string' ? node . text : '' ,
72+ text : node . text ?? '' ,
6973 kind : 'image' ,
7074 } )
71- if ( typeof node . linkUrl === 'string' && node . linkUrl ) {
75+ if ( node . linkUrl ) {
7276 links . push ( {
7377 href : node . linkUrl ,
74- text : typeof node . text === 'string' ? node . text : '' ,
78+ text : node . text ?? '' ,
7579 kind : 'link' ,
7680 } )
7781 }
7882 }
7983
8084 if ( type === 'project-example' ) {
81- if ( typeof node . imageUrl === 'string' && node . imageUrl ) {
85+ if ( node . imageUrl ) {
8286 links . push ( {
8387 href : node . imageUrl ,
84- text : typeof node . title === 'string' ? node . title : '' ,
88+ text : node . title ?? '' ,
8589 kind : 'image' ,
8690 } )
8791 }
88- if ( typeof node . linkUrl === 'string' && node . linkUrl ) {
92+ if ( node . linkUrl ) {
8993 links . push ( {
9094 href : node . linkUrl ,
91- text : typeof node . title === 'string' ? node . title : '' ,
95+ text : node . title ?? '' ,
9296 kind : 'link' ,
9397 } )
9498 }
@@ -139,14 +143,13 @@ export const getRouteMap = (structure: MenuItemsStructure) => {
139143 const routeMap = new Map < string , string > ( )
140144
141145 for ( const item of structure ) {
142- if ( item . children && item . children . length > 0 ) {
143- for ( const child of item . children ) {
144- const route = `/${ item . path } /${ child . path } `
145- routeMap . set ( route , `${ item . path } /${ child . path } /index.md` )
146- }
147- } else {
148- const route = `/${ item . path } `
149- routeMap . set ( route , `${ item . path } /index.md` )
146+ const routes =
147+ item . children && item . children . length > 0
148+ ? item . children . map ( ( child ) => `/${ item . path } /${ child . path } ` )
149+ : [ `/${ item . path } ` ]
150+
151+ for ( const route of routes ) {
152+ routeMap . set ( route , `${ route . slice ( 1 ) } /index.md` )
150153 }
151154 }
152155
@@ -329,7 +332,7 @@ export const checkDocsLinks = ({
329332 continue
330333 }
331334
332- if ( fragment !== undefined ) {
335+ if ( isDefined ( fragment ) ) {
333336 if ( ! fragment || fragment . startsWith ( '#' ) ) {
334337 issues . push ( {
335338 file : doc . file ,
0 commit comments