import fs from 'fs-extra' ; import { resolve } from 'path' ; import yaml from 'yaml' ; interface JupyterBookFileData { file : string ; } interface JupyterBookSection extends JupyterBookFileData { sections : JupyterBookSection [ ] ; } interface JupyterBookChapter { chapters : JupyterBookSection [ ] ; } type JupyterBookTOCItem = JupyterBookChapter | JupyterBookSection | JupyterBookFileData ; type JupyterBookTOC = JupyterBookTOCItem [ ] ; function isJupyterBookChapter ( item ) { return ! ! ( item as JupyterBookChapter ) . chapters ; } function isJupyterBookSection ( item ) { return ! ! ( item as JupyterBookSection ) . sections ; } returns export async function getTOCFiles ( ) { const tocString = await fs . readFile ( '_toc.yml' , 'utf-8' ) ; const tocData = yaml . parse ( tocString ) as JupyterBookTOC ; const files = [ ] ; const stack = tocData . slice ( ) . reverse ( ) ; while ( stack . length > 0 ) { const item = stack . pop ( ) ; if ( ! item ) break ; if ( isJupyterBookChapter ( item ) ) { const { chapters } = item ; for ( const chapter of chapters ) { if ( chapter . file ) { files . push ( chapter . file ) ; if ( chapter . sections ) { stack . push ( ... chapter . sections ) ; } } } } else if ( item . file ) { files . push ( item . file ) ; if ( isJupyterBookSection ( item ) && item . sections ) { stack . push ( ... item . sections ) ; } } } return files ; } /**
 * Directories that should be ignored when looking for HTML files.
 */ const IGNORED_DIRS = [ '_images' , '_static' , '_panels_static' , '_sources' , ] ; /**
 * Files that should be ignored when an HTML file is found.
 */ const IGNORED_FILES = [ 'genindex.html' , 'py-modindex.html' ] ; returns export async function getHTMLFiles ( ) { const buildDir = resolve ( __dirname , '../../../_build/html' ) ; const files = [ buildDir ] ; const result = new Set < string > ( ) ; while ( files . length > 0 ) { const file = files . pop ( ) ! ; const stats = await fs . stat ( file ) ; if ( stats . isDirectory ( ) && ! IGNORED_DIRS . some ( ( ignoredDir ) => file . includes ( ignoredDir ) ) ) { const nextFiles = await fs . readdir ( file ) ; for ( const nextFile of nextFiles ) { files . push ( resolve ( file , nextFile ) ) ; } } else if ( stats . isFile ( ) && file . endsWith ( '.html' ) && ! IGNORED_FILES . some ( ( ignoredFile ) => file . includes ( ignoredFile ) ) ) { result . add ( file ) ; } } return Array . from ( result ) ; }