Skip to content

Commit 7f7a496

Browse files
fix: support GFM protocol autolinks (#4067)
1 parent 5196d98 commit 7f7a496

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎src/rules.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ const reflinkSearch = edit('reflink|nolink(?!\\()', 'g')
501501
.getRegex();
502502

503503
const _caseInsensitiveProtocol = /[hH][tT][tT][pP][sS]?|[fF][tT][pP]/;
504+
const _extendedEmail = /[A-Za-z0-9._+-]+@[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![\w-])/;
505+
const _extendedEmailProtocol = edit(/(?:mailto:email|xmpp:email(?:\/[A-Za-z0-9@.]+)?)/)
506+
.replace(/email/g, _extendedEmail)
507+
.getRegex();
504508

505509
/**
506510
* Normal Inline Grammar
@@ -559,14 +563,16 @@ const inlineGfm: Record<InlineKeys, RegExp> = {
559563
emStrongLDelim: emStrongLDelimGfm,
560564
delLDelim,
561565
delRDelim,
562-
url: edit(/^((?:protocol):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/)
566+
url: edit(/^emailProtocol|^((?:protocol):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/)
567+
.replace('emailProtocol', _extendedEmailProtocol)
563568
.replace('protocol', _caseInsensitiveProtocol)
564569
.replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![\w-])/)
565570
.getRegex(),
566571
_backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,
567572
del: /^(~~?)(?=[^\s~])((?:\\[\s\S]|[^\\])*?(?:\\[\s\S]|[^\s~\\]))\1(?=[^~]|$)/,
568-
text: edit(/^(`+|~+|[^`~])(?:(?=[`~])|(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|protocol:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/)
573+
text: edit(/^(?:[^a-zA-Z0-9](?=emailProtocol)|(`+|~+|[^`~])(?:(?=[`~])|(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|protocol:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9](?=emailProtocol)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))))/)
569574
.replace('protocol', _caseInsensitiveProtocol)
575+
.replace(/emailProtocol/g, /(?:mailto|xmpp):/)
570576
.getRegex(),
571577
};
572578

‎test/specs/gfm/gfm.0.29.json‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,19 @@
147147
"section": "[extension] Autolinks",
148148
"html": "<p><a href=\"mailto:[email protected]\">mailto:[email protected]</a></p>\n<p><a href=\"mailto:[email protected]\">mailto:[email protected]</a></p>\n<p><a href=\"mailto:[email protected]\">mailto:[email protected]</a>.</p>\n<p><a href=\"mailto:[email protected]\">mailto:[email protected]</a>/</p>\n<p>mailto:[email protected]</p>\n<p>mailto:[email protected]_</p>\n<p><a href=\"xmpp:[email protected]\">xmpp:[email protected]</a></p>\n<p><a href=\"xmpp:[email protected]\">xmpp:[email protected]</a>.</p>",
149149
"markdown": "mailto:[email protected]\n\nmailto:[email protected]\n\nmailto:[email protected].\n\nmailto:[email protected]/\n\nmailto:[email protected]\n\nmailto:[email protected]_\n\nxmpp:[email protected]\n\nxmpp:[email protected].",
150-
"example": 633,
151-
"shouldFail": true
150+
"example": 633
152151
},
153152
{
154153
"section": "[extension] Autolinks",
155154
"html": "<p><a href=\"xmpp:[email protected]/txt\">xmpp:[email protected]/txt</a></p>\n<p><a href=\"xmpp:[email protected]/txt@bin\">xmpp:[email protected]/txt@bin</a></p>\n<p><a href=\"xmpp:[email protected]/[email protected]\">xmpp:[email protected]/[email protected]</a></p>",
156155
"markdown": "xmpp:[email protected]/txt\n\nxmpp:[email protected]/txt@bin\n\nxmpp:[email protected]/[email protected]",
157-
"example": 634,
158-
"shouldFail": true
156+
"example": 634
159157
},
160158
{
161159
"section": "[extension] Autolinks",
162160
"html": "<p><a href=\"xmpp:[email protected]/txt\">xmpp:[email protected]/txt</a>/bin</p>",
163161
"markdown": "xmpp:[email protected]/txt/bin",
164-
"example": 635,
165-
"shouldFail": true
162+
"example": 635
166163
},
167164
{
168165
"section": "[extension] Disallowed Raw HTML",

‎test/unit/Lexer.test.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,59 @@ paragraph
21222122
],
21232123
});
21242124
});
2125+
2126+
it('url email protocols', () => {
2127+
expectInlineTokens({
2128+
md: 'Contact mailto:[email protected] and (xmpp:[email protected]/resource).',
2129+
options: { gfm: true },
2130+
tokens: [
2131+
{
2132+
type: 'text',
2133+
raw: 'Contact ',
2134+
text: 'Contact ',
2135+
escaped: false,
2136+
},
2137+
{
2138+
type: 'link',
2139+
raw: 'mailto:[email protected]',
2140+
text: 'mailto:[email protected]',
2141+
href: 'mailto:[email protected]',
2142+
tokens: [
2143+
{
2144+
type: 'text',
2145+
raw: 'mailto:[email protected]',
2146+
text: 'mailto:[email protected]',
2147+
},
2148+
],
2149+
},
2150+
{
2151+
type: 'text',
2152+
raw: ' and (',
2153+
text: ' and (',
2154+
escaped: false,
2155+
},
2156+
{
2157+
type: 'link',
2158+
raw: 'xmpp:[email protected]/resource',
2159+
text: 'xmpp:[email protected]/resource',
2160+
href: 'xmpp:[email protected]/resource',
2161+
tokens: [
2162+
{
2163+
type: 'text',
2164+
raw: 'xmpp:[email protected]/resource',
2165+
text: 'xmpp:[email protected]/resource',
2166+
},
2167+
],
2168+
},
2169+
{
2170+
type: 'text',
2171+
raw: ').',
2172+
text: ').',
2173+
escaped: false,
2174+
},
2175+
],
2176+
});
2177+
});
21252178
});
21262179

21272180
it('text', () => {

0 commit comments

Comments
 (0)