fix(tag/post_link): support url with subdir (#5419)
[hexo.git] / test / scripts / filters / external_link.js
blobdbec1dbca57f23629707a2724399347c48885cb3
1 'use strict';
3 const decache = require('decache');
5 describe('External link', () => {
6   const Hexo = require('../../../dist/hexo');
7   const hexo = new Hexo();
8   let externalLink;
10   beforeEach(() => {
11     decache('../../../dist/plugins/filter/after_render/external_link');
12     externalLink = require('../../../dist/plugins/filter/after_render/external_link').bind(hexo);
13   });
15   hexo.config = {
16     url: 'https://example.com',
17     external_link: {
18       enable: true,
19       field: 'site',
20       exclude: ''
21     }
22   };
24   it('disabled', () => {
25     const content = 'foo'
26       + '<a href="https://hexo.io/">Hexo</a>'
27       + 'bar';
29     hexo.config.external_link.enable = false;
31     should.not.exist(externalLink(content));
32     hexo.config.external_link.enable = true;
33   });
35   it('field is post', () => {
36     const content = 'foo'
37       + '<a href="https://hexo.io/">Hexo</a>'
38       + 'bar';
40     hexo.config.external_link.field = 'post';
42     should.not.exist(externalLink(content));
43     hexo.config.external_link.field = 'site';
44   });
46   it('enabled', () => {
47     const content = [
48       '# External link test',
49       '1. External link',
50       '<a href="https://hexo.io/">Hexo</a>',
51       '2. External link with "rel" Attribute',
52       '<a rel="external" href="https://hexo.io/">Hexo</a>',
53       '<a href="https://hexo.io/" rel="external">Hexo</a>',
54       '<a rel="noopenner" href="https://hexo.io/">Hexo</a>',
55       '<a href="https://hexo.io/" rel="noopenner">Hexo</a>',
56       '<a rel="external noopenner" href="https://hexo.io/">Hexo</a>',
57       '<a href="https://hexo.io/" rel="external noopenner">Hexo</a>',
58       '3. External link with Other Attributes',
59       '<a class="img" href="https://hexo.io/">Hexo</a>',
60       '<a href="https://hexo.io/" class="img">Hexo</a>',
61       '4. Internal link',
62       '<a href="/archives/foo.html">Link</a>',
63       '5. Ignore links have "target" attribute',
64       '<a href="https://hexo.io/" target="_blank">Hexo</a>',
65       '6. Ignore links don\'t have "href" attribute',
66       '<a>Anchor</a>',
67       '7. Ignore links whose hostname is same as config',
68       '<a href="https://example.com">Example Domain</a>'
69     ].join('\n');
71     const result = externalLink(content);
73     result.should.eql([
74       '# External link test',
75       '1. External link',
76       '<a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>',
77       '2. External link with "rel" Attribute',
78       '<a rel="external noopener" target="_blank" href="https://hexo.io/">Hexo</a>',
79       '<a target="_blank" href="https://hexo.io/" rel="external noopener">Hexo</a>',
80       '<a rel="noopenner" target="_blank" href="https://hexo.io/">Hexo</a>',
81       '<a target="_blank" href="https://hexo.io/" rel="noopenner">Hexo</a>',
82       '<a rel="external noopenner" target="_blank" href="https://hexo.io/">Hexo</a>',
83       '<a target="_blank" href="https://hexo.io/" rel="external noopenner">Hexo</a>',
84       '3. External link with Other Attributes',
85       '<a class="img" target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>',
86       '<a target="_blank" rel="noopener" href="https://hexo.io/" class="img">Hexo</a>',
87       '4. Internal link',
88       '<a href="/archives/foo.html">Link</a>',
89       '5. Ignore links have "target" attribute',
90       '<a href="https://hexo.io/" target="_blank">Hexo</a>',
91       '6. Ignore links don\'t have "href" attribute',
92       '<a>Anchor</a>',
93       '7. Ignore links whose hostname is same as config',
94       '<a href="https://example.com">Example Domain</a>'
95     ].join('\n'));
96   });
98   it('exclude - string', () => {
99     const content = [
100       '<a href="https://foo.com/">Hexo</a>',
101       '<a href="https://bar.com/">Hexo</a>',
102       '<a href="https://baz.com/">Hexo</a>'
103     ].join('\n');
105     hexo.config.external_link.exclude = 'foo.com';
107     const result = externalLink(content);
109     result.should.eql([
110       '<a href="https://foo.com/">Hexo</a>',
111       '<a target="_blank" rel="noopener" href="https://bar.com/">Hexo</a>',
112       '<a target="_blank" rel="noopener" href="https://baz.com/">Hexo</a>'
113     ].join('\n'));
115     hexo.config.external_link.exclude = '';
116   });
118   it('exclude - array', () => {
119     const content = [
120       '<a href="https://foo.com/">Hexo</a>',
121       '<a href="https://bar.com/">Hexo</a>',
122       '<a href="https://baz.com/">Hexo</a>'
123     ].join('\n');
125     hexo.config.external_link.exclude = ['foo.com', 'bar.com'];
127     const result = externalLink(content);
129     result.should.eql([
130       '<a href="https://foo.com/">Hexo</a>',
131       '<a href="https://bar.com/">Hexo</a>',
132       '<a target="_blank" rel="noopener" href="https://baz.com/">Hexo</a>'
133     ].join('\n'));
135     hexo.config.external_link.exclude = '';
136   });
139 describe('External link - post', () => {
140   const Hexo = require('../../../dist/hexo');
141   const hexo = new Hexo();
143   let externalLink;
145   beforeEach(() => {
146     decache('../../../dist/plugins/filter/after_post_render/external_link');
147     externalLink = require('../../../dist/plugins/filter/after_post_render/external_link').bind(hexo);
148   });
150   hexo.config = {
151     url: 'https://example.com',
152     external_link: {
153       enable: true,
154       field: 'post',
155       exclude: ''
156     }
157   };
159   it('disabled', () => {
160     const content = 'foo<a href="https://hexo.io/">Hexo</a>bar';
162     const data = {content};
163     hexo.config.external_link.enable = false;
165     externalLink(data);
166     data.content.should.eql(content);
167     hexo.config.external_link.enable = true;
168   });
170   it('field is site', () => {
171     const content = 'foo'
172       + '<a href="https://hexo.io/">Hexo</a>'
173       + 'bar';
175     const data = {content};
176     hexo.config.external_link.field = 'site';
178     externalLink(data);
179     data.content.should.eql(content);
180     hexo.config.external_link.field = 'post';
181   });
183   it('enabled', () => {
184     const content = [
185       '# External link test',
186       '1. External link',
187       '<a href="https://hexo.io/">Hexo</a>',
188       '2. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
189       '<a href="#top">Hexo</a>',
190       '<a href="mailto:hi@hexo.io">Hexo</a>',
191       '<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>',
192       '3. External link with "rel" Attribute',
193       '<a rel="external" href="https://hexo.io/">Hexo</a>',
194       '<a href="https://hexo.io/" rel="external">Hexo</a>',
195       '<a rel="noopenner" href="https://hexo.io/">Hexo</a>',
196       '<a href="https://hexo.io/" rel="noopenner">Hexo</a>',
197       '<a rel="external noopenner" href="https://hexo.io/">Hexo</a>',
198       '<a href="https://hexo.io/" rel="external noopenner">Hexo</a>',
199       '4. External link with Other Attributes',
200       '<a class="img" href="https://hexo.io/">Hexo</a>',
201       '<a href="https://hexo.io/" class="img">Hexo</a>',
202       '5. Internal link',
203       '<a href="/archives/foo.html">Link</a>',
204       '6. Ignore links have "target" attribute',
205       '<a href="https://hexo.io/" target="_blank">Hexo</a>',
206       '7. Ignore links don\'t have "href" attribute',
207       '<a>Anchor</a>',
208       '8. Ignore links whose hostname is same as config',
209       '<a href="https://example.com">Example Domain</a>'
210     ].join('\n');
212     const data = {content};
213     externalLink(data);
215     data.content.should.eql([
216       '# External link test',
217       '1. External link',
218       '<a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>',
219       '2. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
220       '<a href="#top">Hexo</a>',
221       '<a href="mailto:hi@hexo.io">Hexo</a>',
222       '<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>',
223       '3. External link with "rel" Attribute',
224       '<a rel="external noopener" target="_blank" href="https://hexo.io/">Hexo</a>',
225       '<a target="_blank" href="https://hexo.io/" rel="external noopener">Hexo</a>',
226       '<a rel="noopenner" target="_blank" href="https://hexo.io/">Hexo</a>',
227       '<a target="_blank" href="https://hexo.io/" rel="noopenner">Hexo</a>',
228       '<a rel="external noopenner" target="_blank" href="https://hexo.io/">Hexo</a>',
229       '<a target="_blank" href="https://hexo.io/" rel="external noopenner">Hexo</a>',
230       '4. External link with Other Attributes',
231       '<a class="img" target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>',
232       '<a target="_blank" rel="noopener" href="https://hexo.io/" class="img">Hexo</a>',
233       '5. Internal link',
234       '<a href="/archives/foo.html">Link</a>',
235       '6. Ignore links have "target" attribute',
236       '<a href="https://hexo.io/" target="_blank">Hexo</a>',
237       '7. Ignore links don\'t have "href" attribute',
238       '<a>Anchor</a>',
239       '8. Ignore links whose hostname is same as config',
240       '<a href="https://example.com">Example Domain</a>'
241     ].join('\n'));
242   });
245   it('backward compatibility', () => {
246     const content = 'foo'
247       + '<a href="https://hexo.io/">Hexo</a>'
248       + 'bar';
250     const data = {content};
251     hexo.config.external_link = false;
253     externalLink(data);
254     data.content.should.eql(content);
256     hexo.config.external_link = {
257       enable: true,
258       field: 'post',
259       exclude: ''
260     };
261   });
263   it('exclude - string', () => {
264     const content = [
265       '<a href="https://foo.com/">Hexo</a>',
266       '<a href="https://bar.com/">Hexo</a>',
267       '<a href="https://baz.com/">Hexo</a>'
268     ].join('\n');
270     hexo.config.external_link.exclude = 'foo.com';
272     const data = {content};
273     externalLink(data);
275     data.content.should.eql([
276       '<a href="https://foo.com/">Hexo</a>',
277       '<a target="_blank" rel="noopener" href="https://bar.com/">Hexo</a>',
278       '<a target="_blank" rel="noopener" href="https://baz.com/">Hexo</a>'
279     ].join('\n'));
281     hexo.config.external_link.exclude = '';
282   });
284   it('exclude - array', () => {
285     const content = [
286       '<a href="https://foo.com/">Hexo</a>',
287       '<a href="https://bar.com/">Hexo</a>',
288       '<a href="https://baz.com/">Hexo</a>'
289     ].join('\n');
291     hexo.config.external_link.exclude = ['foo.com', 'bar.com'];
293     const data = {content};
294     externalLink(data);
296     data.content.should.eql([
297       '<a href="https://foo.com/">Hexo</a>',
298       '<a href="https://bar.com/">Hexo</a>',
299       '<a target="_blank" rel="noopener" href="https://baz.com/">Hexo</a>'
300     ].join('\n'));
302     hexo.config.external_link.exclude = '';
303   });