Skip to content

Commit

Permalink
fix: ignore javascript: & mailto:
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 27, 2019
1 parent b1cd88a commit 1df133b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function(data) {
const noFollow = 'external nofollow noreferrer';

return data.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => {
if (!isExternal(href, config)) return str;
if (href.startsWith('mailto:') || href.startsWith('javascript:') || !isExternal(href, config)) return str;

if (/rel=/gi.test(str)) {
return str.replace(/rel="(.*?)"/gi, (relStr, rel) => {
Expand Down
12 changes: 10 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ describe('hexo-filter-nofollow', () => {
'5. Internal link',
'<a href="/archives/foo.html">Link</a>',
'6. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>'
'<a>Anchor</a>',
'7. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
'<a href="#top">Hexo</a>',
'<a href="mailto:[email protected]">Hexo</a>',
'<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>'
].join('\n');

hexo.config.nofollow.field = 'post';
Expand All @@ -102,7 +106,11 @@ describe('hexo-filter-nofollow', () => {
'5. Internal link',
'<a href="/archives/foo.html">Link</a>',
'6. Ignore links don\'t have "href" attribute',
'<a>Anchor</a>'
'<a>Anchor</a>',
'7. Link with hash (#), mailto: , javascript: shouldn\'t be processed',
'<a href="#top">Hexo</a>',
'<a href="mailto:[email protected]">Hexo</a>',
'<a href="javascript:alert(\'Hexo is awesome!\');">Hexo</a>'
].join('\n'));

hexo.config.nofollow.field = 'site';
Expand Down

0 comments on commit 1df133b

Please sign in to comment.