2023-07-23 07:27:41 +07:00
|
|
|
import test, { describe } from "node:test"
|
|
|
|
import * as path from "./path"
|
|
|
|
import assert from "node:assert"
|
2023-08-20 05:52:25 +07:00
|
|
|
import { FullSlug, TransformOptions } from "./path"
|
2023-07-13 14:19:35 +07:00
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
describe("typeguards", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
test("isSimpleSlug", () => {
|
|
|
|
assert(path.isSimpleSlug(""))
|
|
|
|
assert(path.isSimpleSlug("abc"))
|
|
|
|
assert(path.isSimpleSlug("abc/"))
|
|
|
|
assert(path.isSimpleSlug("notindex"))
|
|
|
|
assert(path.isSimpleSlug("notindex/def"))
|
2023-07-13 14:19:35 +07:00
|
|
|
|
2023-08-20 05:52:25 +07:00
|
|
|
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"))
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
test("isRelativeURL", () => {
|
2023-07-13 14:19:35 +07:00
|
|
|
assert(path.isRelativeURL("."))
|
|
|
|
assert(path.isRelativeURL(".."))
|
|
|
|
assert(path.isRelativeURL("./abc/def"))
|
|
|
|
assert(path.isRelativeURL("./abc/def#an-anchor"))
|
|
|
|
assert(path.isRelativeURL("./abc/def?query=1#an-anchor"))
|
|
|
|
assert(path.isRelativeURL("../abc/def"))
|
2023-08-17 14:55:28 +07:00
|
|
|
assert(path.isRelativeURL("./abc/def.pdf"))
|
2023-07-13 14:19:35 +07:00
|
|
|
|
|
|
|
assert(!path.isRelativeURL("abc"))
|
2023-07-16 13:02:12 +07:00
|
|
|
assert(!path.isRelativeURL("/abc/def"))
|
2023-07-13 14:19:35 +07:00
|
|
|
assert(!path.isRelativeURL(""))
|
|
|
|
assert(!path.isRelativeURL("./abc/def.html"))
|
|
|
|
assert(!path.isRelativeURL("./abc/def.md"))
|
|
|
|
})
|
|
|
|
|
2023-08-20 05:52:25 +07:00
|
|
|
test("isFullSlug", () => {
|
|
|
|
assert(path.isFullSlug("index"))
|
|
|
|
assert(path.isFullSlug("abc/def"))
|
|
|
|
assert(path.isFullSlug("html.energy"))
|
|
|
|
assert(path.isFullSlug("test.pdf"))
|
2023-07-13 14:19:35 +07:00
|
|
|
|
2023-08-20 05:52:25 +07:00
|
|
|
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"))
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
test("isFilePath", () => {
|
2023-07-16 13:02:12 +07:00
|
|
|
assert(path.isFilePath("content/index.md"))
|
|
|
|
assert(path.isFilePath("content/test.png"))
|
2023-07-13 14:19:35 +07:00
|
|
|
assert(!path.isFilePath("../test.pdf"))
|
|
|
|
assert(!path.isFilePath("content/test"))
|
|
|
|
assert(!path.isFilePath("./content/test"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-07-23 07:27:41 +07:00
|
|
|
describe("transforms", () => {
|
|
|
|
function asserts<Inp, Out>(
|
|
|
|
pairs: [string, string][],
|
|
|
|
transform: (inp: Inp) => Out,
|
|
|
|
checkPre: (x: any) => x is Inp,
|
|
|
|
checkPost: (x: any) => x is Out,
|
|
|
|
) {
|
2023-07-13 14:19:35 +07:00
|
|
|
for (const [inp, expected] of pairs) {
|
|
|
|
assert(checkPre(inp), `${inp} wasn't the expected input type`)
|
|
|
|
const actual = transform(inp)
|
2023-07-23 07:27:41 +07:00
|
|
|
assert.strictEqual(
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
`after transforming ${inp}, '${actual}' was not '${expected}'`,
|
|
|
|
)
|
2023-07-13 14:19:35 +07:00
|
|
|
assert(checkPost(actual), `${actual} wasn't the expected output type`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 05:52:25 +07:00
|
|
|
test("simplifySlug", () => {
|
2023-07-23 07:27:41 +07:00
|
|
|
asserts(
|
|
|
|
[
|
2023-12-03 07:52:44 +07:00
|
|
|
["index", "/"],
|
2023-08-20 05:52:25 +07:00
|
|
|
["abc", "abc"],
|
|
|
|
["abc/index", "abc/"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["abc/def", "abc/def"],
|
|
|
|
],
|
2023-08-20 05:52:25 +07:00
|
|
|
path.simplifySlug,
|
|
|
|
path.isFullSlug,
|
|
|
|
path.isSimpleSlug,
|
2023-07-23 07:27:41 +07:00
|
|
|
)
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
|
2023-08-13 11:16:34 +07:00
|
|
|
test("slugifyFilePath", () => {
|
2023-07-23 07:27:41 +07:00
|
|
|
asserts(
|
|
|
|
[
|
|
|
|
["content/index.md", "content/index"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["content/index.html", "content/index"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["content/_index.md", "content/index"],
|
|
|
|
["/content/index.md", "content/index"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["content/cool.png", "content/cool.png"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["index.md", "index"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["test.mp4", "test.mp4"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["note with spaces.md", "note-with-spaces"],
|
2024-01-14 00:27:00 +07:00
|
|
|
["notes.with.dots.md", "notes.with.dots"],
|
2024-01-14 00:22:27 +07:00
|
|
|
["test/special chars?.md", "test/special-chars-q"],
|
|
|
|
["test/special chars #3.md", "test/special-chars-3"],
|
2023-07-23 07:27:41 +07:00
|
|
|
],
|
|
|
|
path.slugifyFilePath,
|
|
|
|
path.isFilePath,
|
2023-08-20 05:52:25 +07:00
|
|
|
path.isFullSlug,
|
2023-07-23 07:27:41 +07:00
|
|
|
)
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
|
2023-08-13 11:16:34 +07:00
|
|
|
test("transformInternalLink", () => {
|
2023-07-23 07:27:41 +07:00
|
|
|
asserts(
|
|
|
|
[
|
|
|
|
["", "."],
|
|
|
|
[".", "."],
|
2023-08-17 12:04:15 +07:00
|
|
|
["./", "./"],
|
|
|
|
["./index", "./"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["./index#abc", "./#abc"],
|
2023-08-17 12:04:15 +07:00
|
|
|
["./index.html", "./"],
|
|
|
|
["./index.md", "./"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["./index.css", "./index.css"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["content", "./content"],
|
|
|
|
["content/test.md", "./content/test"],
|
2023-08-17 14:55:28 +07:00
|
|
|
["content/test.pdf", "./content/test.pdf"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["./content/test.md", "./content/test"],
|
|
|
|
["../content/test.md", "../content/test"],
|
2023-08-17 12:04:15 +07:00
|
|
|
["tags/", "./tags/"],
|
|
|
|
["/tags/", "./tags/"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["content/with spaces", "./content/with-spaces"],
|
2023-08-17 12:04:15 +07:00
|
|
|
["content/with spaces/index", "./content/with-spaces/"],
|
2023-07-23 07:27:41 +07:00
|
|
|
["content/with spaces#and Anchor!", "./content/with-spaces#and-anchor"],
|
|
|
|
],
|
|
|
|
path.transformInternalLink,
|
|
|
|
(_x: string): _x is string => true,
|
|
|
|
path.isRelativeURL,
|
|
|
|
)
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
|
2023-08-13 11:16:34 +07:00
|
|
|
test("pathToRoot", () => {
|
2023-07-23 07:27:41 +07:00
|
|
|
asserts(
|
|
|
|
[
|
2023-08-20 05:52:25 +07:00
|
|
|
["index", "."],
|
|
|
|
["abc", "."],
|
|
|
|
["abc/def", ".."],
|
|
|
|
["abc/def/ghi", "../.."],
|
|
|
|
["abc/def/index", "../.."],
|
2023-07-23 07:27:41 +07:00
|
|
|
],
|
|
|
|
path.pathToRoot,
|
2023-08-20 05:52:25 +07:00
|
|
|
path.isFullSlug,
|
2023-07-23 07:27:41 +07:00
|
|
|
path.isRelativeURL,
|
|
|
|
)
|
2023-07-13 14:19:35 +07:00
|
|
|
})
|
|
|
|
})
|
2023-08-13 11:16:34 +07:00
|
|
|
|
|
|
|
describe("link strategies", () => {
|
2023-08-17 14:55:52 +07:00
|
|
|
const allSlugs = [
|
|
|
|
"a/b/c",
|
|
|
|
"a/b/d",
|
|
|
|
"a/b/index",
|
|
|
|
"e/f",
|
|
|
|
"e/g/h",
|
|
|
|
"index",
|
|
|
|
"a/test.png",
|
2023-08-20 05:52:25 +07:00
|
|
|
] as FullSlug[]
|
2023-08-13 11:16:34 +07:00
|
|
|
|
|
|
|
describe("absolute", () => {
|
|
|
|
const opts: TransformOptions = {
|
|
|
|
strategy: "absolute",
|
|
|
|
allSlugs,
|
|
|
|
}
|
|
|
|
|
|
|
|
test("from a/b/c", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
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")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
test("from a/b/index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "a/b/index" as FullSlug
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/d", opts), "../../a/b/d")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b", opts), "../../a/b")
|
2023-08-17 14:55:28 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "index", opts), "../../")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
test("from index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "index" as FullSlug
|
2023-08-17 14:55:28 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "index", opts), "./")
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/c", opts), "./a/b/c")
|
2023-08-17 14:55:28 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("shortest", () => {
|
|
|
|
const opts: TransformOptions = {
|
|
|
|
strategy: "shortest",
|
|
|
|
allSlugs,
|
|
|
|
}
|
|
|
|
|
|
|
|
test("from a/b/c", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
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")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
test("from a/b/index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "a/b/index" as FullSlug
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "d", opts), "../../a/b/d")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "h", opts), "../../e/g/h")
|
2023-08-17 14:55:28 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "../../a/b/")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "index", opts), "../../")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
test("from index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "index" as FullSlug
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "d", opts), "./a/b/d")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "h", opts), "./e/g/h")
|
2023-08-17 14:55:28 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "index", opts), "./")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("relative", () => {
|
|
|
|
const opts: TransformOptions = {
|
|
|
|
strategy: "relative",
|
|
|
|
allSlugs,
|
|
|
|
}
|
|
|
|
|
|
|
|
test("from a/b/c", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "a/b/c" as FullSlug
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "d", opts), "./d")
|
2023-08-17 12:04:15 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "index", opts), "./")
|
2023-08-17 14:55:28 +07:00
|
|
|
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), "../../../")
|
2023-08-17 14:55:52 +07:00
|
|
|
assert.strictEqual(
|
|
|
|
path.transformLink(cur, "../../../a/test.png", opts),
|
|
|
|
"../../../a/test.png",
|
|
|
|
)
|
2023-08-17 14:55:28 +07:00
|
|
|
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")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
test("from a/b/index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "a/b/index" as FullSlug
|
2023-08-17 12:04:15 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "../../index", opts), "../../")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "../../", opts), "../../")
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "../../e/g/h", opts), "../../e/g/h")
|
|
|
|
assert.strictEqual(path.transformLink(cur, "c", opts), "./c")
|
|
|
|
})
|
|
|
|
|
|
|
|
test("from index", () => {
|
2023-08-20 05:52:25 +07:00
|
|
|
const cur = "index" as FullSlug
|
2023-08-13 11:16:34 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "e/g/h", opts), "./e/g/h")
|
2023-08-17 12:04:15 +07:00
|
|
|
assert.strictEqual(path.transformLink(cur, "a/b/index", opts), "./a/b/")
|
2023-08-13 11:16:34 +07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|