-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
fix: external link ignore mailto: & javascript: #3812
Conversation
@@ -37,7 +37,7 @@ function externalLinkFilter(data) { | |||
config.external_link.field !== 'post') return; | |||
|
|||
data.content = data.content.replace(/<a.*?(href=['"](.*?)['"]).*?>/gi, (str, hrefStr, href) => { | |||
if (/target=/gi.test(str) || !isExternal(href, config)) return str; | |||
if (/target=/gi.test(str) || href.startsWith('mailto:') || href.startsWith('javascript:') || !isExternal(href, config)) return str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are countless of types (data:
, vbscript:
and so on), can test whether it's a link protocol (http/ftp) or not by testing whether URL(href).origin
is null. See encodeURL()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will change to that.
It required WHATWG URL API. I might mitgate external_link filter to use it in this PR.
98fd59a
to
ab23935
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to handle some oddity in whatwg.
|
||
if (!data.protocol || !sitehost) return false; | ||
if (!data.protocol || !sitehost || !data.origin) return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when there is no data.origin, it returns a string value of 'null'
, not null type.
!data.protocol
should be removed, because it's not null even in javascript:foobar
.
const url = new URL('javascript:foobar')
console.log(url.protocol)
// 'javascript:'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data
variable will be string type if url
is internal link.
you need to check the type of data
first, otherwise data.origin
will cause error if internal link.
if (!sitehost || typeof data === 'string') return false;
if (data.origin === 'null') return false;
const host = data.hostname; | ||
const sitehost = parse(config.url).hostname || config.url; | ||
const sitehost = urlObj(config.url).hostname || config.url; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
urlObj(config.url).hostname
would throw error if config.url
doesn't have http
.
To handle that case,
const sitehost = typeof urlObj(config.url) === 'object' ? urlObj(config.url).hostname : config.url;
Apply code suggestions from code review by @curbengh
@@ -208,23 +208,27 @@ describe('External link - post', () => { | |||
'# External link test', | |||
'1. External link', | |||
'<a href="https://hexo.io/">Hexo</a>', | |||
'2. External link with "rel" Attribute', | |||
'2. Link with hash (#), mailto: , javascript: shouldn\'t be processed', | |||
'<a href="#top">Hexo</a>', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
I might implement |
This reverts commit 91a4784.
This reverts commit 91a4784.
* fix: external link ignore mailto: & javascript: Close hexojs#3796 * refactor: use startsWith instead of regex * refactor(external_link): use whatwg url * fix(external_link): handle whatwg url api Apply code suggestions from code review by @curbengh
Close #3796
What does it do?
Fix #3796 (comment)
How to test
Screenshots
Pull request tasks