comrak 0.56.0

A 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use finl_unicode::categories::CharacterCategories;
use memchr_n::MemchrN;
use std::borrow::Cow;
use std::str;
use std::sync::OnceLock;

use crate::Arena;
use crate::character_set::character_set;
use crate::ctype::{isalnum, isalpha, isspace};
use crate::nodes::{Node, NodeLink, NodeValue, Sourcepos};
use crate::parser::Spx;
use crate::parser::inlines::{Subject, make_inline};

pub(crate) fn process_email_autolinks<'a>(
    arena: &'a Arena<'a>,
    node: Node<'a>,
    contents: &mut Cow<'static, str>,
    relaxed_autolinks: bool,
    sourcepos: &mut Sourcepos,
    spx: &mut Spx,
) {
    let Some((mut post, mut before_len, mut skip)) =
        find_email_autolink(arena, contents, relaxed_autolinks)
    else {
        return;
    };

    let original = std::mem::take(contents);
    let mut offset = 0;
    let mut anchor = node;
    let mut current_sourcepos = *sourcepos;
    let mut first = true;

    loop {
        let initial_end_col = current_sourcepos.end.column;
        current_sourcepos.end.column = spx.consume(before_len);
        let nsp_end_col = spx.consume(skip);

        let before = &original[offset..offset + before_len];
        if first {
            *contents = before.to_string().into();
            *sourcepos = current_sourcepos;
            first = false;
        } else {
            let before = make_inline(
                arena,
                NodeValue::Text(before.to_string().into()),
                current_sourcepos,
            );
            anchor.insert_after(before);
            anchor = before;
        }

        let nsp: Sourcepos = (
            current_sourcepos.end.line,
            current_sourcepos.end.column + 1,
            current_sourcepos.end.line,
            nsp_end_col,
        )
            .into();
        post.data_mut().sourcepos = nsp;
        post.first_child().unwrap().data_mut().sourcepos = nsp;
        anchor.insert_after(post);
        anchor = post;

        offset += before_len + skip;
        if offset == original.len() {
            return;
        }

        let after_sourcepos: Sourcepos = (
            current_sourcepos.end.line,
            nsp.end.column + 1,
            current_sourcepos.end.line,
            initial_end_col,
        )
            .into();

        if let Some((next_post, next_before_len, next_skip)) =
            find_email_autolink(arena, &original[offset..], relaxed_autolinks)
        {
            post = next_post;
            before_len = next_before_len;
            skip = next_skip;
            current_sourcepos = after_sourcepos;
        } else {
            let after = make_inline(
                arena,
                NodeValue::Text(original[offset..].to_string().into()),
                after_sourcepos,
            );
            anchor.insert_after(after);
            return;
        }
    }
}

fn find_email_autolink<'a>(
    arena: &'a Arena<'a>,
    contents: &str,
    relaxed_autolinks: bool,
) -> Option<(Node<'a>, usize, usize)> {
    static MATCHER: OnceLock<MemchrN> = OnceLock::new();
    static RELAXED_MATCHER: OnceLock<MemchrN> = OnceLock::new();

    let matcher = if relaxed_autolinks {
        RELAXED_MATCHER.get_or_init(|| MemchrN::new(b"@"))
    } else {
        MATCHER.get_or_init(|| MemchrN::new(b"@[]"))
    };
    let bytes = contents.as_bytes();
    if !bytes.contains(&b'@') {
        return None;
    }
    let mut bracket_opening = 0;

    for i in matcher.iter(bytes) {
        match bytes[i] {
            b'[' if !relaxed_autolinks => bracket_opening += 1,
            b']' if !relaxed_autolinks => bracket_opening -= 1,
            b'@' => {
                if bracket_opening <= 0
                    && let Some((post, reverse, skip)) =
                        email_match(arena, contents, i, relaxed_autolinks)
                {
                    return Some((post, i - reverse, skip));
                }
            }
            _ => debug_assert!(false, "matcher will only report @, [, or ]"),
        }
    }

    None
}

