rsmarkdown-core 0.1.0

Streaming markdown core: normalize -> preprocess -> block split -> incremental AST (core only, no terminal deps)
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Byte-level scanning helpers ported from `markmend/core/src/preprocess/utils.ts`
//! and `pattern.ts`. All positions are byte offsets into the original string.
//!
//! ASCII needles are searched with memchr: in UTF-8, ASCII bytes never occur
//! inside multi-byte chars, so the returned offsets are always char
//! boundaries — safe to slice with.

/// memchr 版 count(ASCII needle:偏移天然边界,SIMD 加速)。
pub fn count_of(text: &str, needle: &str) -> usize {
    let needle = needle.as_bytes();
    let mut count = 0;
    let mut rest = text.as_bytes();
    while let Some(pos) = memchr::memmem::find(rest, needle) {
        count += 1;
        rest = &rest[pos + needle.len()..];
    }
    count
}

/// JS `\s` (without unicode flag): space, tab, newline, CR, FF, VT.
pub fn is_ws(c: char) -> bool {
    matches!(c, ' ' | '\t' | '\n' | '\r' | '\u{000c}' | '\u{000b}')
}

/// JS `String.prototype.trim`-ish (ASCII + unicode whitespace).
pub fn js_trim(s: &str) -> &str {
    let start = s
        .char_indices()
        .find(|(_, c)| !c.is_whitespace())
        .map(|(i, _)| i)
        .unwrap_or(s.len());
    let end = s
        .char_indices()
        .rev()
        .find(|(_, c)| !c.is_whitespace())
        .map(|(i, c)| i + c.len_utf8())
        .unwrap_or(start);
    &s[start..end]
}

/// Trailing whitespace run (`/\s+$/`), returns byte offset of the run start.
pub fn trailing_ws_offset(s: &str) -> usize {
    let mut end = s.len();
    for (i, c) in s.char_indices().rev() {
        if is_ws(c) {
            end = i;
        } else {
            break;
        }
    }
    end
}

/// `/```[\s\S]*?```/g` — all closed fenced code ranges `[start, end)`.
pub fn find_closed_code_block_ranges(content: &str) -> Vec<(usize, usize)> {
    let bytes = content.as_bytes();
    let mut ranges = Vec::new();
    let mut search = 0;
    while let Some(rel) = memchr::memmem::find(&bytes[search..], b"```") {
        let start = search + rel;
        let after = start + 3;
        let Some(rel2) = memchr::memmem::find(&bytes[after..], b"```") else {
            break;
        };
        let end = after + rel2 + 3;
        ranges.push((start, end));
        search = end;
    }
    ranges
}

pub fn is_position_in_ranges(position: usize, ranges: &[(usize, usize)]) -> bool {
    ranges
        .iter()
        .any(|&(start, end)| position >= start && position < end)
}

/// Whether `[start, end)` overlaps any range.
pub fn is_range_overlapping_ranges(start: usize, end: usize, ranges: &[(usize, usize)]) -> bool {
    ranges.iter().any(|&(rs, re)| {
        (start >= rs && start < re) || (end > rs && end <= re) || (start < rs && end > re)
    })
}

/// Toggle on each ` ``` ` while scanning up to `position`.
/// Byte-safe check: does the triple-backtick sequence start at `i`?
pub(crate) fn is_triple_backtick_at(bytes: &[u8], i: usize) -> bool {
    bytes[i] == b'`' && bytes.get(i + 1) == Some(&b'`') && bytes.get(i + 2) == Some(&b'`')
}

pub fn is_within_code_block(text: &str, position: usize) -> bool {
    let bytes = text.as_bytes();
    let mut in_code = false;
    let mut i = 0;
    while i < position && i < bytes.len() {
        if is_triple_backtick_at(bytes, i) {
            in_code = !in_code;
            i += 3;
        } else {
            i += 1;
        }
    }
    in_code
}

