3 const decache = require('decache');
5 describe('External link', () => {
6 const Hexo = require('../../../dist/hexo');
7 const hexo = new Hexo();
11 decache('../../../dist/plugins/filter/after_render/external_link');
12 externalLink = require('../../../dist/plugins/filter/after_render/external_link').bind(hexo);
16 url: 'https://example.com',
24 it('disabled', () => {
26 + '<a href="https://hexo.io/">Hexo</a>'
29 hexo.config.external_link.enable = false;
31 should.not.exist(externalLink(content));
32 hexo.config.external_link.enable = true;
35 it('field is post', () => {
37 + '<a href="https://hexo.io/">Hexo</a>'
40 hexo.config.external_link.field = 'post';
42 should.not.exist(externalLink(content));
43 hexo.config.external_link.field = 'site';
48 '# External link test',
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>',
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',
67 '7. Ignore links whose hostname is same as config',
68 '<a href="https://example.com">Example Domain</a>'
71 const result = externalLink(content);
74 '# External link test',
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>',
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',
93 '7. Ignore links whose hostname is same as config',
94 '<a href="https://example.com">Example Domain</a>'
98 it('exclude - string', () => {
100 '<a href="https://foo.com/">Hexo</a>',
101 '<a href="https://bar.com/">Hexo</a>',
102 '<a href="https://baz.com/">Hexo</a>'
105 hexo.config.external_link.exclude = 'foo.com';
107 const result = externalLink(content);
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>'
115 hexo.config.external_link.exclude = '';
118 it('exclude - array', () => {
120 '<a href="https://foo.com/">Hexo</a>',
121 '<a href="https://bar.com/">Hexo</a>',
122 '<a href="https://baz.com/">Hexo</a>'
125 hexo.config.external_link.exclude = ['foo.com', 'bar.com'];
127 const result = externalLink(content);
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>'
135 hexo.config.external_link.exclude = '';
139 describe('External link - post', () => {
140 const Hexo = require('../../../dist/hexo');
141 const hexo = new Hexo();
146 decache('../../../dist/plugins/filter/after_post_render/external_link');
147 externalLink = require('../../../dist/plugins/filter/after_post_render/external_link').bind(hexo);
151 url: 'https://example.com',
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;
166 data.content.should.eql(content);
167 hexo.config.external_link.enable = true;
170 it('field is site', () => {
171 const content = 'foo'
172 + '<a href="https://hexo.io/">Hexo</a>'
175 const data = {content};
176 hexo.config.external_link.field = 'site';
179 data.content.should.eql(content);
180 hexo.config.external_link.field = 'post';
183 it('enabled', () => {
185 '# External link test',
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>',
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',
208 '8. Ignore links whose hostname is same as config',
209 '<a href="https://example.com">Example Domain</a>'
212 const data = {content};
215 data.content.should.eql([
216 '# External link test',
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>',
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',
239 '8. Ignore links whose hostname is same as config',
240 '<a href="https://example.com">Example Domain</a>'
245 it('backward compatibility', () => {
246 const content = 'foo'
247 + '<a href="https://hexo.io/">Hexo</a>'
250 const data = {content};
251 hexo.config.external_link = false;
254 data.content.should.eql(content);
256 hexo.config.external_link = {
263 it('exclude - string', () => {
265 '<a href="https://foo.com/">Hexo</a>',
266 '<a href="https://bar.com/">Hexo</a>',
267 '<a href="https://baz.com/">Hexo</a>'
270 hexo.config.external_link.exclude = 'foo.com';
272 const data = {content};
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>'
281 hexo.config.external_link.exclude = '';
284 it('exclude - array', () => {
286 '<a href="https://foo.com/">Hexo</a>',
287 '<a href="https://bar.com/">Hexo</a>',
288 '<a href="https://baz.com/">Hexo</a>'
291 hexo.config.external_link.exclude = ['foo.com', 'bar.com'];
293 const data = {content};
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>'
302 hexo.config.external_link.exclude = '';