Skip to content

Commit 2a94e20

Browse files
authored
Merge pull request #1338 from Feder1co5oave/fix-1218
Fix auto-linking email
2 parents 4ee1c52 + 51e97fd commit 2a94e20

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

lib/marked.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,8 @@ inline.pedantic = merge({}, inline.normal, {
605605

606606
inline.gfm = merge({}, inline.normal, {
607607
escape: edit(inline.escape).replace('])', '~|])').getRegex(),
608-
url: edit(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/)
609-
.replace('email', inline._email)
610-
.getRegex(),
608+
_extended_email: /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,
609+
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
611610
_backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
612611
del: /^~+(?=\S)([\s\S]*?\S)~+/,
613612
text: edit(inline.text)
@@ -616,6 +615,9 @@ inline.gfm = merge({}, inline.normal, {
616615
.getRegex()
617616
});
618617

618+
inline.gfm.url = edit(inline.gfm.url)
619+
.replace('email', inline.gfm._extended_email)
620+
.getRegex();
619621
/**
620622
* GFM + Line Breaks Inline Grammar
621623
*/
@@ -703,22 +705,23 @@ InlineLexer.prototype.output = function(src) {
703705

704706
// url (gfm)
705707
if (!this.inLink && (cap = this.rules.url.exec(src))) {
706-
do {
707-
prevCapZero = cap[0];
708-
cap[0] = this.rules._backpedal.exec(cap[0])[0];
709-
} while (prevCapZero !== cap[0]);
710-
src = src.substring(cap[0].length);
711708
if (cap[2] === '@') {
712709
text = escape(cap[0]);
713710
href = 'mailto:' + text;
714711
} else {
712+
// do extended autolink path validation
713+
do {
714+
prevCapZero = cap[0];
715+
cap[0] = this.rules._backpedal.exec(cap[0])[0];
716+
} while (prevCapZero !== cap[0]);
715717
text = escape(cap[0]);
716718
if (cap[1] === 'www.') {
717719
href = 'http://' + text;
718720
} else {
719721
href = text;
720722
}
721723
}
724+
src = src.substring(cap[0].length);
722725
out += this.renderer.link(href, null, text);
723726
continue;
724727
}

test/specs/marked/marked-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Marked Autolinks', function() {
4949
describe('Marked Code spans', function() {
5050
var section = 'Code spans';
5151

52-
var shouldPassButFails = [1];
52+
var shouldPassButFails = [];
5353

5454
var willNotBeAttemptedByCoreTeam = [];
5555

test/specs/marked/marked.json

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
[
2-
{
3-
"section": "Autolinks",
4-
"markdown": "(See https://www.example.com/fhqwhgads.)",
5-
"html": "<p>(See <a href=\"https://www.example.com/fhqwhgads\">https://www.example.com/fhqwhgads</a>.)</p>",
6-
"example": 10
7-
},
8-
{
9-
"section": "Autolinks",
10-
"markdown": "((http://foo.com))",
11-
"html": "<p>((<a href=\"http://foo.com\">http://foo.com</a>))</p>",
12-
"example": 11
13-
},
14-
{
15-
"section": "Autolinks",
16-
"markdown": "((http://foo.com.))",
17-
"html": "<p>((<a href=\"http://foo.com\">http://foo.com</a>.))</p>",
18-
"example": 12
19-
},
202
{
213
"section": "Code spans",
224
"markdown": "`[email protected]`",
@@ -76,5 +58,35 @@
7658
"markdown": "Link: [constructor][].\n\n[constructor]: https://example.org/",
7759
"html": "<p>Link: <a href=\"https://example.org/\">constructor</a>.</p>",
7860
"example": 10
61+
},
62+
{
63+
"section": "Autolinks",
64+
"markdown": "(See https://www.example.com/fhqwhgads.)",
65+
"html": "<p>(See <a href=\"https://www.example.com/fhqwhgads\">https://www.example.com/fhqwhgads</a>.)</p>",
66+
"example": 11
67+
},
68+
{
69+
"section": "Autolinks",
70+
"markdown": "((http://foo.com))",
71+
"html": "<p>((<a href=\"http://foo.com\">http://foo.com</a>))</p>",
72+
"example": 12
73+
},
74+
{
75+
"section": "Autolinks",
76+
"markdown": "((http://foo.com.))",
77+
"html": "<p>((<a href=\"http://foo.com\">http://foo.com</a>.))</p>",
78+
"example": 13
79+
},
80+
{
81+
"section": "Autolinks",
82+
"markdown": "[email protected]~~",
83+
"html": "<p><del><a href=\"mailto:[email protected]\">[email protected]</a></del></p>",
84+
"example": 1307
85+
},
86+
{
87+
"section": "Autolinks",
88+
"markdown": "**[email protected]**",
89+
"html": "<p><strong><a href=\"mailto:[email protected]\">[email protected]</a></strong></p>",
90+
"example": 1327
7991
}
80-
]
92+
]

0 commit comments

Comments
 (0)