Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(external_link): drop cheerio and use regex #3685

Merged
merged 6 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
'use strict';

const url = require('url');
let cheerio;

function externalLinkFilter(data) {
const { config } = this;
if (!config.external_link) return;

if (!cheerio) cheerio = require('cheerio');

const $ = cheerio.load(data.content, {decodeEntities: false});
const siteHost = url.parse(config.url).hostname || config.url;

$('a').each(function() {
// Exit if the link has target attribute
if ($(this).attr('target')) return;

// Exit if the href attribute doesn't exists
const href = $(this).attr('href');
if (!href) return;
data.content = data.content.replace(/<a.*?(href="(.*?)").*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str)) return str;

const data = url.parse(href);

// Exit if the link doesn't have protocol, which means it's a internal link
if (!data.protocol) return;

// Exit if the url has same host with config.url
if (data.hostname === siteHost) return;

$(this)
.attr('target', '_blank')
.attr('rel', 'noopener');
if (!data.protocol) return str;
// Exit if the url has same host with config.ur
if (data.hostname === siteHost) return str;

if (/rel=/gi.test(str)) {
str = str.replace(/rel="(.*?)"/gi, (relStr, p1) => {
return relStr.replace(p1, `${p1} noopener`);
});
return str.replace(hrefStr, `${hrefStr} target="_blank"`);
}

return str.replace(hrefStr, `${hrefStr} target="_blank" rel="noopener"`);
});

data.content = $.html();
}

module.exports = externalLinkFilter;
28 changes: 20 additions & 8 deletions test/scripts/filters/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ describe('External link', () => {
'# External link test',
'1. External link',
'<a href="https://hexo.io/">Hexo</a>',
'2. Internal link',
'2. External link with "rel" Attribute',
'<a rel="external" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" rel="external">Hexo</a>',
'3. External link with Other Attributes',
'<a class="img" href="https://hexo.io/">Hexo</a>',
'<a href="https://hexo.io/" class="img">Hexo</a>',
'4. Internal link',
'<a href="/archives/foo.html">Link</a>',
'3. Ignore links have "target" attribute',
'5. Ignore links have "target" attribute',
'<a href="https://hexo.io/" target="_blank">Hexo</a>',
'4. Ignore links don\'t have "href" attribute',
'6. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>',
'5. Ignore links whose hostname is same as config',
'7. Ignore links whose hostname is same as config',
'<a href="https://example.com">Example Domain</a>'
].join('\n');

Expand All @@ -44,13 +50,19 @@ describe('External link', () => {
'# External link test',
'1. External link',
'<a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>',
'2. Internal link',
'2. External link with "rel" Attribute',
'<a rel="external noopener" href="https://hexo.io/" target="_blank">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="external noopener">Hexo</a>',
'3. External link with Other Attributes',
'<a class="img" href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>',
'<a href="https://hexo.io/" target="_blank" rel="noopener" class="img">Hexo</a>',
'4. Internal link',
'<a href="/archives/foo.html">Link</a>',
'3. Ignore links have "target" attribute',
'5. Ignore links have "target" attribute',
'<a href="https://hexo.io/" target="_blank">Hexo</a>',
'4. Ignore links don\'t have "href" attribute',
'6. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>',
'5. Ignore links whose hostname is same as config',
'7. Ignore links whose hostname is same as config',
'<a href="https://example.com">Example Domain</a>'
].join('\n'));
});
Expand Down