Jacky Zhao
2023-07-20 410fc9c8d37b0c4118c70678db5d2e55a842f486
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import test, { describe } from 'node:test'
import * as path from './path'
import assert from 'node:assert'
 
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"))
 
    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"))
  })
 
  test('isRelativeURL', () => {
    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"))
 
    assert(!path.isRelativeURL("abc"))
    assert(!path.isRelativeURL("/abc/def"))
    assert(!path.isRelativeURL(""))
    assert(!path.isRelativeURL("../"))
    assert(!path.isRelativeURL("./"))
    assert(!path.isRelativeURL("./abc/def.html"))
    assert(!path.isRelativeURL("./abc/def.md"))
  })
 
  test('isServerSlug', () => {
    assert(path.isServerSlug("index"))
    assert(path.isServerSlug("abc/def"))
 
    assert(!path.isServerSlug("."))
    assert(!path.isServerSlug("./abc/def"))
    assert(!path.isServerSlug("../abc/def"))
    assert(!path.isServerSlug("index.html"))
    assert(!path.isServerSlug("abc/def.html"))
    assert(!path.isServerSlug("abc/def#anchor"))
    assert(!path.isServerSlug("abc/def?query=1"))
    assert(!path.isServerSlug("note with spaces"))
  })
 
  test('isFilePath', () => {
    assert(path.isFilePath("content/index.md"))
    assert(path.isFilePath("content/test.png"))
    assert(!path.isFilePath("../test.pdf"))
    assert(!path.isFilePath("content/test"))
    assert(!path.isFilePath("./content/test"))
  })
})
 
 
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) {
    for (const [inp, expected] of pairs) {
      assert(checkPre(inp), `${inp} wasn't the expected input type`)
      const actual = transform(inp)
      assert.strictEqual(actual, expected, `after transforming ${inp}, '${actual}' was not '${expected}'`)
      assert(checkPost(actual), `${actual} wasn't the expected output type`)
    }
  }
 
  test('canonicalizeServer', () => {
    asserts([
      ["index", ""],
      ["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)
  })
 
  describe('slugifyFilePath', () => {
    asserts([
      ["content/index.md", "content/index"],
      ["content/_index.md", "content/index"],
      ["/content/index.md", "content/index"],
      ["content/cool.png", "content/cool"],
      ["index.md", "index"],
      ["note with spaces.md", "note-with-spaces"],
    ], path.slugifyFilePath, path.isFilePath, path.isServerSlug)
  })
 
  describe('transformInternalLink', () => {
    asserts([
      ["", "."],
      [".", "."],
      ["./", "."],
      ["./index", "."],
      ["./index.html", "."],
      ["./index.md", "."],
      ["content", "./content"],
      ["content/test.md", "./content/test"],
      ["./content/test.md", "./content/test"],
      ["../content/test.md", "../content/test"],
      ["tags/", "./tags"],
      ["/tags/", "./tags"],
      ["content/with spaces", "./content/with-spaces"],
      ["content/with spaces#and Anchor!", "./content/with-spaces#and-anchor"],
    ], path.transformInternalLink, (_x: string): _x is string => true, path.isRelativeURL)
  })
 
  describe('pathToRoot', () => {
    asserts([
      ["", "."],
      ["abc", ".."],
      ["abc/def", "../.."],
    ], path.pathToRoot, path.isCanonicalSlug, path.isRelativeURL)
  })
})