summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy/src/Util/PathNormalizer.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@11ty/eleventy/src/Util/PathNormalizer.js')
-rw-r--r--node_modules/@11ty/eleventy/src/Util/PathNormalizer.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy/src/Util/PathNormalizer.js b/node_modules/@11ty/eleventy/src/Util/PathNormalizer.js
new file mode 100644
index 0000000..cdc3253
--- /dev/null
+++ b/node_modules/@11ty/eleventy/src/Util/PathNormalizer.js
@@ -0,0 +1,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)),
+ );
+ }
+}