# All files / src/Layout LayoutManager.js

25.71% Statements 9/35
0% Branches 0/16
11.11% Functions 1/9
25.71% Lines 9/35

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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    873x
    3x

    3x

    3x

    3x

    3x

    3x

    3x

    3x

     const fs = require('fs-extra');
    const path = require('path');

    const { Layout } = require('./Layout');

    const logger = require('../utils/logger');

    const FRONTMATTER_NONE_ATTR = 'none';

    class LayoutManager {
      constructor(config) {
        this.config = config;

        this.layoutsRootPath = path.join(config.rootPath, '_markbind', 'layouts');

        this.layouts = {};
      }

      /**
       * Flag all layouts for (re)generation when requested
       */
      removeLayouts() {
        this.layouts = {};
      }

      /**
       * Update layouts which have the provided filePaths as dependencies
       */
      updateLayouts(filePaths) {
        const layoutsToRegenerate = Object.entries(this.layouts)
          .filter(([, layout]) => layout.shouldRegenerate(filePaths));

        return Promise.all(layoutsToRegenerate.map(([name, layout]) => {
          this.layouts[name] = new Layout(layout.sourceFilePath, this.config);
          return this.layouts[name].generate();
        }));
      }

      generateLayoutIfNeeded(name) {
        if (this.layouts[name]) {
          return this.layouts[name].generatePromise;
        }

        const layoutPath = path.join(this.layoutsRootPath, name);
        if (!fs.existsSync(layoutPath)) {
          logger.error(`'${name}' layout does not exist`);
          return Promise.resolve();
        }

        this.layouts[name] = new Layout(layoutPath, this.config);
        this.layouts[name].generatePromise = this.layouts[name].generate();
        return this.layouts[name].generatePromise;
      }

      layoutHasPageNav(name) {
        if (name === FRONTMATTER_NONE_ATTR) {
          return false;
        }

        return this.layouts[name] && this.layouts[name].layoutPageNavUuid;
      }

      combineLayoutWithPage(name, pageContent, pageNav, pageIncludedFiles) {
        if (name === FRONTMATTER_NONE_ATTR) {
          return pageContent;
        }

        if (!this.layouts[name]) {
          return pageContent;
        }

        return this.layouts[name].insertPage(pageContent, pageNav, pageIncludedFiles);
      }

      getLayoutPageNjkAssets(name) {
        if (name === FRONTMATTER_NONE_ATTR || !this.layouts[name]) {
          return {};
        }

        return this.layouts[name].getPageNjkAssets();
      }
    }

    module.exports = {
      LayoutManager,
    };