/
index.test.ts
191 lines (183 loc) · 6.7 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
import rehypeExtendedLinks from "../lib/index.js";
import type { Options } from "../lib/index.ts";
import { assert } from "chai";
import { rehype } from "rehype";
describe("Package", () => {
it("should expose the public api", () => {
assert.isFunction(rehypeExtendedLinks);
});
});
describe("Link type check", () => {
// input: <a href="http://example.com">?</a>
// expected: <a href="http://example.com" rel="nofollow">?</a>
it("should not change a relative link", async () => {
const input = `<a href="./example.html">relative</a>`;
const result = await process(input, {});
const output = `<a href="./example.html">relative</a>`;
assert.equal(result, output);
});
it("should not change a fragment link", async () => {
const input = '<a href="#example">fragment</a>';
const expected = '<a href="#example">fragment</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
it("should not change a search link", async () => {
const input = `<a href="?search">search</a>`;
const expected = '<a href="?search">search</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
it("should not change a mailto link", async () => {
const input = '<a href="mailto:a@b.com">mailto</a>';
const expected = '<a href="mailto:a@b.com">mailto</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
// input: <a href="//example.com">?</a>
// expected: <a href="//example.com" rel="nofollow">?</a>
it("should change a protocol-relative link", async () => {
const input = `<a href="//example.com">?</a>`;
const result = await process(input, {});
const output = `<a href="//example.com" rel="nofollow">?</a>`;
assert.equal(result, output);
});
it("should change a http link", async () => {
const input = '<a href="http://example.com">http</a>';
const expected = '<a href="http://example.com" rel="nofollow">http</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
it("should change a https link", async () => {
const input = '<a href="https://example.com">https</a>';
const expected = '<a href="https://example.com" rel="nofollow">https</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
it("should not change a www link", async () => {
const input = '<a href="www.example.com">www</a>';
const expected = '<a href="www.example.com">www</a>';
const result = await process(input, {});
assert.equal(result, expected);
});
});
describe("Options check", () => {
it("should wrap in span because content exist", async () => {
const input = `<a href="//example.com">?</a>`;
const options: Options = {
content: {
type: "element",
tagName: "span",
properties: {},
children: [],
},
};
const result = await process(input, options);
const output = `<a href="//example.com" rel="nofollow">?<span></span></a>`;
assert.equal(result, output);
});
it("should add preContent", async () => {
const input = `<a href="//example.com">?</a>`;
const options: Options = {
preContent: {
type: "element",
tagName: "span",
properties: { className: ["content"] },
children: [{ type: "text", value: "content" }],
},
};
const result = await process(input, options);
const output = `<a href="//example.com" rel="nofollow"><span class="content">content</span>?</a>`;
assert.equal(result, output);
});
it("should wrap in span if wrappedProperties contains", async () => {
const input = `<a href="//example.com">?</a>`;
const options: Options = {
wrappedProperties: {
className: ["link desc"],
},
preContent: {
type: "element",
tagName: "span",
properties: {},
children: [{ type: "text", value: "pre content" }],
},
};
const result = await process(input, options);
const output = `<a href="//example.com" rel="nofollow"><span>pre content</span><span class="link desc">?</span></a>`;
assert.equal(result, output);
});
});
describe("Dynamically generated tests", () => {
const dynPreContentOptions: Options = {
preContent(node) {
const url = node.properties.href;
if (!url) return undefined;
const regex = /^(https?:\/\/)?(www\.)?github\.com\/.*/i;
if (!regex.test(url as string)) return undefined;
return {
type: "element",
tagName: "span",
properties: {},
children: [
{
type: "element",
tagName: "svg",
properties: {},
children: [
{
type: "element",
tagName: "use",
properties: {
href: "#mdi--github",
},
children: [],
},
],
},
],
};
},
};
it("should add preContent to a link that matches the test function", async () => {
const input = `<a href="https://github.com/abc">?</a>`;
const result = await process(input, dynPreContentOptions);
const output = `<a href="https://github.com/abc" rel="nofollow"><span><svg><use href="#mdi--github"></use></svg></span>?</a>`;
assert.equal(result, output);
});
it("should not add preContent to a link that matches the test function", async () => {
const input = `<a href="https://example.com">?</a>`;
const result = await process(input, dynPreContentOptions);
const output = `<a href="https://example.com" rel="nofollow">?</a>`;
assert.equal(result, output);
});
it("should add rel to a link that matches the test function", async () => {
const input = '<a href="http://example.com">http</a>';
const options: Options = {
test: (node) => {
return node.properties.href === "http://example.com";
},
};
const output = '<a href="http://example.com" rel="nofollow">http</a>';
const result = await process(input, options);
assert.equal(result, output);
});
it("should not add rel to a link that does not match the test function", async () => {
const input = '<a href="http://example.com">http</a>';
const options: Options = {
test: (node) => {
return node.properties.href === "http://foobar.com";
},
};
const output = '<a href="http://example.com">http</a>';
const result = await process(input, options);
assert.equal(result, output);
});
});
async function process(input: string, options: Options): Promise<string> {
const result = await rehype()
.use({ settings: { fragment: true } })
.use(rehypeExtendedLinks as any, options)
.process(input);
return result.toString();
}