Skip to content

Commit f68e6b5

Browse files
authored
Merge pull request #21 from lukaszmn/master
`foo.bar` added if url starts with `/`
2 parents 5701724 + 29b0a7f commit f68e6b5

2 files changed

Lines changed: 76 additions & 47 deletions

File tree

src/index.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,60 @@ import queryString from 'query-string';
44
import normalizeUrl from 'normalize-url';
55

66
const setNanoid = url => {
7-
const id = nanoid();
8-
const fullUrl = /^[/?]/.test(url) ? `foo.bar${url}` : url;
7+
const id = nanoid();
8+
const fullUrl = /^[/?]/.test(url) ? `foo.bar${url}` : url;
99

10-
if (!isUrl(normalizeUrl(fullUrl))) {
11-
return url;
12-
}
10+
if (!isUrl(normalizeUrl(fullUrl))) {
11+
return url;
12+
}
1313

14-
let [uri, query] = fullUrl.split('?');
15-
query = queryString.parse(query);
16-
query.v = query.v ? query.v : id;
17-
query = queryString.stringify(query);
14+
let [uri, query] = url.split('?');
15+
query = queryString.parse(query);
16+
query.v = query.v ? query.v : id;
17+
query = queryString.stringify(query);
1818

19-
return `${uri}?${query}`;
19+
return `${uri}?${query}`;
2020
};
2121

