1
+ import { toTitlecase } from './misc.js' ;
1
2
import { urlOption } from './url.js' ;
2
3
3
4
export function toMdFilename ( name : string ) {
4
5
return name
5
6
. split ( '-' )
6
- . map ( ( part ) => ` ${ part . at ( 0 ) ?. toUpperCase ( ) } ${ part . slice ( 1 ) . toLowerCase ( ) } ` )
7
+ . map ( ( part ) => toTitlecase ( part ) )
7
8
. join ( '_' ) ;
8
9
}
9
10
@@ -43,14 +44,13 @@ function parseHeadline(text: string): Heading | null {
43
44
const label = groups . label ?? groups . onlylabel ;
44
45
45
46
return {
46
- docs_anchor : `#${ label . replaceAll ( ' ' , '-' ) . toLowerCase ( ) } ` ,
47
+ docs_anchor : `#${ label . replaceAll ( ' ' , '-' ) . replaceAll ( ':' , '' ) . toLowerCase ( ) } ` ,
47
48
label,
48
49
verb : groups . verb ,
49
50
route : groups . route ,
50
51
} ;
51
52
}
52
53
53
- // https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/user/User.md
54
54
// https://raw.githubusercontent.com/discord/discord-api-docs/main/docs/resources/User.md
55
55
56
56
type ParsedSection = {
@@ -107,10 +107,26 @@ export function parseSections(content: string): ParsedSection[] {
107
107
return res ;
108
108
}
109
109
110
+ function compressAnchor ( anchor : string ) {
111
+ return anchor . replaceAll ( '-' , '' ) ;
112
+ }
113
+
114
+ function anchorsCompressedEqual ( one ?: string , other ?: string ) {
115
+ if ( ! one || ! other ) {
116
+ return false ;
117
+ }
118
+
119
+ const one_ = compressAnchor ( one ) ;
120
+ const other_ = compressAnchor ( other ) ;
121
+
122
+ return one_ === other_ ;
123
+ }
124
+
110
125
export function findRelevantDocsSection ( query : string , docsMd : string ) {
111
126
const sections = parseSections ( docsMd ) ;
112
127
for ( const section of sections ) {
113
- if ( section . heading ?. docs_anchor . startsWith ( query ) ) {
128
+ const anchor = section . heading ?. docs_anchor ;
129
+ if ( anchor ?. startsWith ( query ) || anchorsCompressedEqual ( anchor , query ) ) {
114
130
return section ;
115
131
}
116
132
}
@@ -122,7 +138,14 @@ export async function fetchDocsBody(link: string) {
122
138
return null ;
123
139
}
124
140
125
- const docsMd = await fetch ( githubResource . githubUrl ) . then ( async ( res ) => res . text ( ) ) ;
141
+ const docsMd = await fetch ( githubResource . githubUrl ) . then ( async ( res ) => {
142
+ if ( res . status === 404 ) {
143
+ // some docs pages use the .mdx format
144
+ return fetch ( `${ githubResource . githubUrl } x` ) . then ( async ( innerRes ) => innerRes . text ( ) ) ;
145
+ }
146
+
147
+ return res . text ( ) ;
148
+ } ) ;
126
149
const section = findRelevantDocsSection ( githubResource . docsAnchor , docsMd ) ;
127
150
128
151
if ( section ) {
0 commit comments