import { readFileSync , writeFileSync , ensureDirSync } from 'fs-extra' ; import { glob } from 'glob' ; import { dirname , extname , join , relative } from 'path' ; import { regionParser } from '../region-parser/region-parser' ; import { highlightCodeBlock } from './highlight-code-block' ; function detectAndHighlightRegionBlocks ( parsed , basePath , outDir ) { const fileExtension = extname ( basePath ) . substring ( 1 ) ; for ( const [ regionName , regionSnippet ] of Object . entries ( parsed . regions ) ) { if ( ! regionName ) { continue ; } const highlightedRegion = highlightCodeBlock ( regionSnippet , fileExtension ) ; const regionBaseOutputPath = basePath . replace ( `.${ fileExtension }` , `_${ regionName }-${ fileExtension }.html` ) ; const regionOutputPath = join ( outDir , regionBaseOutputPath ) ; ensureDirSync ( dirname ( regionOutputPath ) ) ; writeFileSync ( regionOutputPath , highlightedRegion ) ; } } if ( require . main === module ) { const outDir = 'dist/docs-content/examples-highlighted' ; const packageName = 'packages/mosaic-examples' ; const inputFiles = glob . sync ( 'packages/mosaic-examples/mosaic/**/*.{html,css,ts}' , { ignore : [ '**/index.ts' , '**/modules.ts' ] } ) ; for ( const execPath of inputFiles ) { const basePath = relative ( packageName , execPath ) ; const fileExtension = extname ( basePath ) . substring ( 1 ) ; const parsed = regionParser ( readFileSync ( execPath , 'utf8' ) , fileExtension ) ; detectAndHighlightRegionBlocks ( parsed , basePath , outDir ) ; const baseOutputPath = basePath . replace ( `.${ fileExtension }` , `-${ fileExtension }.html` ) ; const outputPath = join ( outDir , baseOutputPath ) ; const htmlOutput = highlightCodeBlock ( parsed . contents , fileExtension ) ; ensureDirSync ( dirname ( outputPath ) ) ; writeFileSync ( outputPath , htmlOutput ) ; } }