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

Update to 0290gfm5 #188

Merged
merged 2 commits into from Aug 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions ext/commonmarker/autolink.c
Expand Up @@ -269,6 +269,22 @@ static cmark_node *match(cmark_syntax_extension *ext, cmark_parser *parser,
// inline was finished in inlines.c.
}

static bool validate_protocol(char protocol[], uint8_t *data, int rewind) {
size_t len = strlen(protocol);

// Check that the protocol matches
for (int i = 1; i <= len; i++) {
if (data[-rewind - i] != protocol[len - i]) {
return false;
}
}

char prev_char = data[-rewind - len - 1];

// Make sure the character before the protocol is non-alphanumeric
return !cmark_isalnum(prev_char);
}

static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset, int depth) {
// postprocess_text can recurse very deeply if there is a very long line of
// '@' only. Stop at a reasonable depth to ensure it cannot crash.
Expand All @@ -278,6 +294,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
uint8_t *data = text->as.literal.data,
*at;
size_t size = text->as.literal.len;
bool auto_mailto = true;
bool is_xmpp = false;
int rewind, max_rewind,
nb = 0, np = 0, ns = 0;

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

if (c == '/')
ns++;
if (strchr(":", c) != NULL) {
if (validate_protocol("mailto:", data, rewind)) {
auto_mailto = false;
continue;
}

if (validate_protocol("xmpp:", data, rewind)) {
auto_mailto = false;
is_xmpp = true;
continue;
}
}

break;
}
Expand All @@ -325,6 +353,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
nb++;
else if (c == '.' && link_end < size - 1 && cmark_isalnum(data[link_end + 1]))
np++;
else if (c == '/' && is_xmpp)
continue;
else if (c != '-' && c != '_')
break;
}
Expand All @@ -347,7 +377,8 @@ static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset,
cmark_node *link_node = cmark_node_new_with_mem(CMARK_NODE_LINK, parser->mem);
cmark_strbuf buf;
cmark_strbuf_init(parser->mem, &buf, 10);
cmark_strbuf_puts(&buf, "mailto:");
if (auto_mailto)
cmark_strbuf_puts(&buf, "mailto:");
cmark_strbuf_put(&buf, data - rewind, (bufsize_t)(link_end + rewind));
link_node->as.link.url = cmark_chunk_buf_detach(&buf);

Expand Down
4 changes: 2 additions & 2 deletions ext/commonmarker/cmark-gfm_version.h
@@ -1,7 +1,7 @@
#ifndef CMARK_GFM_VERSION_H
#define CMARK_GFM_VERSION_H

#define CMARK_GFM_VERSION ((0 << 24) | (29 << 16) | (0 << 8) | 4)
#define CMARK_GFM_VERSION_STRING "0.29.0.gfm.4"
#define CMARK_GFM_VERSION ((0 << 24) | (29 << 16) | (0 << 8) | 5)
#define CMARK_GFM_VERSION_STRING "0.29.0.gfm.5"

#endif
2 changes: 1 addition & 1 deletion ext/commonmarker/cmark-upstream