Skip to content

Commit 0578e1e

Browse files
Merge pull request #274 from github/feature/add-xmpp-support
Added xmpp and mailto support to the autoprefixer extension
2 parents ef1cfcb + 4c64c2c commit 0578e1e

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(cmark-gfm)
44
set(PROJECT_VERSION_MAJOR 0)
55
set(PROJECT_VERSION_MINOR 29)
66
set(PROJECT_VERSION_PATCH 0)
7-
set(PROJECT_VERSION_GFM 4)
7+
set(PROJECT_VERSION_GFM 5)
88
set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.gfm.${PROJECT_VERSION_GFM})
99

1010
include("FindAsan.cmake")

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[0.29.0.gfm.5]
2+
* Added xmpp: and mailto: support to the autolink extension
3+
14
[0.29.0.gfm.4]
25
* Remove `source` from list of HTML blocks
36

extensions/autolink.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,22 @@ static cmark_node *match(cmark_syntax_extension *ext, cmark_parser *parser,
269269
// inline was finished in inlines.c.
270270
}
271271

272+
static bool validate_protocol(char protocol[], uint8_t *data, int rewind) {
273+
size_t len = strlen(protocol);
274+
275+
// Check that the protocol matches
276+
for (int i = 1; i <= len; i++) {
277+
if (data[-rewind - i] != protocol[len - i]) {
278+
return false;
279+
}
280+
}
281+
282+
char prev_char = data[-rewind - len - 1];
283+
284+
// Make sure the character before the protocol is non-alphanumeric
285+
return !cmark_isalnum(prev_char);
286+
}
287+
272288
static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset, int depth) {
273289
// postprocess_text can recurse very deeply if there is a very long line of
274290
// '@' only. Stop at a reasonable depth to ensure it cannot crash.
@@ -278,6 +294,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
278294
uint8_t *data = text->as.literal.data,
279295
*at;
280296
size_t size = text->as.literal.len;
297+
bool auto_mailto = true;
298+
bool is_xmpp = false;
281299
int rewind, max_rewind,
282300
nb = 0, np = 0, ns = 0;
283301

@@ -304,8 +322,18 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
304322
if (strchr(".+-_", c) != NULL)
305323
continue;
306324

307-
if (c == '/')
308-
ns++;
325+
if (strchr(":", c) != NULL) {
326+
if (validate_protocol("mailto:", data, rewind)) {
327+
auto_mailto = false;
328+
continue;
329+
}
330+
331+
if (validate_protocol("xmpp:", data, rewind)) {
332+
auto_mailto = false;
333+
is_xmpp = true;
334+
continue;
335+
}
336+
}
309337

310338
break;
311339
}
@@ -325,6 +353,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
325353
nb++;
326354
else if (c == '.' && link_end < size - 1 && cmark_isalnum(data[link_end + 1]))
327355
np++;
356+
else if (c == '/' && is_xmpp)
357+
continue;
328358
else if (c != '-' && c != '_')
329359
break;
330360
}
@@ -347,7 +377,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
347377
cmark_node *link_node = cmark_node_new_with_mem(CMARK_NODE_LINK, parser->mem);
348378
cmark_strbuf buf;
349379
cmark_strbuf_init(parser->mem, &buf, 10);
350-
cmark_strbuf_puts(&buf, "mailto:");
380+
if (auto_mailto)
381+
cmark_strbuf_puts(&buf, "mailto:");
351382
cmark_strbuf_put(&buf, data - rewind, (bufsize_t)(link_end + rewind));
352383
link_node->as.link.url = cmark_chunk_buf_detach(&buf);
353384

test/extensions.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,38 @@ size (100) for parsing delimiters in inlines.c
545545

546546
547547

548+
549+
550+
551+
552+
553+
554+
555+
556+
557+
558+
This is a mailto:[email protected]
559+
560+
561+
562+
563+
564+
565+
566+
mailto:[email protected]/message
567+
568+
569+
570+
571+
572+
573+
574+
xmpp:[email protected]/message
575+
576+
xmpp:[email protected]/message.
577+
578+
Email me at:[email protected]
579+
548580
www.github.com www.github.com/á
549581

550582
www.google.com/a_b
@@ -568,6 +600,22 @@ http://🍄.ga/ http://x🍄.ga/
568600
<p>: <a href="http://google.com">http://google.com</a> <a href="https://google.com">https://google.com</a></p>
569601
<p><a href="http://google.com/%C3%A5">http://google.com/å</a> <a href="http://google.com/%C3%A5">http://google.com/å</a></p>
570602
<p><a href="mailto:[email protected]">[email protected]</a></p>
603+
<p><a href="mailto:[email protected]">[email protected]</a></p>
604+
<p><a href="mailto:[email protected]">[email protected]</a>.</p>
605+
<p><a href="mailto:[email protected]">[email protected]</a>/</p>
606+
<p><a href="mailto:[email protected]">[email protected]</a>/<a href="mailto:[email protected]">[email protected]</a></p>
607+
<p><a href="mailto:[email protected]">mailto:[email protected]</a></p>
608+
<p>This is a <a href="mailto:[email protected]">mailto:[email protected]</a></p>
609+
<p><a href="mailto:[email protected]">mailto:[email protected]</a>.</p>
610+
<p>mmmmailto:<a href="mailto:[email protected]">[email protected]</a></p>
611+
<p><a href="mailto:[email protected]">mailto:[email protected]</a>/</p>
612+
<p><a href="mailto:[email protected]">mailto:[email protected]</a>/message</p>
613+
<p><a href="mailto:[email protected]">mailto:[email protected]</a>/<a href="mailto:[email protected]">mailto:[email protected]</a></p>
614+
<p><a href="xmpp:[email protected]">xmpp:[email protected]</a></p>
615+
<p><a href="xmpp:[email protected]">xmpp:[email protected]</a>.</p>
616+
<p><a href="xmpp:[email protected]/message">xmpp:[email protected]/message</a></p>
617+
<p><a href="xmpp:[email protected]/message">xmpp:[email protected]/message</a>.</p>
618+
<p>Email me at:<a href="mailto:[email protected]">[email protected]</a></p>
571619
<p><a href="http://www.github.com">www.github.com</a> <a href="http://www.github.com/%C3%A1">www.github.com/á</a></p>
572620
<p><a href="http://www.google.com/a_b">www.google.com/a_b</a></p>
573621
<p><strong>Autolink and <a href="http://inlines">http://inlines</a></strong></p>

0 commit comments

Comments
 (0)