# All files / src/External External.ts

33.33% Statements 8/24
0% Branches 0/2
0% Functions 0/2
33.33% Lines 8/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    651x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

     import fs from 'fs-extra';
    import path from 'path';
    import * as jsBeautify from 'js-beautify';
    import { PageSources } from '../Page/PageSources';
    import { NodeProcessor } from '../html/NodeProcessor';
    import * as fsUtil from '../utils/fsUtil';
    import type { ExternalManager, ExternalManagerConfig } from './ExternalManager';

    const htmlBeautify = jsBeautify.html;

    /**
     * Represents external files (e.g. files referenced by <panel src="...">)
     * not directly included inside the generated page
     */
    export class External {
      externalManager: ExternalManager;
      sourceFilePath: string;
      includedFiles: Set<string>;

      constructor(em: ExternalManager, srcFilePath: string) {
        this.externalManager = em;
        this.sourceFilePath = srcFilePath;
        this.includedFiles = new Set([srcFilePath]);
      }

      /**
       * Generates the content of this External instance
       * @param asIfAtFilePath
       * @param resultPath
       * @param config
       * @return {Promise<External>}
       */
      async resolveDependency(asIfAtFilePath: string, resultPath: string, config: ExternalManagerConfig):
      Promise<External> {
        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, undefined, 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);

        return this;
      }
    }