import fs from 'fs' ; import path from 'path' ; const readmeFilePath = path . resolve ( __dirname , '../docs/README.md' ) ; fs . readFile ( readmeFilePath , 'utf8' , ( err , data ) => { if ( err ) throw err ; const lowerCaseFileContents = data . toLowerCase ( ) ; const regexMdLinks = /\[.+?\]\(#.+?\)/gm ; const markdownInternalLinks = data . match ( regexMdLinks ) ; if ( ! markdownInternalLinks ) { throw new Error ( 'No internal links found in README.' ) ; } const singleLinkRegex = /\[(?<text>.+?)\]\(#(?<targetSectionHeading>.+?)\)/ ; for ( let i = 0 , numInternalLinks = markdownInternalLinks . length ; i < numInternalLinks ; i += 1 ) { const targetSectionHeading = markdownInternalLinks [ i ] . match ( singleLinkRegex ) ?. groups ?. targetSectionHeading ; if ( ! targetSectionHeading ) { throw new Error ( `Could not parse link target from ${ markdownInternalLinks [ i ] }` ) ; } const formattedTargetHeading = targetSectionHeading . replace ( /-/g , ' ' ) ; if ( ! lowerCaseFileContents . includes ( `# ${ formattedTargetHeading }` ) ) { throw new Error ( `README file contains link to #${ targetSectionHeading }, but no section with that heading exists.` ) ; } } console . log ( 'README file is valid!' ) ; } ) ;