summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/PathNormalizer.js
blob: cdc325324765c0443d49fc3269c44e252798e93d (plain)
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
import path from "node:path";
import { fileURLToPath } from "node:url";
import { TemplatePath } from "@11ty/eleventy-utils";

export default class PathNormalizer {
	static getParts(inputPath) {
		if (!inputPath) {
			return [];
		}

		let separator = "/";
		if (inputPath.includes(path.sep)) {
			separator = path.sep;
		}

		return inputPath.split(separator).filter((entry) => entry !== ".");
	}

	// order is important here: the top-most directory returns first
	// array of file and all parent directories
	static getAllPaths(inputPath) {
		let parts = this.getParts(inputPath);
		let allPaths = [];

		let fullpath = "";
		for (let part of parts) {
			fullpath += (fullpath.length > 0 ? "/" : "") + part;
			allPaths.push(fullpath);
		}

		return allPaths;
	}

	static normalizeSeperator(inputPath) {
		if (!inputPath) {
			return inputPath;
		}
		return inputPath.split(path.sep).join("/");
	}

	static fullNormalization(inputPath) {
		if (typeof inputPath !== "string") {
			return inputPath;
		}

		// Fix file:///Users/ or file:///C:/ paths passed in
		if (inputPath.startsWith("file://")) {
			inputPath = fileURLToPath(inputPath);
		}

		// Paths should not be absolute (we convert absolute paths to relative)
		// Paths should not have a leading dot slash
		// Paths should always be `/` independent of OS path separator
		return TemplatePath.stripLeadingDotSlash(
			this.normalizeSeperator(TemplatePath.relativePath(inputPath)),
		);
	}
}