# All files / src/utils urlUtil.ts

73.91% Statements 17/23
28.57% Branches 2/7
66.66% Functions 4/6
73.91% Lines 17/23

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
    7620x
    20x

    20x

    448x

    448x

    448x

    448x
    448x

    448x

    20x

    500x

    34x

    20x
    20x
    20x
    20x
    20x

     import path from 'path';
    import pathIsInside from 'path-is-inside';

    const BOILERPLATE_FOLDER_NAME = '_markbind/boilerplates';

    /**
     * Calculates the absolute path of of the immediate parent site of the specified filePath.
     * @param filePath The absolute file path to look up that is nested inside the root directory
     * @param root The base directory from which to terminate the look up
     * @param lookUp The set of urls representing the sites' base directories
     * @return String The immediate parent site's absolute path.
     * @throws If a non-absolute path or path outside the root is given
     */
    function getParentSiteAbsolutePath(filePath: string, root: string, lookUp: Set<string>) {
      Iif (!path.isAbsolute(filePath)) {
        throw new Error(`Non-absolute path given to getParentSiteAbsolutePath: '${filePath}'`);
      }
      Iif (!pathIsInside(filePath, root)) {
        throw new Error(`Path given '${filePath}' is not in root '${root}'`);
      }

      function calculate(file: string): string {
        Iif (file === root) {
          return file;
        }

        const parent = path.dirname(file);
        return lookUp.has(parent)
          ? parent
          : calculate(parent);
      }

      return calculate(filePath);
    }

    /**
     * Calculates the absolute and relative path of of the immediate parent site of the specified filePath.
     */
    function getParentSiteAbsoluteAndRelativePaths(filePath: string, root: string, lookUp: Set<string>) {
      const absolute = getParentSiteAbsolutePath(filePath, root, lookUp);
      return {
        absolute,
        relative: path.relative(root, absolute),
      };
    }

    /**
     * Calculates a boilerplate's source file path at the immediate parent site of
     * the supplied file path.
     */
    function calculateBoilerplateFilePath(
      pathInBoilerplates: string, asIfAt: string, config: Record<string, any>,
    ) {
      return path.resolve(getParentSiteAbsolutePath(asIfAt, config.rootPath, config.baseUrlMap),
                          BOILERPLATE_FOLDER_NAME, pathInBoilerplates);
    }

    const isUrlRegex = /^(?:[a-z]+:)?\/\//i;
    function isUrl(unknownPath: string) {
      return isUrlRegex.test(unknownPath);
    }

    function stripBaseUrl(src: string, baseUrl: string) {
      return src.startsWith(baseUrl)
        ? src.substring(baseUrl.length)
        : src;
    }

    export {
      getParentSiteAbsolutePath,
      getParentSiteAbsoluteAndRelativePaths,
      calculateBoilerplateFilePath,
      isUrl,
      stripBaseUrl,
    };