Skip to content

Commit

Permalink
Sync with cmark-gfm-0.29.0.gfm.5
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Jun 8, 2023
1 parent 32d36dc commit c3ec66e
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/parser/autolink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ fn email_match<'a>(

let size = contents.len();

let mut auto_mailto = true;
let mut is_xmpp = false;
let mut rewind = 0;

while rewind < i {
Expand All @@ -288,6 +290,21 @@ fn email_match<'a>(
continue;
}

if c == b':' {
if validate_protocol("mailto".to_string(), contents, i - rewind - 1) {
auto_mailto = false;
rewind += 1;
continue;
}

if validate_protocol("xmpp".to_string(), contents, i - rewind - 1) {
is_xmpp = true;
auto_mailto = false;
rewind += 1;
continue;
}
}

break;
}

Expand All @@ -307,6 +324,8 @@ fn email_match<'a>(
return None;
} else if c == b'.' && link_end < size - i - 1 && isalnum(contents[i + link_end + 1]) {
np += 1;
} else if c == b'/' && is_xmpp {
// xmpp allows a `/` in the url
} else if c != b'-' && c != b'_' {
break;
}
Expand All @@ -326,7 +345,11 @@ fn email_match<'a>(
return None;
}

let mut url = "mailto:".to_string();
let mut url = if auto_mailto {
"mailto:".to_string()
} else {
"".to_string()
};
let text = str::from_utf8(&contents[i - rewind..link_end + i]).unwrap();
url.push_str(text);

Expand All @@ -346,3 +369,15 @@ fn email_match<'a>(
));
Some((inl, rewind, rewind + link_end))
}

fn validate_protocol(protocol: String, contents: &[u8], cursor: usize) -> bool {
let size = contents.len();
let mut rewind = 0;

while rewind < cursor && isalpha(contents[cursor - rewind - 1]) {
rewind += 1;
}

size - cursor + rewind >= protocol.len()
&& &contents[cursor - rewind..cursor] == protocol.as_bytes()
}

0 comments on commit c3ec66e

Please sign in to comment.