pub fn is_inside_unclosed_code_block(content: &str) -> bool {
    is_within_code_block(content, content.len())
}

/// Last paragraph = content after the last blank line.
/// Returns `(start_line_index, byte_offset_of_start)`.
pub fn last_paragraph_range(content: &str, skip_trailing_empty: bool) -> (usize, usize) {
    let lines: Vec<&str> = content.split('\n').collect();
    let mut start_line = 0;
    for i in (0..lines.len()).rev() {
        if skip_trailing_empty && i == lines.len() - 1 && lines[i].trim().is_empty() {
            continue;
        }
        if lines[i].trim().is_empty() {
            start_line = i + 1;
            break;
        }
    }
    let offset = if start_line == 0 {
        0
    } else {
        (lines[..start_line].join("\n").len() + 1).min(content.len())
    };
    (start_line, offset)
}

pub fn last_paragraph(content: &str, skip_trailing_empty: bool) -> &str {
    let (_start_line, offset) = last_paragraph_range(content, skip_trailing_empty);
    &content[offset..]
}

/// Find last non-empty line index (JS `findLastNonEmptyLineIndex`).
pub fn last_non_empty_line_index(lines: &[&str]) -> isize {
    for (i, line) in lines.iter().enumerate().rev() {
        if !line.trim().is_empty() {
            return i as isize;
        }
    }
    -1
}

pub(crate) fn is_backtick_part_of_triple(text: &str, index: usize) -> bool {
    let mut back = text[..index].chars().rev();
    let before = back.next();
    let before2 = back.next();
    let after = text.get(index + 1..).and_then(|s| s.chars().next());
    let after2 = text.get(index + 2..).and_then(|s| s.chars().next());
    let c = |o: Option<char>| o == Some('`');
    (c(before) && c(before2)) || (c(before) && c(after)) || (c(after) && c(after2))
}

/// `/`[^`\n]+`/`-style inline code ranges `[start, end)` outside code blocks.
pub fn find_inline_code_ranges(
    content: &str,
    code_block_ranges: &[(usize, usize)],
) -> Vec<(usize, usize)> {
    let mut positions: Vec<usize> = Vec::new();
    let bytes = content.as_bytes();
    for i in 0..bytes.len() {
        if is_position_in_ranges(i, code_block_ranges) {
            continue;
        }
        if bytes[i] != b'`' {
            continue;
        }
        if is_backtick_part_of_triple(content, i) {
            continue;
        }
        positions.push(i);
    }
    let mut ranges = Vec::new();
    for pair in positions.chunks(2) {
        if pair.len() == 2 {
            ranges.push((pair[0], pair[1] + 1));
        }
    }
    ranges
}

/// Mask `* _ ~` inside inline code with spaces (offsets preserved).
pub fn mask_inline_code_markdown_markers(content: &str, ranges: &[(usize, usize)]) -> String {
    if ranges.is_empty() {
        return content.to_string();
    }
    let mut bytes = content.as_bytes().to_vec();
    for &(start, end) in ranges {
        let range = start..end.min(bytes.len());
        for b in &mut bytes[range] {
            if *b == b'*' || *b == b'_' || *b == b'~' {
                *b = b' ';
            }
        }
    }
    String::from_utf8(bytes).expect("masking preserves utf8")
}

/// Odd number of preceding backslashes.
pub fn is_escaped_character(content: &str, index: usize) -> bool {
    let mut backslashes = 0;
    let bytes = content.as_bytes();
    let mut i = index;
    while i > 0 && bytes[i - 1] == b'\\' {
        backslashes += 1;
        i -= 1;
    }
    backslashes % 2 == 1
}

fn is_word_char(c: char) -> bool {
    // ~ `[\p{L}\p{N}]` (combining marks approximated via alphanumeric)
    c.is_alphanumeric()
}

