-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
210 lines (196 loc) · 5.5 KB
/
index.test.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import parse5 from 'parse5'
import fromParse5 from 'hast-util-from-parse5'
import plugin, { Options } from './index'
import hast from 'hast'
import toHtml from 'hast-util-to-html'
import gatsby from 'gatsby'
describe('plugin', () => {
test('replace HTTP URLs to HTTPS using URL object', () => {
new Plugin()
.config({
resolve: '@suin/gatsby-transformer-replace-urls',
options: {
replace({ url }) {
const u = new URL(url)
if (u.protocol === 'http:') {
u.protocol = 'https'
}
return u
},
},
})
.transforms(`<a href="http://example.com/"></a>`)
.becomes(`<a href="https://example.com/"></a>`)
})
test('replace only img tags', () => {
new Plugin()
.config({
resolve: '@suin/gatsby-transformer-replace-urls',
options: {
replace({ url, attribute, element }) {
if (element.tagName === 'img' && attribute === 'src') {
const u = new URL(url)
u.host = 'cdn.example.com'
return u
}
return
},
},
})
.transforms(
`<a href="https://example.com/">
<img src="https://example.com/logo.png">
</a>`,
)
.becomes(
`<a href="https://example.com/">
<img src="https://cdn.example.com/logo.png">
</a>`,
)
})
test('replace hash only URL', () => {
new Plugin()
.config({
resolve: '@suin/gatsby-transformer-replace-urls',
options: {
replace({ url }) {
// In this case, you have to compare the url as a string since
// `new URL(url)` throws Error like:
// "TypeError [ERR_INVALID_URL]: Invalid URL: #hash"
if (url === '#hash') {
return '#new-hash'
}
return
},
},
})
.transforms(`<a href="#hash"></a>`)
.becomes(`<a href="#new-hash"></a>`)
})
test('replace URLs in a specific node type', () => {
new Plugin()
.config({
resolve: '@suin/gatsby-transformer-replace-urls',
options: {
replace({ url, parent }) {
if (parent.internal.type === 'File') {
return
} else {
// internal.type is ContentfulBlogPost
const u = new URL(url)
if (u.protocol === 'http:') {
u.protocol = 'https'
}
return u
}
},
},
})
.transforms([
{
parentNodeInternalType: 'ContentfulBlogPost',
html: `<a href="http://example.com/"></a>`,
},
{
parentNodeInternalType: 'File',
html: `<a href="http://example.com/"></a>`,
},
])
.becomes([
{
parentNodeInternalType: 'ContentfulBlogPost',
html: `<a href="https://example.com/"></a>`,
},
{
parentNodeInternalType: 'File',
html: `<a href="http://example.com/"></a>`,
},
])
})
})
class Plugin {
private options?: Options
private html: string = ''
private nodes: readonly NodeExample[] = []
config({ options }: PluginConfig): this {
this.options = options
return this
}
transforms(htmlOrNodes: string | readonly NodeExample[]): this {
if (typeof htmlOrNodes === 'string') {
this.html = htmlOrNodes
} else {
this.nodes = htmlOrNodes
}
return this
}
becomes(htmlOrNodes: string | readonly NodeExample[]): this {
if (typeof htmlOrNodes === 'string') {
this.becomesHtml(htmlOrNodes)
} else {
this.becomesNodes(htmlOrNodes)
}
return this
}
private becomesHtml(html: string): void {
const inAst = toHast(this.html)
const outAst = runPlugin({ htmlAst: inAst }, this.options)
const actual = toHtml(outAst)
expect(actual).toBe(html)
}
private becomesNodes(expectedNodes: readonly NodeExample[]): void {
const actualNodes = this.nodes.map(node => {
const inAst = toHast(node.html)
const outAst = runPlugin(
{
htmlAst: inAst,
htmlNode: {
id: '',
parent: '__parent',
internal: {
type: 'HtmlRehype',
contentDigest: '',
owner: 'gatsby-transformer-rehype',
},
children: [],
},
getNode(id: string): gatsby.NodeInput | undefined {
if (id === '__parent') {
return {
id: '__parent',
internal: {
type: node.parentNodeInternalType,
contentDigest: '',
},
children: [],
}
}
return
},
},
this.options,
)
const outHtml = toHtml(outAst)
return { ...node, html: outHtml }
})
expect(actualNodes).toEqual(expectedNodes)
}
}
type NodeExample = {
readonly parentNodeInternalType: string
readonly html: string
}
const runPlugin = (
{
htmlAst = {} as hast.Root,
htmlNode = {} as gatsby.Node,
getNode = () => {},
}: Partial<Parameters<typeof plugin>[0]>,
{ replace, plugins = [] }: Partial<Parameters<typeof plugin>[1]> = {},
) => plugin({ htmlAst, htmlNode, getNode }, { replace, plugins })
const toHast = (html: string) =>
fromParse5(parse5.parseFragment(html)) as hast.Root
type PluginConfig = {
resolve: '@suin/gatsby-transformer-replace-urls'
options?: Options
}