2222
export default (options = {}) => {
23-
return tree => new Promise((resolve, reject) => {
24-
let tags = ['link', 'script'];
25-
let attributes = ['href', 'src'];
26-
27-
if (options.tags && Array.isArray(options.tags)) {
28-
tags = [...new Set([...tags, ...options.tags])];
29-
}
23+
return tree => new Promise((resolve, reject) => {
24+
let tags = ['link', 'script'];
25+
let attributes = ['href', 'src'];
3026

31-
if (options.exclude) {
32-
tags = tags.filter(tag => !options.exclude.includes(tag));
33-
}
27+
if (options.tags && Array.isArray(options.tags)) {
28+
tags = [...new Set([...tags, ...options.tags])];
29+
}
3430

35-
if (options.attributes && Array.isArray(options.attributes)) {
36-
attributes = [...new Set([...attributes, ...options.attributes])];
37-
}
31+
if (options.exclude) {
32+
tags = tags.filter(tag => !options.exclude.includes(tag));
33+
}
3834

39-
if (!Array.isArray(tree)) {
40-
reject(new Error(`tree is not Array`));
41-
}
35+
if (options.attributes && Array.isArray(options.attributes)) {
36+
attributes = [...new Set([...attributes, ...options.attributes])];
37+
}
4238

43-
if (tree.length === 0) {
44-
resolve(tree);
45-
}
39+
if (!Array.isArray(tree)) {
40+
reject(new Error('tree is not Array'));
41+
}
4642

47-
tree.walk(node => {
48-
if (node.tag && node.attrs) {
49-
node.attrs = Object.keys(node.attrs).reduce((attributeList, attr) => {
50-
if (tags.includes(node.tag) && attributes.includes(attr)) {
51-
return Object.assign(attributeList, {[attr]: setNanoid(node.attrs[attr])});
52-
}
43+
if (tree.length === 0) {
44+
resolve(tree);
45+
}
5346

54-
return attributeList;
55-
}, node.attrs);
56-
}
47+
tree.walk(node => {
48+
if (node.tag && node.attrs) {
49+
node.attrs = Object.keys(node.attrs).reduce((attributeList, attr) => {
50+
if (tags.includes(node.tag) && attributes.includes(attr)) {
51+
return Object.assign(attributeList, {[attr]: setNanoid(node.attrs[attr])});
52+
}
5753

58-
return node;
59-
});
54+
return attributeList;
55+
}, node.attrs);
56+
}
6057

61-
resolve(tree);
58+
return node;
6259
});
60+
61+
resolve(tree);
62+
});
6363
};

test/test-plugin.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,30 @@ test('should return promise', t => {
2828
test('should add nanoid to style links', async t => {
2929
const input = '<link rel="stylesheet" href="style.css">';
3030
const html = (await processing(input)).html;
31-
const id = queryString.parse(parser(html)[0].attrs.href.split('?')[1]).v;
31+
32+
const {url, id} = getParts(html, 'href');
3233
t.truthy(id);
34+
t.is(url, 'style.css');
3335
t.is(id.length, 21);
3436
});
3537

3638
test('should add nanoid to script links', async t => {
3739
const input = '<script src="script.js"></script>';
3840
const html = (await processing(input)).html;
39-
const id = queryString.parse(parser(html)[0].attrs.src.split('?')[1]).v;
41+
42+
const {url, id} = getParts(html, 'src');
4043
t.truthy(id);
44+
t.is(url, 'script.js');
4145
t.is(id.length, 21);
4246
});
4347

4448
test('should add nanoid to iframe links', async t => {
4549
const input = '<iframe src="index.html"></iframe>';
4650
const html = (await processing(input, {tags: ['iframe']})).html;
47-
const id = queryString.parse(parser(html)[0].attrs.src.split('?')[1]).v;
51+
52+
const {url, id} = getParts(html, 'src');
4853
t.truthy(id);
54+
t.is(url, 'index.html');
4955
t.is(id.length, 21);
5056
});
5157

@@ -64,11 +70,12 @@ test('should not remove other attributes', async t => {
6470
const input = '<link rel="stylesheet" href="style.css">';
6571
const html = (await processing(input)).html;
6672

67-
const id = queryString.parse(parser(html)[0].attrs.href.split('?')[1]).v;
73+
const {url, id} = getParts(html, 'href');
6874
const rel = parser(html)[0].attrs.rel;
6975
t.truthy(id);
7076
t.truthy(rel);
7177
t.is(rel, 'stylesheet');
78+
t.is(url, 'style.css');
7279
t.is(id.length, 21);
7380
});
7481

@@ -77,11 +84,12 @@ test('should not add nano id', async t => {
7784
const input = `<link rel="stylesheet" href="style.css?v=${staticID}">`;
7885
const html = (await processing(input)).html;
7986

80-
const id = queryString.parse(parser(html)[0].attrs.href.split('?')[1]).v;
87+
const {url, id} = getParts(html, 'href');
8188
const rel = parser(html)[0].attrs.rel;
8289
t.truthy(id);
8390
t.truthy(rel);
8491
t.is(rel, 'stylesheet');
92+
t.is(url, 'style.css');
8593
t.is(id, staticID);
8694
t.is(id.length, 21);
8795
});
@@ -91,11 +99,12 @@ test('should add nano id for relative path', async t => {
9199
const input = `<link rel="stylesheet" href="/?v=${staticID}">`;
92100
const html = (await processing(input)).html;
93101

94-
const id = queryString.parse(parser(html)[0].attrs.href.split('?')[1]).v;
102+
const {url, id} = getParts(html, 'href');
95103
const rel = parser(html)[0].attrs.rel;
96104
t.truthy(id);
97105
t.truthy(rel);
98106
t.is(rel, 'stylesheet');
107+
t.is(url, '/');
99108
t.is(id, staticID);
100109
t.is(id.length, 21);
101110
});
@@ -108,3 +117,23 @@ test('should not add nano id for not url', async t => {
108117
t.truthy(href);
109118
t.is(href, 'sadsadsadsda');
110119
});
120+
121+
test('should add nano id for root path', async t => {
122+
const input = '<link rel="stylesheet" href="/style.css">';
123+
const html = (await processing(input)).html;
124+
125+
const {url, id} = getParts(html, 'href');
126+
const rel = parser(html)[0].attrs.rel;
127+
t.truthy(id);
128+
t.truthy(rel);
129+
t.is(rel, 'stylesheet');
130+
t.is(url, '/style.css');
131+
t.is(id.length, 21);
132+
});
133+
134+
function getParts(html, attributeName) {
135+
const parts = parser(html)[0].attrs[attributeName].split('?');
136+
const url = parts[0];
137+
const id = queryString.parse(parts[1]).v;
138+
return {url, id};
139+
}

0 commit comments

Comments
 (0)