@@ -30,6 +30,19 @@ interface ConversionResult {
3030 data : Frontmatter
3131}
3232
33+ // The conversion mutates the mdast tree as loosely typed node bags (changing
34+ // node.type, value, url, etc.), so model the handful of fields we touch.
35+ interface MdNode {
36+ type : string
37+ value : string
38+ depth : number
39+ lang ?: string
40+ meta ?: string
41+ url : string
42+ title ?: string
43+ children : MdNode [ ]
44+ }
45+
3346const config : Config = JSON . parse (
3447 await readFile ( path . join ( 'src/codeql-cli/lib/config.json' ) , 'utf-8' ) ,
3548)
@@ -51,10 +64,11 @@ export async function convertContentToDocs(
5164 let depth = 0
5265 let secondaryOptions = false
5366 const frontmatter : Frontmatter = { title : '' , ...frontmatterDefaults }
54- const akaMsLinkMatches : any [ ] = [ ]
67+ const akaMsLinkMatches : MdNode [ ] = [ ]
5568
5669 // Visit all heading nodes
57- visit ( ast , 'heading' , ( node : any ) => {
70+ visit ( ast , 'heading' , ( rawNode ) => {
71+ const node = rawNode as unknown as MdNode
5872 // This is the title of the article, so we want to store it to
5973 // the frontmatter
6074 if ( node . depth === 1 ) {
@@ -85,7 +99,8 @@ export async function convertContentToDocs(
8599
86100 // Visit heading and paragraph nodes to get intro text
87101 let currentNodeIsDescription = false
88- visit ( ast , ( node : any ) => {
102+ visit ( ast , ( rawNode ) => {
103+ const node = rawNode as unknown as MdNode
89104 if ( node . type !== 'heading' && node . type !== 'paragraph' ) return false
90105
91106 // The first paragraph sibling to the heading "Description" is the
@@ -105,10 +120,13 @@ export async function convertContentToDocs(
105120 const matchNodeTypes = [ 'text' , 'code' , 'link' ]
106121 visitParents (
107122 ast ,
108- ( node : any ) => {
109- return node && matchNodeTypes . includes ( node . type )
123+ ( rawNode ) => {
124+ const node = rawNode as unknown as MdNode
125+ return Boolean ( node && matchNodeTypes . includes ( node . type ) )
110126 } ,
111- ( node : any , ancestors : any [ ] ) => {
127+ ( rawNode , rawAncestors ) => {
128+ const node = rawNode as unknown as MdNode
129+ const ancestors = rawAncestors as unknown as MdNode [ ]
112130 // Add the copy button to the example command
113131 if ( node . type === 'code' && node . value . startsWith ( `codeql ${ frontmatter . title } ` ) ) {
114132 node . lang = 'shell'
@@ -166,7 +184,7 @@ export async function convertContentToDocs(
166184 if ( node . type === 'text' && node . value . includes ( '{.interpreted-text' ) ) {
167185 const paragraph = ancestors [ ancestors . length - 1 ] . children
168186 const docRoleTagChild = paragraph . findIndex (
169- ( child : any ) => child . value && child . value . includes ( '{.interpreted-text' ) ,
187+ ( child : MdNode ) => child . value && child . value . includes ( '{.interpreted-text' ) ,
170188 )
171189 const link = paragraph [ docRoleTagChild - 1 ]
172190 // If child node is already a link node, skip it
@@ -200,8 +218,8 @@ export async function convertContentToDocs(
200218 // Make the previous sibling node a link
201219 link . type = 'link'
202220 link . url = `${ RELATIVE_LINK_PATH } /${ linkPath } `
203- link . children = [ { type : 'text' , value : linkText } ]
204- delete link . value
221+ link . children = [ { type : 'text' , value : linkText } ] as unknown as MdNode [ ]
222+ delete ( link as { value ?: string } ) . value
205223 }
206224 }
207225
@@ -226,18 +244,25 @@ export async function convertContentToDocs(
226244 nodeAfter . value = nodeAfter . value . slice ( 1 )
227245 }
228246 // Change the node to an inline code node
229- node . type = 'inlineCode'
230- node . value = node . url
231- node . title = undefined
232- node . url = undefined
233- node . children = undefined
247+ const inlineCode = node as {
248+ type : string
249+ value : string
250+ url ?: string
251+ title ?: string
252+ children ?: MdNode [ ]
253+ }
254+ inlineCode . type = 'inlineCode'
255+ inlineCode . value = node . url
256+ inlineCode . title = undefined
257+ inlineCode . url = undefined
258+ inlineCode . children = undefined
234259 }
235260 } ,
236261 )
237262
238263 // Convert all aka.ms links to the docs.github.com relative path
239264 await Promise . all (
240- akaMsLinkMatches . map ( async ( node : any ) => {
265+ akaMsLinkMatches . map ( async ( node : MdNode ) => {
241266 const url = await getRedirect ( node . url )
242267 // The aka.ms urls are Markdown links in the ast already,
243268 // so we only need to update the url and description
@@ -250,11 +275,20 @@ export async function convertContentToDocs(
250275 )
251276
252277 // remove the program section from the AST
253- remove ( ast , ( node : any ) => node . value && node . value . startsWith ( PROGRAM_SECTION ) )
278+ remove ( ast , ( rawNode ) => {
279+ const node = rawNode as unknown as MdNode
280+ return Boolean ( node . value && node . value . startsWith ( PROGRAM_SECTION ) )
281+ } )
254282 // remove the first heading from the AST because that becomes frontmatter
255- remove ( ast , ( node : any ) => node . type === 'heading' && node . depth === 1 )
283+ remove ( ast , ( rawNode ) => {
284+ const node = rawNode as unknown as MdNode
285+ return node . type === 'heading' && node . depth === 1
286+ } )
256287
257- return { content : toMarkdown ( ast , MARKDOWN_OPTIONS as any ) , data : frontmatter }
288+ return {
289+ content : toMarkdown ( ast , MARKDOWN_OPTIONS as Parameters < typeof toMarkdown > [ 1 ] ) ,
290+ data : frontmatter ,
291+ }
258292}
259293
260294// performs a get request for a aka.ms url and returns the redirect url
0 commit comments