Skip to content
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 autolink literals in link labels #6

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var asciiControl = require('micromark/dist/character/ascii-control')
var markdownLineEnding = require('micromark/dist/character/markdown-line-ending')
var unicodePunctuation = require('micromark/dist/character/unicode-punctuation')
var unicodeWhitespace = require('micromark/dist/character/unicode-whitespace')
var types = require('micromark/dist/constant/types')

var www = {tokenize: tokenizeWww}
var http = {tokenize: tokenizeHttp}
Expand Down Expand Up @@ -59,7 +60,7 @@ function tokenizeEmailAutolink(effects, ok, nok) {

function start(code) {
/* istanbul ignore next - hooks. */
if (!gfmAtext(code) || !previousEmail(self.previous)) {
if (!gfmAtext(code) || !previousEmail(self.previous) || inLabelLink(self.events)) {
return nok(code)
}

Expand Down Expand Up @@ -144,7 +145,7 @@ function tokenizeWwwAutolink(effects, ok, nok) {

function start(code) {
/* istanbul ignore next - hooks. */
if ((code !== 87 && code - 32 !== 87) || !previousWww(self.previous)) {
if ((code !== 87 && code - 32 !== 87) || !previousWww(self.previous) || inLabelLink(self.events)) {
return nok(code)
}

Expand All @@ -171,7 +172,7 @@ function tokenizeHttpAutolink(effects, ok, nok) {

function start(code) {
/* istanbul ignore next - hooks. */
if ((code !== 72 && code - 32 !== 72) || !previousHttp(self.previous)) {
if ((code !== 72 && code - 32 !== 72) || !previousHttp(self.previous) || inLabelLink(self.events)) {
return nok(code)
}

Expand Down Expand Up @@ -639,3 +640,7 @@ function previousHttp(code) {
function previousEmail(code) {
return code !== 47 /* `/` */ && previousHttp(code)
}

function inLabelLink(events) {
return events.some(event => event[1].type === types.labelLink)
}
10 changes: 10 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ a@a-b.c
Can’t end in an underscore followed by a period: aaa@a.b_.

Can contain an underscore followed by a period: aaa@a.b_.c

## Link text should not be expanded

[Visit www.example.com](http://www.example.com) please.

[Visit http://www.example.com](http://www.example.com) please.

[Mail example@example.com](mailto:example@example.com) please.

[link]() <http://autolink> should still be expanded.
5 changes: 5 additions & 0 deletions test/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ <h2>Email autolinks</h2>
<p><a href="mailto:a@a-b.c">a@a-b.c</a></p>
<p>Can’t end in an underscore followed by a period: aaa@a.b_.</p>
<p>Can contain an underscore followed by a period: <a href="mailto:aaa@a.b_.c">aaa@a.b_.c</a></p>
<h2>Link text should not be expanded</h2>
<p><a href="http://www.example.com">Visit www.example.com</a> please.</p>
<p><a href="http://www.example.com">Visit http://www.example.com</a> please.</p>
<p><a href="mailto:example@example.com">Mail example@example.com</a> please.</p>
<p><a href="">link</a> <a href="http://autolink">http://autolink</a> should still be expanded.</p>
2 changes: 1 addition & 1 deletion test/previous-complex.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<p>First punctuation after digits: :<a href="mailto:noreply@example.com">noreply@example.com</a>, :<a href="http://example.com">http://example.com</a>, :<a href="https://example.com">https://example.com</a>, :<a href="http://www.example.com">www.example.com</a></p>
<p>Last punctuation before caps: @<a href="mailto:noreply@example.com">noreply@example.com</a>, @<a href="http://example.com">http://example.com</a>, @<a href="https://example.com">https://example.com</a>, @<a href="http://www.example.com">www.example.com</a></p>
<p>First uppercase: <a href="mailto:Anoreply@example.com">Anoreply@example.com</a>, Ahttp://example.com, Ahttps://example.com, Awww.example.com</p>
<p>First punctuation after uppercase: [<a href="mailto:noreply@example.com">noreply@example.com</a>, [<a href="http://example.com">http://example.com</a>, [<a href="https://example.com">https://example.com</a>, [<a href="http://www.example.com">www.example.com</a></p>
<p>Punctuation after uppercase: \<a href="mailto:noreply@example.com">noreply@example.com</a>, \<a href="http://example.com">http://example.com</a>, \<a href="https://example.com">https://example.com</a>, \<a href="http://www.example.com">www.example.com</a></p>
<p>Last punctuation before lowercase (1): `<a href="mailto:noreply@example.com">noreply@example.com</a>;</p>
<p>(2) `<a href="http://example.com">http://example.com</a>;</p>
<p>(3) `<a href="https://example.com">https://example.com</a>;</p>
Expand Down
2 changes: 1 addition & 1 deletion test/previous-complex.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Last punctuation before caps: @noreply@example.com, @http://example.com, @https:

First uppercase: Anoreply@example.com, Ahttp://example.com, Ahttps://example.com, Awww.example.com

First punctuation after uppercase: [noreply@example.com, [http://example.com, [https://example.com, [www.example.com
Punctuation after uppercase: \noreply@example.com, \http://example.com, \https://example.com, \www.example.com

Last punctuation before lowercase (1): `noreply@example.com;

Expand Down
2 changes: 1 addition & 1 deletion test/previous.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ <h2>Others characters before</h2>
<p>While other characters before the email aren’t allowed by GFM, they work on
github.com: !<a href="mailto:a@b.c">a@b.c</a>, &quot;<a href="mailto:a@b.c">a@b.c</a>, #<a href="mailto:a@b.c">a@b.c</a>, $<a href="mailto:a@b.c">a@b.c</a>, &amp;<a href="mailto:a@b.c">a@b.c</a>, '<a href="mailto:a@b.c">a@b.c</a>, )<a href="mailto:a@b.c">a@b.c</a>, <a href="mailto:+a@b.c">+a@b.c</a>,
,<a href="mailto:a@b.c">a@b.c</a>, <a href="mailto:-a@b.c">-a@b.c</a>, <a href="mailto:.a@b.c">.a@b.c</a>, /a@b.c, :<a href="mailto:a@b.c">a@b.c</a>, ;<a href="mailto:a@b.c">a@b.c</a>, &lt;<a href="mailto:a@b.c">a@b.c</a>, =<a href="mailto:a@b.c">a@b.c</a>, &gt;<a href="mailto:a@b.c">a@b.c</a>, ?<a href="mailto:a@b.c">a@b.c</a>,
@<a href="mailto:a@b.c">a@b.c</a>, [<a href="mailto:a@b.c">a@b.c</a>, \<a href="mailto:a@b.c">a@b.c</a>, ]<a href="mailto:a@b.c">a@b.c</a>, ^<a href="mailto:a@b.c">a@b.c</a>, `<a href="mailto:a@b.c">a@b.c</a>, {<a href="mailto:a@b.c">a@b.c</a>, }<a href="mailto:a@b.c">a@b.c</a>.</p>
@<a href="mailto:a@b.c">a@b.c</a>, \<a href="mailto:a@b.c">a@b.c</a>, ]<a href="mailto:a@b.c">a@b.c</a>, ^<a href="mailto:a@b.c">a@b.c</a>, `<a href="mailto:a@b.c">a@b.c</a>, {<a href="mailto:a@b.c">a@b.c</a>, }<a href="mailto:a@b.c">a@b.c</a>.</p>
2 changes: 1 addition & 1 deletion test/previous.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ Can start after tilde~a@b.c.
While other characters before the email aren’t allowed by GFM, they work on
github.com: !a@b.c, "a@b.c, #a@b.c, $a@b.c, &a@b.c, 'a@b.c, )a@b.c, +a@b.c,
,a@b.c, -a@b.c, .a@b.c, /a@b.c, :a@b.c, ;a@b.c, <a@b.c, =a@b.c, >a@b.c, ?a@b.c,
@a@b.c, [a@b.c, \a@b.c, ]a@b.c, ^a@b.c, `a@b.c, {a@b.c, }a@b.c.
@a@b.c, \a@b.c, ]a@b.c, ^a@b.c, `a@b.c, {a@b.c, }a@b.c.