@@ -7,15 +7,25 @@ import yaml from 'js-yaml'
77import fs from 'fs'
88import { visit } from 'unist-util-visit'
99import { h } from 'hastscript'
10+ // @ts -ignore - no types available for @primer/octicons
1011import octicons from '@primer/octicons'
1112import { parse } from 'parse5'
1213import { fromParse5 } from 'hast-util-from-parse5'
1314import murmur from 'imurmurhash'
1415import { getPrompt } from './copilot-prompt'
16+ import type { Element } from 'hast'
1517
16- const languages = yaml . load ( fs . readFileSync ( './data/code-languages.yml' , 'utf8' ) )
18+ interface LanguageConfig {
19+ name : string
20+ [ key : string ] : any
21+ }
22+
23+ type Languages = Record < string , LanguageConfig >
24+
25+ const languages = yaml . load ( fs . readFileSync ( './data/code-languages.yml' , 'utf8' ) ) as Languages
1726
18- const matcher = ( node ) =>
27+ // Using any due to conflicting unist/hast type definitions between dependencies
28+ const matcher = ( node : any ) : boolean =>
1929 node . type === 'element' &&
2030 node . tagName === 'pre' &&
2131 // For now, limit to ones with the copy or prompt meta,
@@ -25,28 +35,39 @@ const matcher = (node) =>
2535 ! getPreMeta ( node ) . annotate
2636
2737export default function codeHeader ( ) {
28- return ( tree ) => {
29- visit ( tree , matcher , ( node , index , parent ) => {
30- parent . children [ index ] = wrapCodeExample ( node , tree )
38+ // Using any due to conflicting unist/hast type definitions between dependencies
39+ return ( tree : any ) => {
40+ // Using any due to conflicting unist/hast type definitions between dependencies
41+ visit ( tree , matcher , ( node : any , index : number | undefined , parent : any ) => {
42+ if ( index !== undefined && parent ) {
43+ parent . children [ index ] = wrapCodeExample ( node , tree )
44+ }
3145 } )
3246 }
3347}
3448
35- function wrapCodeExample ( node , tree ) {
36- const lang = node . children [ 0 ] . properties . className ?. [ 0 ] . replace ( 'language-' , '' )
37- const code = node . children [ 0 ] . children [ 0 ] . value
49+ // Using any due to conflicting unist/hast type definitions between dependencies
50+ function wrapCodeExample ( node : any , tree : any ) : Element {
51+ const lang : string = node . children [ 0 ] . properties . className ?. [ 0 ] . replace ( 'language-' , '' )
52+ const code : string = node . children [ 0 ] . children [ 0 ] . value
3853
3954 const subnav = null // getSubnav() lives in annotate.js, not needed for normal code blocks
4055 const prompt = getPrompt ( node , tree , code ) // returns null if there's no prompt
41- const hasCopy = Boolean ( getPreMeta ( node ) . copy ) // defaults to true
56+ const hasCopy : boolean = Boolean ( getPreMeta ( node ) . copy ) // defaults to true
4257
4358 const headerHast = header ( lang , code , subnav , prompt , hasCopy )
4459
4560 return h ( 'div' , { className : 'code-example' } , [ headerHast , node ] )
4661}
4762
48- export function header ( lang , code , subnav = null , prompt = null , hasCopy = true ) {
49- const codeId = murmur ( 'js-btn-copy' ) . hash ( code ) . result ( )
63+ export function header (
64+ lang : string ,
65+ code : string ,
66+ subnav : Element | null = null ,
67+ prompt : Element | null = null ,
68+ hasCopy : boolean = true ,
69+ ) : Element {
70+ const codeId : string = murmur ( 'js-btn-copy' ) . hash ( code ) . result ( ) . toString ( )
5071
5172 return h (
5273 'header' ,
@@ -83,14 +104,16 @@ export function header(lang, code, subnav = null, prompt = null, hasCopy = true)
83104 )
84105}
85106
86- function btnIcon ( ) {
87- const btnIconHtml = octicons . copy . toSVG ( )
107+ function btnIcon ( ) : Element {
108+ const btnIconHtml : string = octicons . copy . toSVG ( )
88109 const btnIconAst = parse ( String ( btnIconHtml ) , { sourceCodeLocationInfo : true } )
110+ // @ts -ignore - fromParse5 file option typing issue
89111 const btnIcon = fromParse5 ( btnIconAst , { file : btnIconHtml } )
90- return btnIcon
112+ return btnIcon as Element
91113}
92114
93- export function getPreMeta ( node ) {
115+ // Using any due to conflicting unist/hast type definitions between dependencies
116+ export function getPreMeta ( node : any ) : Record < string , any > {
94117 // Here's why this monstrosity works:
95118 // https://github.com/syntax-tree/mdast-util-to-hast/blob/c87cd606731c88a27dbce4bfeaab913a9589bf83/lib/handlers/code.js#L40-L42
96119 return node . children [ 0 ] ?. data ?. meta || { }
0 commit comments