# All files / src/html SiteLinkManager.ts

76.92% Statements 20/26
85.71% Branches 6/7
50% Functions 3/6
80% Lines 20/25

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
    737x
    7x

    7x

    7x

    7x

    95x
    95x

    4x
    4x

    4x

    12x
    2x

    10x
    10x
    2x

    8x
    8x
    4x

    4x
    4x

     import has from 'lodash/has';
    import * as linkProcessor from './linkProcessor';
    import type { NodeProcessorConfig } from './NodeProcessor';
    import { MbNode } from '../utils/node';

    const _ = { has };

    const tagsToValidate: Set<string> = new Set([
      'img',
      'pic',
      'thumbnail',
      'a',
      'link',
      'script',
    ]);

    export class SiteLinkManager {
      private config: NodeProcessorConfig;
      private intralinkCollection: Map<string, Set<string>>;

      constructor(config: NodeProcessorConfig) {
        this.config = config;
        this.intralinkCollection = new Map();
      }

      /**
       * Adds a resourcePath and cwf to the intralinkCollection,
       * ensuring each pair of (resourcePath, cwf) appears only once
       */
      _addToCollection(resourcePath: string, cwf: string) {
        if (!this.intralinkCollection.has(cwf)) {
          this.intralinkCollection.set(cwf, new Set());
        }
        // We have checked and set cwf in intralinkCollection above
        this.intralinkCollection.get(cwf)!.add(resourcePath);
      }

      validateAllIntralinks() {
        Iif (!this.config.intrasiteLinkValidation.enabled) {
          return;
        }

        this.intralinkCollection.forEach((resourcePaths, cwf) => {
          resourcePaths.forEach(resourcePath => linkProcessor.validateIntraLink(resourcePath, cwf, this.config));
        });

        this.intralinkCollection = new Map();
      }

      /**
       * Add a link to the intralinkCollection to be validated later,
       * if the node should be validated and intralink validation is not disabled.
       */
      collectIntraLinkToValidate(node: MbNode, cwf: string) {
        if (!tagsToValidate.has(node.name)) {
          return 'Should not validate';
        }

        const hasIntralinkValidationDisabled = _.has(node.attribs, 'no-validation');
        if (hasIntralinkValidationDisabled) {
          return 'Intralink validation disabled';
        }

        const resourcePath = linkProcessor.getDefaultTagsResourcePath(node);
        if (!resourcePath || !linkProcessor.isIntraLink(resourcePath)) {
          return 'Should not validate';
        }

        this._addToCollection(resourcePath, cwf);
        return 'Intralink collected to be validated later';
      }
    }