pub fn is_underscore_inside_word(content: &str, start: usize, len: usize) -> bool {
    // start 是 marker 的字节偏移(边界);前一个字符从边界往前取整字符,
    // 避免 start - 1 落在多字节字符中间(曾导致 char boundary panic)。
    let prev = content.get(..start).and_then(|s| s.chars().next_back());
    let next = content.get(start + len..).and_then(|s| s.chars().next());
    prev.is_some_and(is_word_char) && next.is_some_and(is_word_char)
}

pub fn should_ignore_underscore_marker(content: &str, start: usize, len: usize) -> bool {
    is_escaped_character(content, start) || is_underscore_inside_word(content, start, len)
}

/// Mask escaped / intraword underscore runs with spaces.
pub fn mask_invalid_underscore_markers(content: &str) -> String {
    let mut bytes = content.as_bytes().to_vec();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] != b'_' {
            index += 1;
            continue;
        }
        let run_start = index;
        while index < bytes.len() && bytes[index] == b'_' {
            index += 1;
        }
        let run_len = index - run_start;
        if should_ignore_underscore_marker(content, run_start, run_len) {
            for b in &mut bytes[run_start..index] {
                *b = b' ';
            }
        }
    }
    String::from_utf8(bytes).expect("masking preserves utf8")
}

/// `/!?\[[^\]]*\]\([^)]*\)/g` — replace each complete link/image with `[text]()`.
fn replace_complete_links(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut i = 0;
    while i < text.len() {
        let ch = text[i..].chars().next().expect("char at boundary");
        let rest = &text[i..];
        let is_image = rest.starts_with("![");
        if ch == '[' || (is_image && ch == '!') {
            let label_start = i + usize::from(is_image);
            if let Some(close_rel) = memchr::memchr(b']', &text.as_bytes()[label_start..]) {
                let after_label = label_start + close_rel + 1;
                if text[after_label..].starts_with('(') {
                    let paren_rest = &text[after_label + 1..];
                    if let Some(cp) = memchr::memchr(b')', paren_rest.as_bytes()) {
                        out.push_str(&text[i..after_label]);
                        out.push_str("()");
                        i = after_label + 1 + cp + 1;
                        continue;
                    }
                }
            }
        }
        out.push(ch);
        i += ch.len_utf8();
    }
    out
}

/// `/!?\[[^\]]*\]\([^)]*$/g` — replace an unclosed link at the end with `[text](`.
fn replace_incomplete_link_suffix(text: &str) -> String {
    let bytes = text.as_bytes();
    // find the LAST `](` such that everything after it is non-`)`
    let mut best: Option<usize> = None;
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'[' || (bytes[i] == b'!' && bytes.get(i + 1) == Some(&b'[')) {
            let label_start = if bytes[i] == b'!' { i + 1 } else { i };
            if let Some(rel) = memchr::memchr(b']', &text.as_bytes()[label_start..]) {
                let after = label_start + rel + 1;
                if text[after..].starts_with('(') {
                    let rest = &text[after + 1..];
                    if !rest.contains(')') && !rest.is_empty() {
                        best = Some(after - 1);
                        i = text.len();
                        continue;
                    }
                }
            }
        }
        i += 1;
    }
    if let Some(pos) = best {
        let mut out = text[..pos].to_string();
        out.push_str("](");
        out
    } else {
        text.to_string()
    }
}

/// Remove code blocks, HTML tags, and link/image URL bodies from text,
/// so markdown markers inside URLs don't get counted.
pub fn remove_urls_from_text(text: &str) -> String {
    // 1. remove code blocks
    let mut result = String::with_capacity(text.len());
    let ranges = find_closed_code_block_ranges(text);
    let mut cursor = 0;
    for &(start, end) in &ranges {
        result.push_str(&text[cursor..start]);
        cursor = end;
    }
    result.push_str(&text[cursor..]);

    // 2. remove HTML tags `<[^>]*>`
    let mut no_html = String::with_capacity(result.len());
    let mut i = 0;
    while i < result.len() {
        let ch = result[i..].chars().next().expect("char at boundary");
        if ch == '<' {
            if let Some(rel) = memchr::memchr(b'>', &result.as_bytes()[i..]) {
                no_html.push(' ');
                i += rel + 1;
                continue;
            }
        }
        no_html.push(ch);
        i += ch.len_utf8();
    }

    // 3. complete links -> `[text]()`
    let after_complete = replace_complete_links(&no_html);
    // 4. incomplete link suffix -> `[text](`
    replace_incomplete_link_suffix(&after_complete)
}

