Skip to content

Commit

Permalink
perf(external_link): optimize external link filter
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Dec 29, 2024
1 parent 0fd4d43 commit f629e39
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/plugins/filter/after_render/external_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const rTargetAttr = /target=/i;
const rRelAttr = /rel=/i;
const rRelStrAttr = /rel=["']([^<>"']*)["']/i;

const addNoopener = (relStr: string, rel: string) => {
return rel.includes('noopenner') ? relStr : `rel="${rel} noopener"`;
};

function externalLinkFilter(this: Hexo, data: string): string {
if (!EXTERNAL_LINK_SITE_ENABLED) return;

Expand All @@ -31,9 +35,7 @@ function externalLinkFilter(this: Hexo, data: string): string {
result += str;
} else {
if (rRelAttr.test(str)) {
result += str.replace(rRelStrAttr, (relStr, rel) => {
return rel.includes('noopenner') ? relStr : `rel="${rel} noopener"`;
}).replace('href=', 'target="_blank" href=');
result += str.replace(rRelStrAttr, addNoopener).replace('href=', 'target="_blank" href=');
} else {
result += str.replace('href=', 'target="_blank" rel="noopener" href=');
}
Expand Down

0 comments on commit f629e39

Please sign in to comment.