fn email_match<'a>(
    arena: &'a Arena<'a>,
    contents: &str,
    i: usize,
    relaxed_autolinks: bool,
) -> Option<(Node<'a>, usize, usize)> {
    const EMAIL_OK_SET: [bool; 256] = character_set!(b".+-_");

    let size = contents.len();
    let bytes = contents.as_bytes();

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

    while rewind < i {
        let c = bytes[i - rewind - 1];

        if isalnum(c) || EMAIL_OK_SET[c as usize] {
            rewind += 1;
            continue;
        }

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

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

        break;
    }

    if rewind == 0 {
        return None;
    }

    let mut link_end = 1;
    let mut np = 0;

    while link_end < size - i {
        let c = bytes[i + link_end];

        if isalnum(c) {
            // empty
        } else if c == b'@' {
            return None;
        } else if c == b'.' && link_end < size - i - 1 && isalnum(bytes[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;
        }

        link_end += 1;
    }

    if link_end < 2
        || np == 0
        || (!isalpha(bytes[i + link_end - 1]) && bytes[i + link_end - 1] != b'.')
    {
        return None;
    }

    link_end = autolink_delim(&contents[i..], link_end, relaxed_autolinks);
    if link_end == 0 {
        return None;
    }

    let text = &contents[i - rewind..link_end + i];
    let url = if auto_mailto {
        format!("mailto:{text}")
    } else {
        text.to_string()
    };

    let inl = make_inline(
        arena,
        NodeValue::Link(Box::new(NodeLink {
            url,
            title: String::new(),
        })),
        (0, 1, 0, 1).into(),
    );

    inl.append(make_inline(
        arena,
        NodeValue::Text(text.to_string().into()),
        (0, 1, 0, 1).into(),
    ));
    Some((inl, rewind, rewind + link_end))
}

fn validate_protocol(protocol: &str, contents: &str, cursor: usize) -> bool {
    let size = contents.len();
    let bytes = contents.as_bytes();
    let mut rewind = 0;

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

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

pub fn www_match<'a>(
    subject: &mut Subject<'a, '_, '_, '_, '_, '_>,
) -> Option<(Node<'a>, usize, usize)> {
    const WWW_DELIMS: [bool; 256] = character_set!(b"*_~([");
    let i = subject.scanner.pos;
    let relaxed_autolinks = subject.options.parse.relaxed_autolinks;
    let bytes = subject.input.as_bytes();

    if i < subject.relaxed_autolink_dead_zone_end {
        return None;
    }

    if i > 0 && !isspace(bytes[i - 1]) && !WWW_DELIMS[bytes[i - 1] as usize] {
        return None;
    }

    if !subject.input[i..].starts_with("www.") {
        return None;
    }

    // Skip over "www." for domain validation, but account for its length in the overall link
    let mut link_end = check_domain(&subject.input[i + 4..], relaxed_autolinks)? + 4;

    while i + link_end < subject.input.len() && !isspace(bytes[i + link_end]) {
        // basic test to detect whether we're in a normal markdown link - not exhaustive
        if relaxed_autolinks && bytes[i + link_end - 1] == b']' && bytes[i + link_end] == b'(' {
            subject.relaxed_autolink_dead_zone_end = i + link_end;
            return None;
        }
        link_end += 1;
    }

    link_end = autolink_delim(&subject.input[i..], link_end, relaxed_autolinks);

    let mut url = "http://".to_string();
    url.push_str(&subject.input[i..link_end + i]);

    let inl = make_inline(
        subject.arena,
        NodeValue::Link(Box::new(NodeLink {
            url,
            title: String::new(),
        })),
        (0, 1, 0, 1).into(),
    );

    inl.append(make_inline(
        subject.arena,
        NodeValue::Text(subject.input[i..link_end + i].to_string().into()),
        (0, 1, 0, 1).into(),
    ));
    Some((inl, 0, link_end))
}

const MAX_DOMAIN_DOTS: usize = 10;

struct DomainScan {
    end: usize,
    dots: usize,
    uscored_host: bool,
}

fn scan_domain(data: &str) -> DomainScan {
    let mut end = 0;
    let mut dots = 0;
    let mut uscores = 0;
    let mut prev_uscores = 0;

    for (i, c) in data.char_indices() {
        match c {
            '\\' if i + 1 < data.len() => {}
            '_' => uscores += 1,
            '.' => {
                prev_uscores = uscores;
                uscores = 0;
                dots += 1;
            }
            c if is_valid_hostchar(c) || c == '-' => {}
            _ => {
                end = i;
                break;
            }
        }
        end = i + c.len_utf8();
    }

    DomainScan {
        end,
        dots,
        uscored_host: uscores > 0 || prev_uscores > 0,
    }
}

fn check_domain(data: &str, allow_short: bool) -> Option<usize> {
    let scan = scan_domain(data);

    if scan.dots > MAX_DOMAIN_DOTS {
        return Some(scan.end);
    }

    if scan.uscored_host {
        return None;
    }

    (allow_short || scan.dots > 0).then_some(scan.end)
}

fn is_valid_hostchar(ch: char) -> bool {
    !(ch.is_whitespace() || ch.is_punctuation() || ch.is_symbol())
}

fn autolink_delim(data: &str, mut link_end: usize, relaxed_autolinks: bool) -> usize {
    const LINK_END_ASSORTMENT: [bool; 256] = character_set!(b"?!.,:*_~'\"");
    // \u{2069} (Pop Directional Isolate)
    const LINK_END_UNICODE: [u8; 3] = [0xe2, 0x81, 0xa9];

    let bytes = data.as_bytes();
    let mut parentheses = (0, 0);
    let mut brackets = (0, 0);
    let mut braces = (0, 0);
    for (i, &b) in bytes.iter().enumerate().take(link_end) {
        if b == b'<' {
            link_end = i;
            break;
        }

        match b {
            b'(' => parentheses.0 += 1,
            b')' => parentheses.1 += 1,
            b'[' => brackets.0 += 1,
            b']' => brackets.1 += 1,
            b'{' => braces.0 += 1,
            b'}' => braces.1 += 1,
            _ => (),
        }
    }

    while link_end > 0 {
        let cclose = bytes[link_end - 1];
        let counts = match cclose {
            b')' => Some(parentheses),
            b']' if relaxed_autolinks => Some(brackets),
            b'}' if relaxed_autolinks => Some(braces),
            _ => None,
        };

        if LINK_END_ASSORTMENT[cclose as usize] {
            link_end -= 1;
        } else if cclose == b';' {
            let mut new_end = link_end - 2;

            while new_end > 0 && isalpha(bytes[new_end]) {
                new_end -= 1;
            }

            if new_end < link_end - 2 && bytes[new_end] == b'&' {
                link_end = new_end;
            } else {
                link_end -= 1;
            }
        } else if let Some((opening, closing)) = counts {
            if closing <= opening {
                break;
            }

            match cclose {
                b')' => parentheses.1 -= 1,
                b']' => brackets.1 -= 1,
                b'}' => braces.1 -= 1,
                _ => unreachable!(),
            }
            link_end -= 1;
        } else if cclose == LINK_END_UNICODE[2] {
            let slice = &bytes[link_end - LINK_END_UNICODE.len()..link_end];

            if slice == LINK_END_UNICODE {
                link_end -= LINK_END_UNICODE.len();
                break;
            }

            break;
        } else {
            break;
        }
    }

    link_end
}

pub fn url_match<'a>(
    subject: &mut Subject<'a, '_, '_, '_, '_, '_>,
) -> Option<(Node<'a>, usize, usize)> {
    const SCHEMES: [&str; 3] = ["http", "https", "ftp"];

    let i = subject.scanner.pos;
    let relaxed_autolinks = subject.options.parse.relaxed_autolinks;
    let bytes = subject.input.as_bytes();
    let size = subject.input.len();

    if i < subject.relaxed_autolink_dead_zone_end {
        return None;
    }

    if size - i < 4 || bytes[i + 1] != b'/' || bytes[i + 2] != b'/' {
        return None;
    }

    let mut rewind = 0;
    while rewind < i && isalpha(bytes[i - rewind - 1]) {
        rewind += 1;
    }

    if !relaxed_autolinks {
        let scheme = &subject.input[i - rewind..i];
        let cond = |s: &&str| size - i + rewind >= s.len() && scheme.eq_ignore_ascii_case(s);
        if !SCHEMES.iter().any(cond) {
            return None;
        }

        if !subject.input[i + 3..]
            .chars()
            .next()
            .is_some_and(is_valid_hostchar)
        {
            return None;
        }
    }

    let mut link_end = check_domain(&subject.input[i + 3..], true)? + 3;

    while link_end < size - i && !isspace(bytes[i + link_end]) {
        // basic test to detect whether we're in a normal markdown link - not exhaustive
        if relaxed_autolinks
            && link_end > 0
            && bytes[i + link_end - 1] == b']'
            && bytes[i + link_end] == b'('
        {
            subject.relaxed_autolink_dead_zone_end = i + link_end;
            return None;
        }
        link_end += 1;
    }

    link_end = autolink_delim(&subject.input[i..], link_end, relaxed_autolinks);

    let url = &subject.input[i - rewind..i + link_end];
    let inl = make_inline(
        subject.arena,
        NodeValue::Link(Box::new(NodeLink {
            url: url.to_string(),
            title: String::new(),
        })),
        (0, 1, 0, 1).into(),
    );

    inl.append(make_inline(
        subject.arena,
        NodeValue::Text(url.to_string().into()),
        (0, 1, 0, 1).into(),
    ));
    Some((inl, rewind, rewind + link_end))
}

#[cfg(test)]
mod tests {
    use super::find_email_autolink;
    use crate::Arena;

    #[test]
    fn email_autolink_bracket_depth() {
        let cases = [
            ("foo@example.com", false, Some((0, 15))),
            ("☃ foo@example.com", false, Some((4, 15))),
            ("[foo@example.com]", false, None),
            ("[[foo@example.com]] bar@example.com", false, Some((20, 15))),
            ("[foo@example.com]", true, Some((1, 15))),
            ("]][[foo@example.com", false, Some((4, 15))),
            ("[foo@example.com", false, None),
            ("[foo@example.com", true, Some((1, 15))),
            ("no email or brackets", false, None),
        ];

        for (contents, relaxed, expected) in cases {
            let arena = Arena::new();
            let actual = find_email_autolink(&arena, contents, relaxed)
                .map(|(_, before_len, skip)| (before_len, skip));
            assert_eq!(actual, expected, "{contents:?}, relaxed: {relaxed}");
        }
    }
}