/// Remove `$$...$$` block math (and optionally `$...$` inline math) spans.
pub fn remove_math_blocks_from_text(text: &str, single_dollar_enabled: bool) -> String {
    let mut bytes = text.as_bytes().to_vec();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' && bytes.get(i + 1) == Some(&b'$') {
            i += 2;
            continue;
        }
        if bytes[i] == b'$' && bytes.get(i + 1) == Some(&b'$') {
            // find closing `$$`
            let mut j = i + 2;
            let mut closing = None;
            while j + 1 < bytes.len() {
                if bytes[j] == b'$' && bytes[j + 1] == b'$' {
                    closing = Some(j);
                    break;
                }
                j += 1;
            }
            if let Some(pos) = closing {
                bytes.drain(i..pos + 2);
                continue; // same position re-checked
            } else {
                bytes.truncate(i);
                break;
            }
        }
        if single_dollar_enabled && bytes[i] == b'$' {
            let mut j = i + 1;
            let mut closing = None;
            while j < bytes.len() {
                if bytes[j] == b'$' && bytes.get(j - 1) != Some(&b'\\') {
                    closing = Some(j);
                    break;
                }
                j += 1;
            }
            if let Some(pos) = closing {
                bytes.drain(i..pos + 1);
                continue;
            } else {
                bytes.truncate(i);
                break;
            }
        }
        i += 1;
    }
    String::from_utf8(bytes).expect("math removal preserves utf8")
}

/// Whether `position` sits inside a `$$` math span (or `$` inline math when enabled).
pub fn is_within_math_block(text: &str, position: usize, single_dollar_enabled: bool) -> bool {
    let mut in_block = false;
    let mut in_inline = false;
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < position && i < text.len() {
        if bytes[i] == b'\\' && bytes.get(i + 1) == Some(&b'$') {
            i += 2;
            continue;
        }
        if bytes[i] == b'$' && bytes.get(i + 1) == Some(&b'$') {
            in_block = !in_block;
            i += 2;
            continue;
        }
        if single_dollar_enabled && !in_block && bytes[i] == b'$' {
            in_inline = !in_inline;
        }
        i += 1;
    }
    in_block || in_inline
}

/// Fold every `$$…$$` span that crosses a line break onto one line, leaving
/// fenced code blocks alone.
///
/// CommonMark reads the lines of a display formula as block syntax — `Z[J]`
/// over a lone `=` is a setext heading, a line opening with `+ c` is a list
/// item — and pulldown-cmark's math cannot cross a block boundary, so the
/// formula fell apart into escaped text. Whitespace is insignificant to LaTeX,
/// so the span is the same formula on one line, which pulldown-cmark reads as
/// display math whole.
pub fn join_multiline_math(content: &str) -> String {
    if !content.contains("$$") {
        return content.to_string();
    }
    let code = find_closed_code_block_ranges(content);
    let mut out = String::with_capacity(content.len());
    let mut copied = 0;
    let mut from = 0;
    while let Some(open) = next_double_dollar(content, from, &code) {
        let Some(close) = next_double_dollar(content, open + 2, &code) else {
            break;
        };
        let span = &content[open..close + 2];
        if span.contains('\n') {
            out.push_str(&content[copied..open]);
            let mut lines = span.lines().map(str::trim).filter(|line| !line.is_empty());
            if let Some(first) = lines.next() {
                out.push_str(first);
            }
            for line in lines {
                out.push(' ');
                out.push_str(line);
            }
            copied = close + 2;
        }
        from = close + 2;
    }
    out.push_str(&content[copied..]);
    out
}

