11import { ScrapeCallback , Scraper } from './scraper.js' ;
22import { deserializeXml } from '../utils/xml.js' ;
3- import { AnyNode , CheerioAPI } from 'cheerio' ;
3+ import { AnyNode , Cheerio , CheerioAPI } from 'cheerio' ;
44import { Element as DOMElement } from 'domhandler' ;
55
66export type WikiFunctionType = 'panelfunc' | 'classfunc' | 'libraryfunc' | 'hook' ;
@@ -140,7 +140,31 @@ export function isClass(page: WikiPage): page is TypePage {
140140 return page . type === 'class' ;
141141}
142142
143- // Handle <callback> in a description of an argument/return
143+ // Handle things like <page> tags, <warning>, etc.
144+ function markdownifyDescription ( $ : CheerioAPI , e : Cheerio < AnyNode > ) : string {
145+ // <page> => markdown []()
146+ for ( const page of $ ( e ) . find ( 'page' ) ) {
147+ const $p = $ ( page ) ;
148+ const url = $p . text ( ) ;
149+ const text = $p . attr ( "text" ) || url ;
150+ $p . replaceWith ( `[${ text } ](${ url } )` ) ;
151+ }
152+
153+ let description = $ ( e ) . text ( ) ;
154+
155+ // Fixup []() that use local URLs. This is not exclusive to the result of <page> conversions
156+ description = description . replace ( / \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) " ] + ) (?: \" ( [ ^ \" ] + ) \" ) ? \) / g, function ( match , text , url ) {
157+ if ( url . indexOf ( "://" ) == - 1 ) {
158+ return `[${ text } ](https://wiki.facepunch.com/gmod/${ url } )` ;
159+ }
160+
161+ return match ;
162+ } ) ;
163+
164+ return description ;
165+ } ;
166+
167+ // Extract <callback> in a description of an argument/return
144168function handleCallbackInDescription ( $ : CheerioAPI , e : AnyNode ) : [ string ?, FunctionCallback ?] {
145169 let description : string = "" ;
146170 let callback : FunctionCallback = undefined ! ;
@@ -181,7 +205,7 @@ function handleCallbackInDescription($: CheerioAPI, e: AnyNode): [string?, Funct
181205 }
182206 }
183207 } else {
184- description += $ ( node ) . text ( ) ;
208+ description += markdownifyDescription ( $ , $ ( node ) ) ;
185209 }
186210 }
187211
@@ -231,7 +255,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
231255 return < EnumValue > {
232256 key : $el . attr ( 'key' ) ! ,
233257 value : $el . attr ( 'value' ) ! ,
234- description : $el . text ( ) ,
258+ description : markdownifyDescription ( $ , $el ) ,
235259 deprecated : deprecated || undefined ,
236260 } ;
237261 } ) . get ( ) ;
@@ -240,7 +264,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
240264 type : 'enum' ,
241265 name : address ,
242266 address : address ,
243- description : $ ( 'description' ) . text ( ) ,
267+ description : markdownifyDescription ( $ , $ ( 'description' ) ) ,
244268 realm : $ ( 'realm' ) . text ( ) as Realm ,
245269 items
246270 } ;
@@ -270,7 +294,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
270294 type : 'struct' ,
271295 name : address ,
272296 address : address ,
273- description : $ ( 'structure description' ) . text ( ) ,
297+ description : markdownifyDescription ( $ , $ ( 'structure description' ) ) ,
274298 realm : $ ( 'realm' ) . text ( ) as Realm ,
275299 fields,
276300 deprecated
@@ -280,7 +304,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
280304 type : 'panel' ,
281305 name : address ,
282306 address : address ,
283- description : $ ( 'panel description' ) . text ( ) ,
307+ description : markdownifyDescription ( $ , $ ( 'panel description' ) ) ,
284308 realm : $ ( 'realm' ) . text ( ) as Realm ,
285309 parent : $ ( 'parent' ) . text ( ) ,
286310 deprecated
@@ -335,7 +359,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
335359 parent : mainElement . attr ( 'parent' ) ! ,
336360 name : mainElement . attr ( 'name' ) ! ,
337361 address : address ,
338- description : $ ( 'description:first' ) . text ( ) ,
362+ description : markdownifyDescription ( $ , $ ( 'description:first' ) ) ,
339363 realm : $ ( 'realm:first' ) . text ( ) as Realm ,
340364 arguments : argumentList ,
341365 returns,
@@ -378,7 +402,7 @@ export class WikiPageMarkupScraper extends Scraper<WikiPage> {
378402 name : $el . attr ( 'name' ) ,
379403 parent : $el . attr ( 'parent' ) ,
380404 address : address ,
381- description : $ ( 'type summary' ) . text ( ) ,
405+ description : markdownifyDescription ( $ , $ ( 'type summary' ) ) ,
382406 deprecated : deprecated
383407 } ;
384408 }
0 commit comments