import * as visit from 'unist-util-visit' ; import * as remove from 'unist-util-remove' ; import { electronLatestStableTag } from '../../package.json' ; import { Node } from 'unist' ; const langRegex = /(javascript.*|js.*)/ ; const metaRegex = /fiddle='([^']*)'(.*)/ ; interface IAdditionalNode extends Node { lang : string ; meta : string ; data : { hProperties : Record < string , string > ; } ; } export const fiddleUrls = ( ) => ( tree ) => { visit ( tree , 'code' , ( node ) => { if ( ! node . lang || ! node . meta ) return ; if ( node . lang === 'fiddle' || node . value === '' ) { remove ( tree , node ) ; } const langMatch = node . lang . match ( langRegex ) ; const metaMatch = node . meta . match ( metaRegex ) ; if ( langMatch && metaMatch ) { const [ folder , ... _ ] = metaMatch [ 1 ] . split ( '|' ) ; const url = `electron/${ electronLatestStableTag }/${ folder }` ; node . data = node . data || { } ; node . data . hProperties = node . data . hProperties || { } ; node . data . hProperties . dataFiddleUrl = url ; } } ) ; return tree ; } ;