Striven
yesterday c538c151c7462ad0395ff2c15c5e11e89e362aa8
quartz/util/path.test.ts
@@ -1,45 +1,25 @@
import test, { describe } from "node:test"
import * as path from "./path"
import assert from "node:assert"
import { CanonicalSlug, ServerSlug, TransformOptions } from "./path"
import { FullSlug, TransformOptions, SimpleSlug } from "./path"
describe("typeguards", () => {
  test("isClientSlug", () => {
    assert(path.isClientSlug("http://example.com"))
    assert(path.isClientSlug("http://example.com/index"))
    assert(path.isClientSlug("http://example.com/index.html"))
    assert(path.isClientSlug("http://example.com/"))
    assert(path.isClientSlug("https://example.com"))
    assert(path.isClientSlug("https://example.com/abc/def"))
    assert(path.isClientSlug("https://example.com/abc/def/"))
    assert(path.isClientSlug("https://example.com/abc/def#cool"))
    assert(path.isClientSlug("https://example.com/abc/def?field=1&another=2"))
    assert(path.isClientSlug("https://example.com/abc/def?field=1&another=2#cool"))
    assert(path.isClientSlug("https://example.com/abc/def.html?field=1&another=2#cool"))
  test("isSimpleSlug", () => {
    assert(path.isSimpleSlug(""))
    assert(path.isSimpleSlug("abc"))
    assert(path.isSimpleSlug("abc/"))
    assert(path.isSimpleSlug("notindex"))
    assert(path.isSimpleSlug("notindex/def"))
    assert(!path.isClientSlug("./"))
    assert(!path.isClientSlug(""))
    assert(!path.isClientSlug("ipfs://example.com"))
    assert(!path.isClientSlug("http"))
    assert(!path.isClientSlug("https"))
  })
  test("isCanonicalSlug", () => {
    assert(path.isCanonicalSlug(""))
    assert(path.isCanonicalSlug("abc"))
    assert(path.isCanonicalSlug("notindex"))
    assert(path.isCanonicalSlug("notindex/def"))
    assert(!path.isCanonicalSlug("//"))
    assert(!path.isCanonicalSlug("index"))
    assert(!path.isCanonicalSlug("https://example.com"))
    assert(!path.isCanonicalSlug("/abc"))
    assert(!path.isCanonicalSlug("abc/"))
    assert(!path.isCanonicalSlug("abc/index"))
    assert(!path.isCanonicalSlug("abc#anchor"))
    assert(!path.isCanonicalSlug("abc?query=1"))
    assert(!path.isCanonicalSlug("index.md"))
    assert(!path.isCanonicalSlug("index.html"))
    assert(!path.isSimpleSlug("//"))
    assert(!path.isSimpleSlug("index"))
    assert(!path.isSimpleSlug("https://example.com"))
    assert(!path.isSimpleSlug("/abc"))
    assert(!path.isSimpleSlug("abc/index"))
    assert(!path.isSimpleSlug("abc#anchor"))
    assert(!path.isSimpleSlug("abc?query=1"))
    assert(!path.isSimpleSlug("index.md"))
    assert(!path.isSimpleSlug("index.html"))
  })
  test("isRelativeURL", () => {
@@ -58,18 +38,29 @@
    assert(!path.isRelativeURL("./abc/def.md"))
  })
  test("isServerSlug", () => {
    assert(path.isServerSlug("index"))
    assert(path.isServerSlug("abc/def"))
    assert(path.isServerSlug("html.energy"))
    assert(path.isServerSlug("test.pdf"))
  test("isAbsoluteURL", () => {
    assert(path.isAbsoluteURL("https://example.com"))
    assert(path.isAbsoluteURL("http://example.com"))
    assert(path.isAbsoluteURL("ftp://example.com/a/b/c"))
    assert(path.isAbsoluteURL("http://host/%25"))
    assert(path.isAbsoluteURL("file://host/twoslashes?more//slashes"))
    assert(!path.isServerSlug("."))
    assert(!path.isServerSlug("./abc/def"))
    assert(!path.isServerSlug("../abc/def"))
    assert(!path.isServerSlug("abc/def#anchor"))
    assert(!path.isServerSlug("abc/def?query=1"))
    assert(!path.isServerSlug("note with spaces"))
    assert(!path.isAbsoluteURL("example.com/abc/def"))
    assert(!path.isAbsoluteURL("abc"))
  })
  test("isFullSlug", () => {
    assert(path.isFullSlug("index"))
    assert(path.isFullSlug("abc/def"))
    assert(path.isFullSlug("html.energy"))
    assert(path.isFullSlug("test.pdf"))
    assert(!path.isFullSlug("."))
    assert(!path.isFullSlug("./abc/def"))
    assert(!path.isFullSlug("../abc/def"))
    assert(!path.isFullSlug("abc/def#anchor"))
    assert(!path.isFullSlug("abc/def?query=1"))
    assert(!path.isFullSlug("note with spaces"))
  })
  test("isFilePath", () => {
@@ -100,40 +91,17 @@
    }
  }
  test("canonicalizeServer", () => {
  test("simplifySlug", () => {
    asserts(
      [
        ["index", ""],
        ["abc/index", "abc"],
        ["index", "/"],
        ["abc", "abc"],
        ["abc/index", "abc/"],
        ["abc/def", "abc/def"],
      ],
      path.canonicalizeServer,
      path.isServerSlug,
      path.isCanonicalSlug,
    )
  })
  test("canonicalizeClient", () => {
    asserts(
      [
        ["http://localhost:3000", ""],
        ["http://localhost:3000/index", ""],
        ["http://localhost:3000/test", "test"],
        ["http://example.com", ""],
        ["http://example.com/index", ""],
        ["http://example.com/index.html", ""],
        ["http://example.com/", ""],
        ["https://example.com", ""],
        ["https://example.com/abc/def", "abc/def"],
        ["https://example.com/abc/def/", "abc/def"],
        ["https://example.com/abc/def#cool", "abc/def"],
        ["https://example.com/abc/def?field=1&another=2", "abc/def"],
        ["https://example.com/abc/def?field=1&another=2#cool", "abc/def"],
        ["https://example.com/abc/def.html?field=1&another=2#cool", "abc/def"],
      ],
      path.canonicalizeClient,
      path.isClientSlug,
      path.isCanonicalSlug,
      path.simplifySlug,
      path.isFullSlug,
      path.isSimpleSlug,
    )
  })
@@ -148,10 +116,14 @@
        ["index.md", "index"],
        ["test.mp4", "test.mp4"],
        ["note with spaces.md", "note-with-spaces"],
        ["notes.with.dots.md", "notes.with.dots"],
        ["test/special chars?.md", "test/special-chars"],
        ["test/special chars #3.md", "test/special-chars-3"],
        ["cool/what about r&d?.md", "cool/what-about-r-and-d"],
      ],
      path.slugifyFilePath,
      path.isFilePath,
      path.isServerSlug,
      path.isFullSlug,
    )
  })
@@ -186,19 +158,52 @@
  test("pathToRoot", () => {
    asserts(
      [
        ["", "."],
        ["abc", ".."],
        ["abc/def", "../.."],
        ["index", "."],
        ["abc", "."],
        ["abc/def", ".."],
        ["abc/def/ghi", "../.."],
        ["abc/def/index", "../.."],
      ],
      path.pathToRoot,
      path.isCanonicalSlug,
      path.isFullSlug,
      path.isRelativeURL,
    )
  })
  test("joinSegments", () => {
    assert.strictEqual(path.joinSegments("a", "b"), "a/b")
    assert.strictEqual(path.joinSegments("a/", "b"), "a/b")
    assert.strictEqual(path.joinSegments("a", "b/"), "a/b/")
    assert.strictEqual(path.joinSegments("a/", "b/"), "a/b/")
    // preserve leading and trailing slashes
    assert.strictEqual(path.joinSegments("/a", "b"), "/a/b")
    assert.strictEqual(path.joinSegments("/a/", "b"), "/a/b")
    assert.strictEqual(path.joinSegments("/a", "b/"), "/a/b/")
    assert.strictEqual(path.joinSegments("/a/", "b/"), "/a/b/")
    // lone slash
    assert.strictEqual(path.joinSegments("/a/", "b", "/"), "/a/b/")
    assert.strictEqual(path.joinSegments("a/", "b" + "/"), "a/b/")
    // works with protocol specifiers
    assert.strictEqual(path.joinSegments("https://example.com", "a"), "https://example.com/a")
    assert.strictEqual(path.joinSegments("https://example.com/", "a"), "https://example.com/a")
    assert.strictEqual(path.joinSegments("https://example.com", "a/"), "https://example.com/a/")
    assert.strictEqual(path.joinSegments("https://example.com/", "a/"), "https://example.com/a/")
  })
})
describe("link strategies", () => {
  const allSlugs = ["a/b/c", "a/b/d", "a/b/index", "e/f", "e/g/h", "index", "a/test.png"] as ServerSlug[]
  const allSlugs = [
    "a/b/c",
    "a/b/d",
    "a/b/index",
    "e/f",
    "e/g/h",
    "index",
    "a/test.png",
  ] as FullSlug[]
  describe("absolute", () => {
    const opts: TransformOptions = {
@@ -207,28 +212,28 @@
    }
    test("from a/b/c", () => {
      const cur = "a/b/c" as CanonicalSlug
      assert.strictEqual(path.transformLink(cur, "a/b/d", opts), "../../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../../a/b/")
      assert.strictEqual(path.transformLink(cur, "e/f", opts), "../../../e/f")
      assert.strictEqual(path.transformLink(cur, "e/g/h", opts), "../../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "index", opts), "../../../")
      assert.strictEqual(path.transformLink(cur, "index.png", opts), "../../../index.png")
      assert.strictEqual(path.transformLink(cur, "index#abc", opts), "../../../#abc")
      assert.strictEqual(path.transformLink(cur, "tag/test", opts), "../../../tag/test")
      assert.strictEqual(path.transformLink(cur, "a/b/c#test", opts), "../../../a/b/c#test")
      assert.strictEqual(path.transformLink(cur, "a/test.png", opts), "../../../a/test.png")
      const cur = "a/b/c" as FullSlug
      assert.strictEqual(path.transformLink(cur, "a/b/d", opts), "../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../a/b/")
      assert.strictEqual(path.transformLink(cur, "e/f", opts), "../../e/f")
      assert.strictEqual(path.transformLink(cur, "e/g/h", opts), "../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "index", opts), "../../")
      assert.strictEqual(path.transformLink(cur, "index.png", opts), "../../index.png")
      assert.strictEqual(path.transformLink(cur, "index#abc", opts), "../../#abc")
      assert.strictEqual(path.transformLink(cur, "tag/test", opts), "../../tag/test")
      assert.strictEqual(path.transformLink(cur, "a/b/c#test", opts), "../../a/b/c#test")
      assert.strictEqual(path.transformLink(cur, "a/test.png", opts), "../../a/test.png")
    })
    test("from a/b/index", () => {
      const cur = "a/b" as CanonicalSlug
      const cur = "a/b/index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "a/b/d", opts), "../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "a/b", opts), "../../a/b")
      assert.strictEqual(path.transformLink(cur, "index", opts), "../../")
    })
    test("from index", () => {
      const cur = "" as CanonicalSlug
      const cur = "index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "index", opts), "./")
      assert.strictEqual(path.transformLink(cur, "a/b/c", opts), "./a/b/c")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
@@ -242,20 +247,20 @@
    }
    test("from a/b/c", () => {
      const cur = "a/b/c" as CanonicalSlug
      assert.strictEqual(path.transformLink(cur, "d", opts), "../../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "h", opts), "../../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../../a/b/")
      assert.strictEqual(path.transformLink(cur, "a/b/index.png", opts), "../../../a/b/index.png")
      assert.strictEqual(path.transformLink(cur, "a/b/index#abc", opts), "../../../a/b/#abc")
      assert.strictEqual(path.transformLink(cur, "index", opts), "../../../")
      assert.strictEqual(path.transformLink(cur, "index.png", opts), "../../../index.png")
      assert.strictEqual(path.transformLink(cur, "test.png", opts), "../../../a/test.png")
      assert.strictEqual(path.transformLink(cur, "index#abc", opts), "../../../#abc")
      const cur = "a/b/c" as FullSlug
      assert.strictEqual(path.transformLink(cur, "d", opts), "../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "h", opts), "../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../a/b/")
      assert.strictEqual(path.transformLink(cur, "a/b/index.png", opts), "../../a/b/index.png")
      assert.strictEqual(path.transformLink(cur, "a/b/index#abc", opts), "../../a/b/#abc")
      assert.strictEqual(path.transformLink(cur, "index", opts), "../../")
      assert.strictEqual(path.transformLink(cur, "index.png", opts), "../../index.png")
      assert.strictEqual(path.transformLink(cur, "test.png", opts), "../../a/test.png")
      assert.strictEqual(path.transformLink(cur, "index#abc", opts), "../../#abc")
    })
    test("from a/b/index", () => {
      const cur = "a/b" as CanonicalSlug
      const cur = "a/b/index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "d", opts), "../../a/b/d")
      assert.strictEqual(path.transformLink(cur, "h", opts), "../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../a/b/")
@@ -263,7 +268,7 @@
    })
    test("from index", () => {
      const cur = "" as CanonicalSlug
      const cur = "index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "d", opts), "./a/b/d")
      assert.strictEqual(path.transformLink(cur, "h", opts), "./e/g/h")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
@@ -278,21 +283,24 @@
    }
    test("from a/b/c", () => {
      const cur = "a/b/c" as CanonicalSlug
      const cur = "a/b/c" as FullSlug
      assert.strictEqual(path.transformLink(cur, "d", opts), "./d")
      assert.strictEqual(path.transformLink(cur, "index", opts), "./")
      assert.strictEqual(path.transformLink(cur, "../../../index", opts), "../../../")
      assert.strictEqual(path.transformLink(cur, "../../../index.png", opts), "../../../index.png")
      assert.strictEqual(path.transformLink(cur, "../../../index#abc", opts), "../../../#abc")
      assert.strictEqual(path.transformLink(cur, "../../../", opts), "../../../")
      assert.strictEqual(path.transformLink(cur, "../../../a/test.png", opts), "../../../a/test.png")
      assert.strictEqual(
        path.transformLink(cur, "../../../a/test.png", opts),
        "../../../a/test.png",
      )
      assert.strictEqual(path.transformLink(cur, "../../../e/g/h", opts), "../../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "../../../e/g/h", opts), "../../../e/g/h")
      assert.strictEqual(path.transformLink(cur, "../../../e/g/h#abc", opts), "../../../e/g/h#abc")
    })
    test("from a/b/index", () => {
      const cur = "a/b" as CanonicalSlug
      const cur = "a/b/index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "../../index", opts), "../../")
      assert.strictEqual(path.transformLink(cur, "../../", opts), "../../")
      assert.strictEqual(path.transformLink(cur, "../../e/g/h", opts), "../../e/g/h")
@@ -300,9 +308,56 @@
    })
    test("from index", () => {
      const cur = "" as CanonicalSlug
      const cur = "index" as FullSlug
      assert.strictEqual(path.transformLink(cur, "e/g/h", opts), "./e/g/h")
      assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
    })
  })
})
describe("resolveRelative", () => {
  test("from index", () => {
    assert.strictEqual(path.resolveRelative("index" as FullSlug, "index" as FullSlug), "./")
    assert.strictEqual(path.resolveRelative("index" as FullSlug, "abc" as FullSlug), "./abc")
    assert.strictEqual(
      path.resolveRelative("index" as FullSlug, "abc/def" as FullSlug),
      "./abc/def",
    )
    assert.strictEqual(
      path.resolveRelative("index" as FullSlug, "abc/def/ghi" as FullSlug),
      "./abc/def/ghi",
    )
  })
  test("from nested page", () => {
    assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "index" as FullSlug), "../")
    assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "abc" as FullSlug), "../abc")
    assert.strictEqual(
      path.resolveRelative("abc/def" as FullSlug, "abc/def" as FullSlug),
      "../abc/def",
    )
    assert.strictEqual(
      path.resolveRelative("abc/def" as FullSlug, "ghi/jkl" as FullSlug),
      "../ghi/jkl",
    )
  })
  test("with index paths", () => {
    assert.strictEqual(path.resolveRelative("abc/index" as FullSlug, "index" as FullSlug), "../")
    assert.strictEqual(
      path.resolveRelative("abc/def/index" as FullSlug, "index" as FullSlug),
      "../../",
    )
    assert.strictEqual(path.resolveRelative("index" as FullSlug, "abc/index" as FullSlug), "./abc/")
    assert.strictEqual(
      path.resolveRelative("abc/def" as FullSlug, "abc/index" as FullSlug),
      "../abc/",
    )
  })
  test("with simple slugs", () => {
    assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "" as SimpleSlug), "../")
    assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "ghi" as SimpleSlug), "../ghi")
    assert.strictEqual(path.resolveRelative("abc/def" as FullSlug, "ghi/" as SimpleSlug), "../ghi/")
  })
})