/// The next `$$` at or after `from` that is neither escaped nor inside a
/// closed fenced code block.
fn next_double_dollar(content: &str, mut from: usize, code: &[(usize, usize)]) -> Option<usize> {
    let bytes = content.as_bytes();
    loop {
        let at = from + memchr::memmem::find(&bytes[from..], b"$$")?;
        if at > 0 && bytes[at - 1] == b'\\' {
            from = at + 1;
            continue;
        }
        if let Some(&(_, end)) = code.iter().find(|&&(start, end)| start <= at && at < end) {
            from = end;
            continue;
        }
        return Some(at);
    }
}

/// Whether `position` is inside `](...)` of a link/image.
pub fn is_within_link_or_image_url(text: &str, position: usize) -> bool {
    let bytes = text.as_bytes();
    let mut i = position;
    while i > 0 {
        i -= 1;
        match bytes[i] {
            b')' => return false,
            b'(' if i > 0 && bytes[i - 1] == b']' => return true,
            b'(' => return false,
            b'\n' => return false,
            _ => {}
        }
    }
    false
}

/// Whether `position` is inside an unclosed HTML tag (`<...>`).
pub fn is_within_html_tag(text: &str, position: usize) -> bool {
    let mut in_tag = false;
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < position && i < text.len() {
        if bytes[i] == b'<' && (i == 0 || bytes[i - 1] != b'\\') {
            in_tag = true;
        } else if bytes[i] == b'>' && in_tag && (i == 0 || bytes[i - 1] != b'\\') {
            in_tag = false;
        }
        i += 1;
    }
    in_tag
}

