Skip to content

Commit

Permalink
refactor(open_graph): try to make climate happy
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 23, 2019
1 parent 55ca546 commit 07d83c7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/plugins/filter/after_post_render/external_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ function externalLinkFilter(data) {

const siteHost = url.parse(config.url).hostname || config.url;

data.content = data.content.replace(/<a.*?(href="(.*?)").*?>/gi, (str, hrefStr, href) => {
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 str;
// Exit if the url has same host with config.url
if (data.hostname === siteHost) return str;
if (!data.protocol || data.hostname === siteHost) return str;

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

0 comments on commit 07d83c7

Please sign in to comment.