import fs from 'fs-extra'; import path from 'path'; import jsBeautify from 'js-beautify'; import { PageSources } from '../Page/PageSources.js'; import { NodeProcessor } from '../html/NodeProcessor.js'; import * as fsUtil from '../utils/fsUtil.js'; import type { ExternalManager, ExternalManagerConfig } from './ExternalManager.js'; const htmlBeautify = jsBeautify.html; /** * Represents external files (e.g. files referenced by ) * not directly included inside the generated page */ export class External { externalManager: ExternalManager; sourceFilePath: string; includedFiles: Set; userScriptsAndStyles: string[]; constructor(em: ExternalManager, srcFilePath: string, userScriptsAndStyles: string[]) { this.externalManager = em; this.sourceFilePath = srcFilePath; this.includedFiles = new Set([srcFilePath]); this.userScriptsAndStyles = userScriptsAndStyles; } /** * Generates the content of this External instance * @param asIfAtFilePath * @param resultPath * @param config * @return {Promise} */ async resolveDependency(asIfAtFilePath: string, resultPath: string, config: ExternalManagerConfig): Promise { const fileConfig = { ...config, headerIdMap: {}, }; const { variableProcessor, pluginManager, siteLinkManager } = config; const pageSources = new PageSources(); const docId = `ext-${fsUtil.removeExtension(path.basename(asIfAtFilePath))}`; const nodeProcessor = new NodeProcessor(fileConfig, pageSources, variableProcessor, pluginManager, siteLinkManager, this.userScriptsAndStyles, docId); const nunjucksProcessed = variableProcessor.renderWithSiteVariables(this.sourceFilePath, pageSources); const mdHtmlProcessed = await nodeProcessor.process(this.sourceFilePath, nunjucksProcessed, asIfAtFilePath) as string; const pluginPostRendered = pluginManager.postRender(nodeProcessor.frontmatter, mdHtmlProcessed); const outputContentHTML = process.env.TEST_MODE ? htmlBeautify(pluginPostRendered, pluginManager.htmlBeautifyOptions) : pluginPostRendered; await fs.outputFile(resultPath, outputContentHTML); pageSources.addAllToSet(this.includedFiles); await this.externalManager.generateDependencies(pageSources.getDynamicIncludeSrc(), this.includedFiles, this.userScriptsAndStyles); return this; } }