/// Append `suffix` before trailing whitespace.
pub fn append_before_trailing_whitespace(content: &str, suffix: &str) -> String {
    let ws = trailing_ws_offset(content);
    format!("{}{}{}", &content[..ws], suffix, &content[ws..])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn closed_code_ranges() {
        let s = "```js\ncode\n``` and `inline";
        let ranges = find_closed_code_block_ranges(s);
        assert_eq!(ranges, vec![(0, 14)]);
        assert!(is_position_in_ranges(5, &ranges));
        assert!(!is_position_in_ranges(20, &ranges));
    }

    #[test]
    fn underscore_after_multibyte_char_is_safe() {
        // 回归:`` `x`,_y_ `` 曾因 start-1 落在 ','(3 字节)中间 panic。
        let content = "`x`,_y_";
        let code = find_closed_code_block_ranges(content);
        let inline = find_inline_code_ranges(content, &code);
        let masked = mask_inline_code_markdown_markers(content, &inline);
        let star_pos = masked.find('_').unwrap();
        assert!(!should_ignore_underscore_marker(content, star_pos, 1));
    }

    #[test]
    fn underscore_inside_cjk_word_boundaries() {
        // 中文相邻 `_x_` 处理不 panic(多字节边界安全)。
        let content = "中文_x_结尾";
        let star_pos = content.find('_').unwrap();
        let _ = should_ignore_underscore_marker(content, star_pos, 1);
        assert!(content.is_char_boundary(star_pos + 1));
    }

    #[test]
    fn inline_code_ranges_skip_triples() {
        let s = "```a``` text `b` and `c";
        let ranges = find_inline_code_ranges(s, &find_closed_code_block_ranges(s));
        // `b` at ... and `c` unmatched -> single
        assert_eq!(ranges.len(), 1);
        assert_eq!(&s[ranges[0].0..ranges[0].1], "`b`");
    }

    #[test]
    fn last_paragraph_split() {
        let (line, off) = last_paragraph_range("Para1\n\nPara2 text", false);
        assert_eq!(line, 2);
        assert_eq!(&"Para1\n\nPara2 text"[off..], "Para2 text");
        // skipTrailingEmpty=true skips only ONE trailing empty line; a double
        // trailing newline leaves an empty last paragraph (matches the JS)
        let (line2, off2) = last_paragraph_range("Para1 **bold**\n\n", true);
        assert_eq!(line2, 2);
        assert_eq!(off2, 16);
        let (line3, _) = last_paragraph_range("Para1\n\nPara2\n", true);
        assert_eq!(line3, 2);
        let (line4, _) = last_paragraph_range("**Contribution\n", true);
        assert_eq!(line4, 0);
    }

    #[test]
    fn url_removal_keeps_label() {
        let s = "[text](https://example.com/page_with_underscore) more";
        let r = remove_urls_from_text(s);
        assert_eq!(r, "[text]() more");
    }

    #[test]
    fn incomplete_url_suffix() {
        let s = "Visit [Google](https://www.goo";
        let r = remove_urls_from_text(s);
        assert_eq!(r, "Visit [Google](");
    }

    #[test]
    fn math_removal() {
        let s = "a $$x = 1$$ b $$y = 2$$ c";
        assert_eq!(remove_math_blocks_from_text(s, false), "a  b  c");
        let s2 = "unclosed $$x";
        assert_eq!(remove_math_blocks_from_text(s2, false), "unclosed ");
    }

    #[test]
    fn underscore_inside_word_ignored() {
        let s = "snake_case text";
        assert!(should_ignore_underscore_marker(s, 5, 1));
        let s2 = "snake case _";
        assert!(!should_ignore_underscore_marker(s2, 10, 1));
    }

    #[test]
    fn append_before_trailing() {
        assert_eq!(
            append_before_trailing_whitespace("text  ", "**"),
            "text**  "
        );
        assert_eq!(append_before_trailing_whitespace("text", "**"), "text**");
    }

    #[test]
    fn backtick_after_cjk_does_not_panic() {
        // index 落在多字节字符(中文标点)之后:`text[..index-1]` 会跨 char 边界。
        let s = "中文:`code` 后";
        let tick = s.find('`').unwrap();
        let _ = is_backtick_part_of_triple(s, tick);
        let s2 = "注解``code``";
        let tick = s2.find('`').unwrap();
        let _ = is_backtick_part_of_triple(s2, tick);
    }

    #[test]
    fn within_link_url() {
        assert!(is_within_link_or_image_url("[t](https://a", 9));
        // position before the closing paren is still inside the URL
        assert!(is_within_link_or_image_url("[t](https://a)", 9));
        assert!(is_within_link_or_image_url("[t](https://a) mid", 9));
        assert!(!is_within_link_or_image_url("[t](https://a)", 14));
        assert!(!is_within_link_or_image_url("plain text", 4));
    }

    #[test]
    fn join_multiline_math_folds_a_span_onto_one_line() {
        assert_eq!(
            join_multiline_math("before\n\n$$ \\boxed{ Z[J]\n=\n\\int x\\,dx $$\n\nafter"),
            "before\n\n$$ \\boxed{ Z[J] = \\int x\\,dx $$\n\nafter"
        );
        assert_eq!(join_multiline_math("$$\nE = mc^2\n$$"), "$$ E = mc^2 $$");
        assert_eq!(
            join_multiline_math("$$ a\n\n  b  \n$$ and $$c\nd$$"),
            "$$ a b $$ and $$c d$$"
        );
    }

    #[test]
    fn join_multiline_math_leaves_the_rest_alone() {
        for untouched in [
            "no math here",
            "single line $$ a = b $$ stays",
            "unclosed $$ a\n= b",
            "escaped \\$$ a\n= b \\$$",
            "```\n$$\nx\n$$\n```",
        ] {
            assert_eq!(join_multiline_math(untouched), untouched);
        }
    }
}