import{readFile}from"../file";import{htmlPage}from"./dsl";importtype{PageSyntax,Dependency}from"../types/html";import{File}from"../file/classes";importtype{PageSettings}from"../types/site";/** * Is the link string that we are provided internal? * @param l the link to reference * @param settings page settings configuration * @returns */constisInternalLink=(l: string,{ rootUrl }: {rootUrl: string})=>{if(l.includes(rootUrl)){returntrue;}constisExternal=l.includes("data:image")||l.includes("http://")||l.includes("https://")||l.startsWith("#");return!isExternal;};/** * Convert a link string to a legitimate file path on disk. * * @param l the link to convert * @param settings page settings to carry */constlinkStringToFile=(l: string,{ rootUrl, sourceDir }: {rootUrl: string;sourceDir: string})=>{// remove the leading rootUrl from the link if it existsconstlinkWithoutRoot=l.replace(rootUrl,"").replace("http://","").replace("https://","");// path the now-local url to the source dirreturnsourceDir.toString().concat(linkWithoutRoot);};constmakeDependencies=(dependencies: Dependency[],settings: PageSettings): File[]=>{constres: File[]=[];dependencies.forEach((dep)=>{if(!isInternalLink(dep.src,settings)){return;}if(dep.src===settings.targetDir){return;}try{constfile=readFile(linkStringToFile(dep.src,settings),settings);res.push(file);}catch(e){// console.warn(`Dependency file ${dep.src} doesn't exist`);}});returnres;};/** * An HTML AST builder. * May or may not be assocaited with a file. */classHtmlPage{privatepageStructure: PageSyntax;privatecurrentBuildSettings: PageSettings;privatecachedDependencies: File[]|undefined;constructor(pageSyntax: PageSyntax,settings: PageSettings){this.pageStructure=pageSyntax;this.currentBuildSettings=settings;}staticcreate(pageSyntax: PageSyntax,settings: PageSettings){returnnewthis(pageSyntax,settings);}// Find all of the dependencies in the page.// We define a 'dependency' as any internal link.// For example -- src/index.css, src/file.html, etc.// Produces these dependencies as Files.dependencies(settings=this.currentBuildSettings){if(!this.cachedDependencies){this.toString();}returnthis.cachedDependencies??[];}toString(){const{ dependsOn, body }=htmlPage(this.pageStructure);this.cachedDependencies=makeDependencies(dependsOn,this.currentBuildSettings);returnbody;}}exportdefaultHtmlPage;