Skip to content

Commit

Permalink
refactor(external_link): indexOf is faster than includes/test
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 6, 2019
1 parent 6d9ed7a commit 61dd31d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ function externalLinkFilter(data) {
: [config.external_link.exclude];

data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str) || !isExternal(href, config)) return str;
if (str.indexOf('target=') !== -1 || !isExternal(href, config)) return str;

if (/rel=/gi.test(str)) {
if (str.indexOf('rel=') !== -1) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`);
if (rel.indexOf('noopenner') === -1) relStr = relStr.replace(rel, `${rel} noopener`);
return relStr;
});
return str.replace(hrefStr, `${hrefStr} target="_blank"`);
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/filter/after_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ function externalLinkFilter(data) {
: [config.external_link.exclude];

data = data.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (/target=/gi.test(str) || !isExternal(href, config)) return str;
if (str.indexOf('target=') !== -1 || !isExternal(href, config)) return str;

if (/rel=/gi.test(str)) {
if (str.indexOf('rel=') !== -1) {
str = str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
if (!rel.includes('noopenner')) relStr = relStr.replace(rel, `${rel} noopener`);
if (rel.indexOf('noopenner') === -1) relStr = relStr.replace(rel, `${rel} noopener`);
return relStr;
});
return str.replace(hrefStr, `${hrefStr} target="_blank"`);
Expand Down

0 comments on commit 61dd31d

Please sign in to comment.