Skip to main content

instruction_md/
inline.rs

1//! Inline syntax (§11.1, §12), read where CommonMark reads text — never inside code, raw HTML, an autolink, a link
2//! destination or title, or a definition:
3//!
4//! ```text
5//! :kind[label]{attrs}   typed references and annotations; after line start, whitespace or ( [ { < " '
6//! [[target|label]]      a navigation link; ![[target|label]] an embed
7//! [^label]              a footnote reference
8//! $formula$             inline math: no space inside either dollar, no digit after the closer
9//! ${name}               a parameter
10//! ```
11//!
12//! The reference reads these as micromark constructs (CommonMark, with GFM's tables, task list items and autolink
13//! literals). This module is a hand-written scanner with the same precedence: a block pass (containers, fenced and
14//! indented code, HTML blocks, definitions, headings, tables, paragraphs) finds the text, and a left-to-right inline
15//! pass tries, at each character, the constructs micromark tries there, in its order — escapes, code spans,
16//! autolinks and raw HTML, link labels and their resources and references, GFM autolink literals, and the forms
17//! above.
18
19use std::collections::{HashMap, HashSet};
20
21use crate::js::js_trim;
22use crate::read::RX;
23
24/// What an inline form is.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26pub enum TokenType {
27    Directive,
28    Wiki,
29    Embed,
30    Footnote,
31    Math,
32    Param,
33}
34
35impl TokenType {
36    pub fn as_str(self) -> &'static str {
37        match self {
38            TokenType::Directive => "directive",
39            TokenType::Wiki => "wiki",
40            TokenType::Embed => "embed",
41            TokenType::Footnote => "footnote",
42            TokenType::Math => "math",
43            TokenType::Param => "param",
44        }
45    }
46}
47
48/// One inline form, with its byte offsets into the source it was read from.
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub struct InlineToken {
51    pub token_type: TokenType,
52    pub start: usize,
53    pub end: usize,
54    /// A directive's kind.
55    pub kind: String,
56    /// A directive's label (and where it lies), a wiki-link's or embed's label, a footnote's label.
57    pub label: String,
58    pub label_start: usize,
59    pub label_end: usize,
60    /// A directive's attribute list's text between the braces; `None` without one.
61    pub attributes: Option<String>,
62    /// A wiki-link's or an embed's target; `piped`: it has a label too.
63    pub target: String,
64    pub piped: bool,
65    /// Inline math's formula.
66    pub formula: String,
67    /// A parameter's name.
68    pub name: String,
69}
70
71impl InlineToken {
72    fn new(token_type: TokenType, start: usize, end: usize) -> InlineToken {
73        InlineToken {
74            token_type,
75            start,
76            end,
77            kind: String::new(),
78            label: String::new(),
79            label_start: start,
80            label_end: start,
81            attributes: None,
82            target: String::new(),
83            piped: false,
84            formula: String::new(),
85            name: String::new(),
86        }
87    }
88}
89
90/// How deeply directive labels nest before the scanner stops looking inside them (a guard for hostile input).
91const MAX_LABEL_DEPTH: usize = 32;
92
93/// The inline forms in a Markdown source, outermost first.
94pub fn inline_tokens(source: &str) -> Vec<InlineToken> {
95    let blocks = Blocks::read(source);
96    let mut out = Vec::new();
97    for chunk in &blocks.chunks {
98        let (v, map) = chunk.text(source);
99        let closers = label_closers(&v);
100        let mut s = Scanner { v: &v, map: &map, source, defs: &blocks.defs, out: &mut out, cell: chunk.cell, closers, math_fails_until: 0 };
101        s.scan(0, v.len(), 0);
102    }
103    out
104}
105
106/// Splits a wiki-link's text at its first unescaped pipe into its target and label; `\|` is a literal pipe.
107pub fn split_wiki(inner: &str) -> (String, String, bool) {
108    let b = inner.as_bytes();
109    let mut i = 0;
110    while i < b.len() {
111        if b[i] == b'\\' {
112            i += 2;
113            continue;
114        }
115        if b[i] == b'|' {
116            return (unescape_wiki(&inner[..i]), unescape_wiki(&inner[i + 1..]), true);
117        }
118        i += 1;
119    }
120    (unescape_wiki(inner), String::new(), false)
121}
122
123fn unescape_wiki(s: &str) -> String {
124    let mut out = String::with_capacity(s.len());
125    let mut chars = s.chars().peekable();
126    while let Some(c) = chars.next() {
127        if c == '\\'
128            && let Some(&n) = chars.peek()
129            && matches!(n, '\\' | '|' | ']' | '[')
130        {
131            out.push(n);
132            chars.next();
133            continue;
134        }
135        out.push(c);
136    }
137    js_trim(&out).to_string()
138}
139
140// ---- the block pass ----
141
142/// A run of inline text: the source spans of its lines, joined by line breaks.
143struct Chunk {
144    spans: Vec<(usize, usize)>,
145    cell: bool,
146}
147
148impl Chunk {
149    /// The chunk's text, and each of its byte offsets' place in the source (one past the end too).
150    fn text(&self, source: &str) -> (String, Vec<usize>) {
151        let mut v = String::new();
152        let mut map = Vec::new();
153        for (k, &(a, b)) in self.spans.iter().enumerate() {
154            if k > 0 {
155                let prev_end = self.spans[k - 1].1;
156                v.push('\n');
157                map.push(prev_end);
158            }
159            v.push_str(&source[a..b]);
160            map.extend(a..b);
161        }
162        map.push(self.spans.last().map_or(0, |s| s.1));
163        (v, map)
164    }
165}
166
167#[derive(Clone, Copy, PartialEq, Debug)]
168enum Container {
169    Quote,
170    /// a list item: the width its content is indented by
171    Item(usize),
172}
173
174#[derive(PartialEq, Debug)]
175enum Leaf {
176    None,
177    Paragraph,
178    Fenced(u8, usize),
179    Indented,
180    /// an HTML block of CommonMark's kind 1 to 7
181    Html(u8),
182    Table,
183}
184
185struct Blocks {
186    chunks: Vec<Chunk>,
187    defs: HashSet<String>,
188}
189
190/// A line's indentation from byte p: its columns (tabs to stops of four) and where its content starts.
191fn indentation(line: &str, p: usize) -> (usize, usize) {
192    let b = line.as_bytes();
193    let (mut col, mut i) = (0, p);
194    while i < b.len() {
195        match b[i] {
196            b' ' => col += 1,
197            b'\t' => col += 4 - col % 4,
198            _ => break,
199        }
200        i += 1;
201    }
202    (col, i)
203}
204
205/// Consumes n columns of indentation from p (a tab that straddles them is consumed whole).
206fn consume_columns(line: &str, p: usize, n: usize) -> usize {
207    let b = line.as_bytes();
208    let (mut col, mut i) = (0, p);
209    while i < b.len() && col < n {
210        match b[i] {
211            b' ' => col += 1,
212            b'\t' => col += 4 - col % 4,
213            _ => break,
214        }
215        i += 1;
216    }
217    i
218}
219
220fn is_blank(s: &str) -> bool {
221    s.bytes().all(|c| c == b' ' || c == b'\t')
222}
223
224/// A list marker at p (after at most three spaces): (the content's indent width, ordered, start number is 1, blank
225/// after the marker, where the content starts).
226fn list_marker(line: &str, p: usize) -> Option<(usize, bool, bool, bool, usize)> {
227    let (ind, q) = indentation(line, p);
228    if ind > 3 {
229        return None;
230    }
231    let b = line.as_bytes();
232    let (marker_end, ordered, one) = match b.get(q) {
233        Some(b'-' | b'*' | b'+') => (q + 1, false, true),
234        Some(c) if c.is_ascii_digit() => {
235            let mut k = q;
236            while k < b.len() && b[k].is_ascii_digit() && k - q < 10 {
237                k += 1;
238            }
239            if k - q > 9 || !matches!(b.get(k), Some(b'.' | b')')) {
240                return None;
241            }
242            (k + 1, true, &line[q..k] == "1")
243        }
244        _ => return None,
245    };
246    if marker_end < b.len() && b[marker_end] != b' ' && b[marker_end] != b'\t' {
247        return None;
248    }
249    let rest = &line[marker_end..];
250    let width = ind + (marker_end - q);
251    if is_blank(rest) {
252        return Some((width + 1, ordered, one, true, line.len()));
253    }
254    let (spaces, content) = indentation(line, marker_end);
255    if spaces >= 5 {
256        return Some((width + 1, ordered, one, false, marker_end + 1));
257    }
258    Some((width + spaces.max(1), ordered, one, false, content))
259}
260
261/// The opening of a code fence at p: its character and length.
262fn fence_open(line: &str, p: usize) -> Option<(u8, usize)> {
263    let (ind, q) = indentation(line, p);
264    if ind > 3 {
265        return None;
266    }
267    let b = line.as_bytes();
268    let ch = *b.get(q)?;
269    if ch != b'`' && ch != b'~' {
270        return None;
271    }
272    let mut k = q;
273    while k < b.len() && b[k] == ch {
274        k += 1;
275    }
276    let len = k - q;
277    if len < 3 || ch == b'`' && line[k..].contains('`') {
278        return None;
279    }
280    Some((ch, len))
281}
282
283fn fence_close(line: &str, p: usize, ch: u8, len: usize) -> bool {
284    let (ind, q) = indentation(line, p);
285    if ind > 3 {
286        return false;
287    }
288    let b = line.as_bytes();
289    let mut k = q;
290    while k < b.len() && b[k] == ch {
291        k += 1;
292    }
293    k - q >= len && is_blank(&line[k..])
294}
295
296/// A thematic break: three or more of one of `-`, `*` or `_`, with spaces and tabs between (after at most three
297/// spaces).
298fn thematic(rest: &str) -> bool {
299    let (ind, q) = indentation(rest, 0);
300    if ind > 3 {
301        return false;
302    }
303    let b = rest.as_bytes();
304    let Some(&ch) = b.get(q).filter(|c| matches!(c, b'-' | b'*' | b'_')) else { return false };
305    let mut n = 0;
306    for &c in &b[q..] {
307        if c == ch {
308            n += 1;
309        } else if c != b' ' && c != b'\t' {
310            return false;
311        }
312    }
313    n >= 3
314}
315
316fn atx(rest: &str) -> bool {
317    RX.heading.is_match(rest)
318}
319
320/// The HTML block kind a line starts (1 to 7), if any.
321fn html_start(rest: &str, in_paragraph: bool) -> Option<u8> {
322    let (ind, q) = indentation(rest, 0);
323    if ind > 3 || !rest[q..].starts_with('<') {
324        return None;
325    }
326    let t = &rest[q..];
327    if RX.html_literals[0].0.is_match(rest) {
328        return Some(1);
329    }
330    if t.starts_with("<!--") {
331        return Some(2);
332    }
333    if t.starts_with("<?") {
334        return Some(3);
335    }
336    if t.starts_with("<![CDATA[") {
337        return Some(5);
338    }
339    if RX.html_literals[3].0.is_match(rest) {
340        return Some(4);
341    }
342    if RX.html_block.is_match(rest) {
343        return Some(6);
344    }
345    if !in_paragraph && RX.html_tag_line.is_match(rest) {
346        return Some(7);
347    }
348    None
349}
350
351fn html_ends(kind: u8, line: &str) -> bool {
352    match kind {
353        1 => RX.html_literals[0].1.is_match(line),
354        2 => line.contains("-->"),
355        3 => line.contains("?>"),
356        4 => line.contains('>'),
357        5 => line.contains("]]>"),
358        _ => false,
359    }
360}
361
362/// A GFM table's delimiter row: its cell count.
363fn delimiter_row(rest: &str) -> Option<usize> {
364    let t = rest.trim_matches([' ', '\t']);
365    if t.is_empty() || !t.contains('-') {
366        return None;
367    }
368    let t = t.strip_prefix('|').unwrap_or(t);
369    let t = t.strip_suffix('|').unwrap_or(t);
370    let mut n = 0;
371    for cell in t.split('|') {
372        let c = cell.trim_matches([' ', '\t']);
373        let c = c.strip_prefix(':').unwrap_or(c);
374        let c = c.strip_suffix(':').unwrap_or(c);
375        if c.is_empty() || !c.bytes().all(|b| b == b'-') {
376            return None;
377        }
378        n += 1;
379    }
380    if !rest.contains('|') && n == 1 {
381        // (a lone run of dashes is a setext underline or a thematic break)
382        return None;
383    }
384    Some(n)
385}
386
387/// A table row's cells: their spans in the line (from p).
388fn row_cells(line: &str, p: usize, base: usize) -> Vec<(usize, usize)> {
389    let b = line.as_bytes();
390    let mut start = p;
391    let mut end = b.len();
392    while start < end && (b[start] == b' ' || b[start] == b'\t') {
393        start += 1;
394    }
395    while end > start && (b[end - 1] == b' ' || b[end - 1] == b'\t') {
396        end -= 1;
397    }
398    if start < end && b[start] == b'|' {
399        start += 1;
400    }
401    if end > start && b[end - 1] == b'|' && !(end >= 2 + start && b[end - 2] == b'\\') {
402        end -= 1;
403    }
404    let mut cells = Vec::new();
405    let mut cell_start = start;
406    let mut i = start;
407    while i < end {
408        if b[i] == b'\\' {
409            i += 2;
410            continue;
411        }
412        if b[i] == b'|' {
413            cells.push((cell_start, i));
414            cell_start = i + 1;
415        }
416        i += 1;
417    }
418    cells.push((cell_start, end.max(cell_start)));
419    cells
420        .into_iter()
421        .map(|(a, z)| {
422            let (mut a, mut z) = (a, z.min(b.len()));
423            while a < z && (b[a] == b' ' || b[a] == b'\t') {
424                a += 1;
425            }
426            while z > a && (b[z - 1] == b' ' || b[z - 1] == b'\t') {
427                z -= 1;
428            }
429            (base + a, base + z)
430        })
431        .collect()
432}
433
434fn header_cells(line: &str) -> usize {
435    row_cells(line, 0, 0).len()
436}
437
438/// A link reference definition on one line: its normalized label.
439fn definition(rest: &str) -> Option<String> {
440    let (ind, q) = indentation(rest, 0);
441    if ind > 3 {
442        return None;
443    }
444    let b = rest.as_bytes();
445    if b.get(q) != Some(&b'[') {
446        return None;
447    }
448    let mut k = q + 1;
449    while k < b.len() && b[k] != b']' {
450        if b[k] == b'[' {
451            return None;
452        }
453        k += if b[k] == b'\\' { 2 } else { 1 };
454    }
455    if k >= b.len() || b.get(k + 1) != Some(&b':') {
456        return None;
457    }
458    let label = &rest[q + 1..k];
459    if label.trim().is_empty() || label.len() > 999 {
460        return None;
461    }
462    let mut i = k + 2;
463    while i < b.len() && (b[i] == b' ' || b[i] == b'\t') {
464        i += 1;
465    }
466    // the destination
467    if i >= b.len() {
468        return None;
469    }
470    if b[i] == b'<' {
471        i += 1;
472        while i < b.len() && b[i] != b'>' {
473            if b[i] == b'<' {
474                return None;
475            }
476            i += if b[i] == b'\\' { 2 } else { 1 };
477        }
478        if i >= b.len() {
479            return None;
480        }
481        i += 1;
482    } else {
483        let mut depth = 0i32;
484        let start = i;
485        while i < b.len() && b[i] > b' ' {
486            match b[i] {
487                b'\\' => i += 1,
488                b'(' => depth += 1,
489                b')' => {
490                    if depth == 0 {
491                        break;
492                    }
493                    depth -= 1;
494                }
495                _ => {}
496            }
497            i += 1;
498        }
499        if i == start || depth != 0 {
500            return None;
501        }
502    }
503    let after_dest = i;
504    while i < b.len() && (b[i] == b' ' || b[i] == b'\t') {
505        i += 1;
506    }
507    if i < b.len() {
508        if i == after_dest {
509            return None;
510        }
511        let close = match b[i] {
512            b'"' => b'"',
513            b'\'' => b'\'',
514            b'(' => b')',
515            _ => return None,
516        };
517        i += 1;
518        while i < b.len() && b[i] != close {
519            i += if b[i] == b'\\' { 2 } else { 1 };
520        }
521        if i >= b.len() {
522            return None;
523        }
524        if !is_blank(&rest[i + 1..]) {
525            return None;
526        }
527    }
528    Some(normalize_label(label))
529}
530
531fn normalize_label(s: &str) -> String {
532    s.split_whitespace().collect::<Vec<_>>().join(" ").to_lowercase()
533}
534
535impl Blocks {
536    fn read(source: &str) -> Blocks {
537        let mut b = Blocks { chunks: Vec::new(), defs: HashSet::new() };
538        let mut containers: Vec<Container> = Vec::new();
539        let mut leaf = Leaf::None;
540        let mut para: Vec<(usize, usize)> = Vec::new();
541        let mut table_cols = 0;
542        let mut offset = 0;
543        let bom = if source.starts_with('\u{feff}') { 3 } else { 0 };
544        for raw_line in source.split_inclusive('\n') {
545            let line_start = offset;
546            offset += raw_line.len();
547            let line = raw_line.trim_end_matches('\n').trim_end_matches('\r');
548            let skip = if line_start == 0 { bom.min(line.len()) } else { 0 };
549            let line_base = line_start;
550            // the containers this line continues
551            let mut p = skip;
552            let mut matched = 0;
553            for c in &containers {
554                match c {
555                    Container::Quote => {
556                        let (ind, q) = indentation(line, p);
557                        if ind <= 3 && line.as_bytes().get(q) == Some(&b'>') {
558                            p = q + 1;
559                            if matches!(line.as_bytes().get(p), Some(b' ' | b'\t')) {
560                                p += 1;
561                            }
562                            matched += 1;
563                        } else {
564                            break;
565                        }
566                    }
567                    Container::Item(width) => {
568                        if is_blank(&line[p..]) {
569                            matched += 1;
570                        } else if indentation(line, p).0 >= *width {
571                            p = consume_columns(line, p, *width);
572                            matched += 1;
573                        } else {
574                            break;
575                        }
576                    }
577                }
578            }
579            let all = matched == containers.len();
580            if all {
581                if let Leaf::Fenced(ch, len) = leaf {
582                    if fence_close(line, p, ch, len) {
583                        leaf = Leaf::None;
584                    }
585                    continue;
586                }
587                if let Leaf::Html(kind) = leaf {
588                    if kind <= 5 {
589                        if html_ends(kind, &line[p..]) {
590                            leaf = Leaf::None;
591                        }
592                        continue;
593                    }
594                    if is_blank(&line[p..]) {
595                        leaf = Leaf::None;
596                    } else {
597                        continue;
598                    }
599                }
600            }
601            let rest = &line[p..];
602            // a lazy continuation line of a paragraph
603            if !all && leaf == Leaf::Paragraph && !is_blank(rest) && !Self::interrupts(rest, true) {
604                para.push(Self::text_span(line, p, line_base));
605                continue;
606            }
607            if !all {
608                b.finish(&mut leaf, &mut para);
609                containers.truncate(matched);
610            }
611            // new containers
612            loop {
613                let rest = &line[p..];
614                let (ind, q) = indentation(line, p);
615                if ind > 3 {
616                    break;
617                }
618                if line.as_bytes().get(q) == Some(&b'>') {
619                    b.finish(&mut leaf, &mut para);
620                    containers.push(Container::Quote);
621                    p = q + 1;
622                    if matches!(line.as_bytes().get(p), Some(b' ' | b'\t')) {
623                        p += 1;
624                    }
625                    continue;
626                }
627                if thematic(rest) {
628                    break;
629                }
630                if let Some((width, ordered, one, blank_after, content)) = list_marker(line, p) {
631                    if leaf == Leaf::Paragraph && (blank_after || ordered && !one) {
632                        break;
633                    }
634                    b.finish(&mut leaf, &mut para);
635                    containers.push(Container::Item(width));
636                    p = content;
637                    continue;
638                }
639                break;
640            }
641            let rest = &line[p..];
642            if is_blank(rest) {
643                if leaf != Leaf::Indented {
644                    b.finish(&mut leaf, &mut para);
645                }
646                continue;
647            }
648            let (ind, _) = indentation(line, p);
649            if leaf == Leaf::Indented {
650                if ind >= 4 {
651                    continue;
652                }
653                leaf = Leaf::None;
654            }
655            if leaf == Leaf::Table {
656                if Self::interrupts(rest, false) {
657                    leaf = Leaf::None;
658                } else {
659                    for span in row_cells(line, p, line_base) {
660                        b.chunks.push(Chunk { spans: vec![span], cell: true });
661                    }
662                    continue;
663                }
664            }
665            if ind >= 4 && leaf != Leaf::Paragraph {
666                leaf = Leaf::Indented;
667                continue;
668            }
669            if let Some((ch, len)) = fence_open(line, p) {
670                b.finish(&mut leaf, &mut para);
671                leaf = Leaf::Fenced(ch, len);
672                continue;
673            }
674            if let Some(kind) = html_start(rest, leaf == Leaf::Paragraph) {
675                b.finish(&mut leaf, &mut para);
676                leaf = Leaf::Html(kind);
677                if kind <= 5 && html_ends(kind, rest) {
678                    leaf = Leaf::None;
679                }
680                continue;
681            }
682            if atx(rest) {
683                b.finish(&mut leaf, &mut para);
684                b.chunks.push(Chunk { spans: vec![Self::heading_span(line, p, line_base)], cell: false });
685                continue;
686            }
687            if leaf == Leaf::Paragraph && Self::setext(rest) {
688                b.finish(&mut leaf, &mut para);
689                continue;
690            }
691            if thematic(rest) {
692                b.finish(&mut leaf, &mut para);
693                continue;
694            }
695            if leaf == Leaf::Paragraph
696                && para.len() == 1
697                && let Some(n) = delimiter_row(rest)
698            {
699                let (a, z) = para[0];
700                if header_cells(&source[a..z]) == n {
701                    for span in row_cells(&source[a..z], 0, a) {
702                        b.chunks.push(Chunk { spans: vec![span], cell: true });
703                    }
704                    para.clear();
705                    leaf = Leaf::Table;
706                    table_cols = n;
707                    continue;
708                }
709            }
710            if leaf != Leaf::Paragraph {
711                leaf = Leaf::Paragraph;
712                para.clear();
713            }
714            if para.is_empty()
715                && let Some(label) = definition(rest)
716            {
717                b.defs.insert(label);
718                continue;
719            }
720            para.push(Self::text_span(line, p, line_base));
721        }
722        let _ = table_cols;
723        b.finish(&mut leaf, &mut para);
724        b
725    }
726
727    /// A paragraph line's text: its leading whitespace left out.
728    fn text_span(line: &str, p: usize, base: usize) -> (usize, usize) {
729        let (_, q) = indentation(line, p);
730        (base + q, base + line.len())
731    }
732
733    /// An ATX heading's content: without its opening and closing sequences.
734    fn heading_span(line: &str, p: usize, base: usize) -> (usize, usize) {
735        let b = line.as_bytes();
736        let (_, mut q) = indentation(line, p);
737        while q < b.len() && b[q] == b'#' {
738            q += 1;
739        }
740        while q < b.len() && (b[q] == b' ' || b[q] == b'\t') {
741            q += 1;
742        }
743        let mut end = b.len();
744        while end > q && (b[end - 1] == b' ' || b[end - 1] == b'\t') {
745            end -= 1;
746        }
747        let mut k = end;
748        while k > q && b[k - 1] == b'#' {
749            k -= 1;
750        }
751        if k < end && (k == q || b[k - 1] == b' ' || b[k - 1] == b'\t') {
752            end = k;
753            while end > q && (b[end - 1] == b' ' || b[end - 1] == b'\t') {
754                end -= 1;
755            }
756        }
757        (base + q, base + end.max(q))
758    }
759
760    fn setext(rest: &str) -> bool {
761        let (ind, q) = indentation(rest, 0);
762        if ind > 3 {
763            return false;
764        }
765        let t = rest[q..].trim_end_matches([' ', '\t']);
766        !t.is_empty() && (t.bytes().all(|c| c == b'=') || t.bytes().all(|c| c == b'-'))
767    }
768
769    /// Does the line start a block that ends a paragraph (and a table)?
770    fn interrupts(rest: &str, paragraph: bool) -> bool {
771        let (ind, q) = indentation(rest, 0);
772        if ind > 3 {
773            return false;
774        }
775        let b = rest.as_bytes();
776        b.get(q) == Some(&b'>')
777            || atx(rest)
778            || thematic(rest)
779            || fence_open(rest, 0).is_some()
780            || html_start(rest, paragraph).is_some()
781            || list_marker(rest, 0).is_some_and(|(_, ordered, one, blank_after, _)| !blank_after && (!ordered || one))
782    }
783
784    fn finish(&mut self, leaf: &mut Leaf, para: &mut Vec<(usize, usize)>) {
785        if *leaf == Leaf::Paragraph && !para.is_empty() {
786            self.chunks.push(Chunk { spans: std::mem::take(para), cell: false });
787        }
788        para.clear();
789        if matches!(*leaf, Leaf::Paragraph | Leaf::Table) {
790            *leaf = Leaf::None;
791        }
792    }
793}
794
795// ---- the inline pass ----
796
797struct LabelStart {
798    image: bool,
799    inactive: bool,
800    /// where the label's text starts (after `[` or `![`)
801    text: usize,
802}
803
804struct Scanner<'a> {
805    v: &'a str,
806    map: &'a [usize],
807    source: &'a str,
808    defs: &'a HashSet<String>,
809    out: &'a mut Vec<InlineToken>,
810    cell: bool,
811    /// each `[`'s closing `]` as a directive's label reads it (balanced, on its line): computed once, so that no
812    /// line of openers makes the scan quadratic
813    closers: HashMap<usize, usize>,
814    /// a math scan from an earlier `$` ran to its line's end without a closer (and without escapes): every `$`
815    /// before here fails too
816    math_fails_until: usize,
817}
818
819/// Each `[`'s closing `]` as a directive label reads it: brackets balanced, `\[`, `\]` and `\\` escaped, within a
820/// line.
821fn label_closers(v: &str) -> HashMap<usize, usize> {
822    let b = v.as_bytes();
823    let mut out = HashMap::new();
824    let mut open: Vec<usize> = Vec::new();
825    let mut i = 0;
826    while i < b.len() {
827        match b[i] {
828            b'\\' if i + 1 < b.len() && matches!(b[i + 1], b'[' | b']' | b'\\') => i += 1,
829            b'[' => open.push(i),
830            b']' => {
831                if let Some(o) = open.pop() {
832                    out.insert(o, i);
833                }
834            }
835            b'\n' | b'\r' => open.clear(),
836            _ => {}
837        }
838        i += 1;
839    }
840    out
841}
842
843fn byte(s: &str, i: usize) -> u8 {
844    s.as_bytes().get(i).copied().unwrap_or(0)
845}
846
847/// `spaced` in micromark's sense: the start, a space, a tab or a line ending.
848fn spaced(prev: Option<char>) -> bool {
849    matches!(prev, None | Some(' ' | '\t' | '\n' | '\r'))
850}
851
852fn ascii_punctuation(c: u8) -> bool {
853    c.is_ascii_punctuation()
854}
855
856impl Scanner<'_> {
857    fn src(&self, i: usize) -> usize {
858        self.map.get(i).copied().unwrap_or(0)
859    }
860
861    /// Scans v[from..to] (a chunk, or a directive's label) for the inline forms.
862    fn scan(&mut self, from: usize, to: usize, depth: usize) {
863        let v = self.v;
864        let b = v.as_bytes();
865        let mut i = from;
866        let mut prev: Option<char> = None;
867        let mut labels: Vec<LabelStart> = Vec::new();
868        // (how many label starts are open and active: an autolink literal may not start inside one)
869        let mut active = 0usize;
870        while i < to {
871            let c = b[i];
872            let at = i;
873            let next = match c {
874                b'\\' => {
875                    if i + 1 < to && ascii_punctuation(b[i + 1]) {
876                        Some((i + 2, b[i + 1] as char))
877                    } else {
878                        None
879                    }
880                }
881                b'`' => self.code_span(i, to),
882                b'<' => self.autolink(i, to).or_else(|| self.html(i, to)),
883                b':' if spaced(prev) || matches!(prev, Some('(' | '[' | '{' | '<' | '"' | '\'')) => self.directive(i, to, depth),
884                b'[' => {
885                    if let Some(n) = self.footnote(i, to).or_else(|| self.wiki(i, to, false)) {
886                        Some(n)
887                    } else {
888                        labels.push(LabelStart { image: false, inactive: false, text: i + 1 });
889                        active += 1;
890                        Some((i + 1, '['))
891                    }
892                }
893                b'!' => {
894                    if let Some(n) = self.wiki(i, to, true) {
895                        Some(n)
896                    } else if byte(v, i + 1) == b'[' && i + 1 < to {
897                        labels.push(LabelStart { image: true, inactive: false, text: i + 2 });
898                        active += 1;
899                        Some((i + 2, '['))
900                    } else {
901                        None
902                    }
903                }
904                b']' => self.label_end(i, to, &mut labels, &mut active),
905                b'$' => self.param(i, to).or_else(|| self.math(i, to)),
906                b'h' | b'H' if !prev.is_some_and(|p| p.is_ascii_alphabetic()) && active == 0 => self.protocol_autolink(i, to),
907                b'w' | b'W' if (spaced(prev) || matches!(prev, Some('(' | '*' | '_' | '[' | ']' | '~'))) && active == 0 => {
908                    self.www_autolink(i, to)
909                }
910                _ => None,
911            };
912            match next {
913                Some((n, last)) if n > at => {
914                    i = n;
915                    prev = Some(last);
916                }
917                _ => {
918                    let ch = v[i..].chars().next().unwrap_or(' ');
919                    i += ch.len_utf8();
920                    prev = Some(ch);
921                }
922            }
923        }
924    }
925
926    /// A code span at i: past its closing run, or none (the run is text).
927    fn code_span(&self, i: usize, to: usize) -> Option<(usize, char)> {
928        let b = self.v.as_bytes();
929        let mut n = 0;
930        while i + n < to && b[i + n] == b'`' {
931            n += 1;
932        }
933        let mut k = i + n;
934        while k < to {
935            if b[k] == b'`' {
936                let mut m = 0;
937                while k + m < to && b[k + m] == b'`' {
938                    m += 1;
939                }
940                if m == n {
941                    return Some((k + m, '`'));
942                }
943                k += m;
944            } else {
945                k += 1;
946            }
947        }
948        // no closing run: the opening run is text
949        Some((i + n, '`'))
950    }
951
952    /// An autolink, `<scheme:…>` or `<address@domain>`.
953    fn autolink(&self, i: usize, to: usize) -> Option<(usize, char)> {
954        let b = self.v.as_bytes();
955        let mut k = i + 1;
956        // a scheme
957        let start = k;
958        while k < to && (b[k].is_ascii_alphanumeric() || matches!(b[k], b'+' | b'.' | b'-')) {
959            k += 1;
960        }
961        if k - start >= 2 && k - start <= 32 && b[start].is_ascii_alphabetic() && k < to && b[k] == b':' {
962            k += 1;
963            while k < to && b[k] > b' ' && b[k] != b'<' && b[k] != b'>' && b[k] != 0x7f {
964                k += 1;
965            }
966            if k < to && b[k] == b'>' {
967                return Some((k + 1, '>'));
968            }
969            return None;
970        }
971        // an email address
972        let mut k = i + 1;
973        let local = k;
974        while k < to && (b[k].is_ascii_alphanumeric() || b".!#$%&'*+/=?^_`{|}~-".contains(&b[k])) {
975            k += 1;
976        }
977        if k == local || k >= to || b[k] != b'@' {
978            return None;
979        }
980        k += 1;
981        loop {
982            let label = k;
983            while k < to && (b[k].is_ascii_alphanumeric() || b[k] == b'-') && k - label < 63 {
984                k += 1;
985            }
986            if k == label {
987                return None;
988            }
989            if k < to && b[k] == b'.' {
990                k += 1;
991                continue;
992            }
993            break;
994        }
995        if k < to && b[k] == b'>' { Some((k + 1, '>')) } else { None }
996    }
997
998    /// Raw inline HTML: a tag, a closing tag, a comment, a processing instruction, a declaration, a CDATA section.
999    fn html(&self, i: usize, to: usize) -> Option<(usize, char)> {
1000        let v = &self.v[..to];
1001        let b = v.as_bytes();
1002        let rest = &v[i..];
1003        let find = |pat: &str, from: usize| v[from..].find(pat).map(|p| from + p);
1004        if rest.starts_with("<!--") {
1005            if rest.starts_with("<!-->") {
1006                return Some((i + 5, '>'));
1007            }
1008            if rest.starts_with("<!--->") {
1009                return Some((i + 6, '>'));
1010            }
1011            return find("-->", i + 4).map(|e| (e + 3, '>'));
1012        }
1013        if rest.starts_with("<?") {
1014            return find("?>", i + 2).map(|e| (e + 2, '>'));
1015        }
1016        if rest.starts_with("<![CDATA[") {
1017            return find("]]>", i + 9).map(|e| (e + 3, '>'));
1018        }
1019        if rest.starts_with("<!") {
1020            if !byte(v, i + 2).is_ascii_alphabetic() {
1021                return None;
1022            }
1023            return find(">", i + 2).map(|e| (e + 1, '>'));
1024        }
1025        let ws = |c: u8| matches!(c, b' ' | b'\t' | b'\n' | b'\r');
1026        if rest.starts_with("</") {
1027            let mut k = i + 2;
1028            if !byte(v, k).is_ascii_alphabetic() {
1029                return None;
1030            }
1031            while k < b.len() && (b[k].is_ascii_alphanumeric() || b[k] == b'-') {
1032                k += 1;
1033            }
1034            while k < b.len() && ws(b[k]) {
1035                k += 1;
1036            }
1037            return (byte(v, k) == b'>').then_some((k + 1, '>'));
1038        }
1039        // an open tag
1040        let mut k = i + 1;
1041        if !byte(v, k).is_ascii_alphabetic() {
1042            return None;
1043        }
1044        while k < b.len() && (b[k].is_ascii_alphanumeric() || b[k] == b'-') {
1045            k += 1;
1046        }
1047        loop {
1048            let before = k;
1049            while k < b.len() && ws(b[k]) {
1050                k += 1;
1051            }
1052            match byte(v, k) {
1053                b'>' => return Some((k + 1, '>')),
1054                b'/' if byte(v, k + 1) == b'>' => return Some((k + 2, '>')),
1055                c if (c.is_ascii_alphabetic() || c == b'_' || c == b':') && k > before => {
1056                    while k < b.len() && (b[k].is_ascii_alphanumeric() || matches!(b[k], b'_' | b'.' | b':' | b'-')) {
1057                        k += 1;
1058                    }
1059                    let save = k;
1060                    while k < b.len() && ws(b[k]) {
1061                        k += 1;
1062                    }
1063                    if byte(v, k) == b'=' {
1064                        k += 1;
1065                        while k < b.len() && ws(b[k]) {
1066                            k += 1;
1067                        }
1068                        match byte(v, k) {
1069                            q @ (b'"' | b'\'') => {
1070                                let close = v[k + 1..].find(q as char)?;
1071                                k = k + 1 + close + 1;
1072                            }
1073                            _ => {
1074                                let s = k;
1075                                while k < b.len() && !ws(b[k]) && !b"\"'=<>`".contains(&b[k]) {
1076                                    k += 1;
1077                                }
1078                                if k == s {
1079                                    return None;
1080                                }
1081                            }
1082                        }
1083                    } else {
1084                        k = save;
1085                    }
1086                }
1087                _ => return None,
1088            }
1089        }
1090    }
1091
1092    /// `:kind[label]{attrs}` at i.
1093    fn directive(&mut self, i: usize, to: usize, depth: usize) -> Option<(usize, char)> {
1094        let v = self.v;
1095        let b = v.as_bytes();
1096        let mut k = i + 1;
1097        if !(k < to && b[k].is_ascii_lowercase()) {
1098            return None;
1099        }
1100        let name_start = k;
1101        let mut qualified = false;
1102        loop {
1103            while k < to && (b[k].is_ascii_lowercase() || b[k].is_ascii_digit() || b[k] == b'-') {
1104                k += 1;
1105            }
1106            if k < to && b[k] == b':' && !qualified {
1107                qualified = true;
1108                k += 1;
1109                if !(k < to && b[k].is_ascii_lowercase()) {
1110                    return None;
1111                }
1112                continue;
1113            }
1114            break;
1115        }
1116        if !(k < to && b[k] == b'[') {
1117            return None;
1118        }
1119        let name_end = k;
1120        let label_open = k;
1121        let label_start = k + 1;
1122        k = match self.closers.get(&label_open) {
1123            Some(&close) if close < to => close,
1124            _ => return None,
1125        };
1126        let label_end = k;
1127        k += 1;
1128        let mut end = k;
1129        let mut attributes = None;
1130        if k < to
1131            && b[k] == b'{'
1132            && let Some(close) = Self::inline_attributes(v, k, to)
1133        {
1134            attributes = Some((k + 1, close - 1));
1135            end = close;
1136        }
1137        let mut t = InlineToken::new(TokenType::Directive, self.src(i), self.src(end));
1138        t.kind = self.source[self.src(name_start)..self.src(name_end)].to_string();
1139        if label_end > label_start {
1140            t.label_start = self.src(label_start);
1141            t.label_end = self.src(label_end);
1142            t.label = self.source[t.label_start..t.label_end].to_string();
1143        } else {
1144            t.label_start = self.src(label_open) + 1;
1145            t.label_end = t.label_start;
1146        }
1147        if let Some((a, z)) = attributes {
1148            t.attributes = Some(self.source[self.src(a)..self.src(z)].to_string());
1149        }
1150        self.out.push(t);
1151        if label_end > label_start && depth < MAX_LABEL_DEPTH {
1152            self.scan(label_start, label_end, depth + 1);
1153        }
1154        let last = if attributes.is_some() { '}' } else { ']' };
1155        Some((end, last))
1156    }
1157
1158    /// `{…}` on the same line from k; a quoted value may hold a brace: where it closes, exclusive.
1159    fn inline_attributes(v: &str, k: usize, to: usize) -> Option<usize> {
1160        let b = v.as_bytes();
1161        let mut j = k + 1;
1162        let mut quoted = false;
1163        while j < to {
1164            let c = b[j];
1165            if c == b'\n' || c == b'\r' {
1166                return None;
1167            }
1168            if quoted {
1169                if c == b'\\' {
1170                    if j + 1 >= to || b[j + 1] == b'\n' || b[j + 1] == b'\r' {
1171                        return None;
1172                    }
1173                    j += 2;
1174                    continue;
1175                }
1176                if c == b'"' {
1177                    quoted = false;
1178                }
1179                j += 1;
1180                continue;
1181            }
1182            match c {
1183                b'{' => return None,
1184                b'"' => quoted = true,
1185                b'}' => return Some(j + 1),
1186                _ => {}
1187            }
1188            j += 1;
1189        }
1190        None
1191    }
1192
1193    /// `[^label]` at i.
1194    fn footnote(&mut self, i: usize, to: usize) -> Option<(usize, char)> {
1195        let b = self.v.as_bytes();
1196        if !(i + 1 < to && b[i + 1] == b'^') {
1197            return None;
1198        }
1199        let mut k = i + 2;
1200        if !(k < to && b[k].is_ascii_alphanumeric()) {
1201            return None;
1202        }
1203        while k < to && (b[k].is_ascii_alphanumeric() || matches!(b[k], b'.' | b'_' | b'-')) {
1204            k += 1;
1205        }
1206        if !(k < to && b[k] == b']') {
1207            return None;
1208        }
1209        k += 1;
1210        if k < to && (b[k] == b'(' || b[k] == b':') {
1211            return None;
1212        }
1213        let (s, e) = (self.src(i), self.src(k));
1214        let mut t = InlineToken::new(TokenType::Footnote, s, e);
1215        t.label = self.source[s + 2..e - 1].to_string();
1216        self.out.push(t);
1217        Some((k, ']'))
1218    }
1219
1220    /// `[[target|label]]`, or `![[…]]` (an embed), at i.
1221    fn wiki(&mut self, i: usize, to: usize, embed: bool) -> Option<(usize, char)> {
1222        let b = self.v.as_bytes();
1223        let open = if embed { i + 1 } else { i };
1224        if !(open + 1 < to && b[open] == b'[' && b[open + 1] == b'[') || embed && b[i] != b'!' {
1225            return None;
1226        }
1227        let mut k = open + 2;
1228        let mut empty = true;
1229        loop {
1230            if k >= to || b[k] == b'\n' || b[k] == b'\r' || b[k] == b'[' {
1231                return None;
1232            }
1233            if b[k] == b']' {
1234                if empty {
1235                    return None;
1236                }
1237                break;
1238            }
1239            empty = false;
1240            if b[k] == b'\\' {
1241                k += 1;
1242                if k >= to || b[k] == b'\n' || b[k] == b'\r' {
1243                    return None;
1244                }
1245            }
1246            k += 1;
1247        }
1248        if !(k + 1 < to && b[k + 1] == b']') {
1249            return None;
1250        }
1251        let end = k + 2;
1252        // an ordinary link comes first: `[[x]](url)` is a link whose text is [x]
1253        if end < to && b[end] == b'(' {
1254            return None;
1255        }
1256        let (s, e) = (self.src(i), self.src(end));
1257        let raw = &self.source[s..e];
1258        let mut inner = if embed { raw[3..raw.len() - 2].to_string() } else { raw[2..raw.len() - 2].to_string() };
1259        // in a table cell the separator pipe is escaped for the cell tokenizer, which removes that one layer
1260        if self.cell {
1261            inner = inner.replace("\\|", "|");
1262        }
1263        let (target, label, piped) = split_wiki(&inner);
1264        let mut t = InlineToken::new(if embed { TokenType::Embed } else { TokenType::Wiki }, s, e);
1265        t.target = target;
1266        t.label = label;
1267        t.piped = piped;
1268        self.out.push(t);
1269        Some((end, ']'))
1270    }
1271
1272    /// `${name}` at i.
1273    fn param(&mut self, i: usize, to: usize) -> Option<(usize, char)> {
1274        let b = self.v.as_bytes();
1275        if !(i + 1 < to && b[i + 1] == b'{') {
1276            return None;
1277        }
1278        let mut k = i + 2;
1279        if !(k < to && (b[k].is_ascii_alphabetic() || b[k] == b'_')) {
1280            return None;
1281        }
1282        while k < to && (b[k].is_ascii_alphanumeric() || matches!(b[k], b'_' | b'.' | b'-')) {
1283            k += 1;
1284        }
1285        if !(k < to && b[k] == b'}') {
1286            return None;
1287        }
1288        k += 1;
1289        let (s, e) = (self.src(i), self.src(k));
1290        let mut t = InlineToken::new(TokenType::Param, s, e);
1291        t.name = self.source[s + 2..e - 1].to_string();
1292        self.out.push(t);
1293        Some((k, '}'))
1294    }
1295
1296    /// `$formula$` at i.
1297    fn math(&mut self, i: usize, to: usize) -> Option<(usize, char)> {
1298        let v = self.v;
1299        let b = v.as_bytes();
1300        if i < self.math_fails_until {
1301            return None;
1302        }
1303        let first = *b.get(i + 1).filter(|_| i + 1 < to)?;
1304        if first == b' ' || first == b'\t' || first == b'\n' || first == b'\r' || first.is_ascii_digit() || first == b'$' {
1305            return None;
1306        }
1307        let mut k = i + 1;
1308        let mut previous: Option<char> = None;
1309        let mut escaped = false;
1310        while k < to {
1311            let c = b[k];
1312            if c == b'\n' || c == b'\r' {
1313                if !escaped {
1314                    self.math_fails_until = k;
1315                }
1316                return None;
1317            }
1318            escaped = escaped || c == b'\\';
1319            if c == b'$' && !spaced(previous) {
1320                // the closer is not followed by a digit — which keeps `$5 and $10` ordinary text
1321                let after = if k + 1 < to { b[k + 1] } else { 0 };
1322                if !after.is_ascii_digit() {
1323                    let end = k + 1;
1324                    let (s, e) = (self.src(i), self.src(end));
1325                    let mut t = InlineToken::new(TokenType::Math, s, e);
1326                    t.formula = self.source[s + 1..e - 1].to_string();
1327                    self.out.push(t);
1328                    return Some((end, '$'));
1329                }
1330            }
1331            let ch = v[k..].chars().next().unwrap_or(' ');
1332            previous = Some(ch);
1333            k += ch.len_utf8();
1334            if c == b'\\' {
1335                if k >= to || b[k] == b'\n' || b[k] == b'\r' {
1336                    return None;
1337                }
1338                let ch = v[k..].chars().next().unwrap_or(' ');
1339                previous = Some(ch);
1340                k += ch.len_utf8();
1341            }
1342        }
1343        if !escaped {
1344            self.math_fails_until = to;
1345        }
1346        None
1347    }
1348
1349    /// `]`: the end of a label — a link or an image with its resource or reference, or text.
1350    fn label_end(&mut self, i: usize, to: usize, labels: &mut Vec<LabelStart>, active: &mut usize) -> Option<(usize, char)> {
1351        let start = labels.pop()?;
1352        if start.inactive {
1353            return None;
1354        }
1355        *active -= 1;
1356        let v = self.v;
1357        let b = v.as_bytes();
1358        let after = i + 1;
1359        let mut link_end = None;
1360        if after < to && b[after] == b'(' {
1361            link_end = self.resource(after, to);
1362        }
1363        let defined = |label: &str| label.len() <= 999 && self.defs.contains(&normalize_label(label));
1364        if self.defs.is_empty() {
1365            // (no definitions: a reference names nothing)
1366        } else if link_end.is_none() && after < to && b[after] == b'[' {
1367            // a full or collapsed reference
1368            let horizon = (after + 1 + 1001).min(to);
1369            if let Some(close) = v.as_bytes()[after + 1..horizon].iter().position(|&c| c == b']').map(|p| after + 1 + p) {
1370                let inner = &v[after + 1..close];
1371                let label = if inner.is_empty() { &v[start.text..i] } else { inner };
1372                if !inner.contains('[') && defined(label) {
1373                    link_end = Some(close + 1);
1374                }
1375            }
1376        }
1377        if link_end.is_none() && !self.defs.is_empty() && defined(&v[start.text..i]) {
1378            link_end = Some(after);
1379        }
1380        let end = link_end?;
1381        if !start.image {
1382            // no links in links
1383            for l in labels.iter_mut() {
1384                if !l.image && !l.inactive {
1385                    l.inactive = true;
1386                    *active -= 1;
1387                }
1388            }
1389        }
1390        Some((end, byte(v, end - 1) as char))
1391    }
1392
1393    /// A link resource `(destination "title")` from k (at `(`): where it ends, exclusive.
1394    fn resource(&self, k: usize, to: usize) -> Option<usize> {
1395        let b = self.v.as_bytes();
1396        let ws = |c: u8| matches!(c, b' ' | b'\t' | b'\n' | b'\r');
1397        let mut j = k + 1;
1398        let skip_ws = |j: &mut usize| {
1399            let mut breaks = 0;
1400            while *j < to && ws(b[*j]) {
1401                if b[*j] == b'\n' {
1402                    breaks += 1;
1403                }
1404                *j += 1;
1405            }
1406            breaks <= 1
1407        };
1408        if !skip_ws(&mut j) {
1409            return None;
1410        }
1411        if j < to && b[j] == b')' {
1412            return Some(j + 1);
1413        }
1414        // the destination
1415        if j < to && b[j] == b'<' {
1416            j += 1;
1417            while j < to && b[j] != b'>' {
1418                if b[j] == b'<' || b[j] == b'\n' || b[j] == b'\r' {
1419                    return None;
1420                }
1421                j += if b[j] == b'\\' { 2 } else { 1 };
1422            }
1423            if j >= to {
1424                return None;
1425            }
1426            j += 1;
1427        } else {
1428            let mut depth = 0;
1429            let start = j;
1430            while j < to && b[j] > b' ' && b[j] != 0x7f {
1431                match b[j] {
1432                    b'\\' => j += 1,
1433                    b'(' => {
1434                        depth += 1;
1435                        if depth > 32 {
1436                            return None;
1437                        }
1438                    }
1439                    b')' => {
1440                        if depth == 0 {
1441                            break;
1442                        }
1443                        depth -= 1;
1444                    }
1445                    _ => {}
1446                }
1447                j += 1;
1448            }
1449            if j == start || depth != 0 {
1450                return None;
1451            }
1452        }
1453        let before_ws = j;
1454        if !skip_ws(&mut j) {
1455            return None;
1456        }
1457        if j < to && j > before_ws && matches!(b[j], b'"' | b'\'' | b'(') {
1458            let close = if b[j] == b'(' { b')' } else { b[j] };
1459            j += 1;
1460            while j < to && b[j] != close {
1461                if b[j] == b'(' && close == b')' {
1462                    return None;
1463                }
1464                j += if b[j] == b'\\' { 2 } else { 1 };
1465            }
1466            if j >= to {
1467                return None;
1468            }
1469            j += 1;
1470            if !skip_ws(&mut j) {
1471                return None;
1472            }
1473        }
1474        if j < to && b[j] == b')' { Some(j + 1) } else { None }
1475    }
1476
1477    /// A GFM `http://` or `https://` autolink literal at i.
1478    fn protocol_autolink(&self, i: usize, to: usize) -> Option<(usize, char)> {
1479        let v = self.v;
1480        let b = v.as_bytes();
1481        let mut k = i;
1482        while k < to && b[k].is_ascii_alphabetic() && k - i < 5 {
1483            k += 1;
1484        }
1485        let protocol = v.as_bytes()[i..k].to_ascii_lowercase();
1486        if !(protocol == b"http" || protocol == b"https")
1487            || byte(v, k) != b':'
1488            || byte(v, k + 1) != b'/'
1489            || byte(v, k + 2) != b'/'
1490            || k + 3 > to
1491        {
1492            return None;
1493        }
1494        k += 3;
1495        let c = v[k..to].chars().next()?;
1496        if c.is_ascii_control() || c.is_whitespace() || is_unicode_punctuation(c) {
1497            return None;
1498        }
1499        let k = self.domain(k, to)?;
1500        let k = self.path(k, to);
1501        Some((k, v[..k].chars().next_back().unwrap_or(' ')))
1502    }
1503
1504    /// A GFM `www.` autolink literal at i.
1505    fn www_autolink(&self, i: usize, to: usize) -> Option<(usize, char)> {
1506        let v = self.v;
1507        if i + 4 > to || !v.as_bytes()[i..i + 3].eq_ignore_ascii_case(b"www") || byte(v, i + 3) != b'.' {
1508            return None;
1509        }
1510        let k = self.domain(i, to)?;
1511        let k = self.path(k, to);
1512        Some((k, v[..k].chars().next_back().unwrap_or(' ')))
1513    }
1514
1515    /// An autolink literal's domain from k: where it ends.
1516    fn domain(&self, k: usize, to: usize) -> Option<usize> {
1517        let v = self.v;
1518        let mut j = k;
1519        let mut seen = false;
1520        let mut underscore_last = false;
1521        let mut underscore_last_last = false;
1522        while j < to {
1523            let c = v[j..].chars().next()?;
1524            if c == '.' || c == '_' {
1525                if self.trail(j, to) {
1526                    break;
1527                }
1528                if c == '_' {
1529                    underscore_last = true;
1530                } else {
1531                    underscore_last_last = underscore_last;
1532                    underscore_last = false;
1533                }
1534                j += 1;
1535                continue;
1536            }
1537            if c.is_whitespace() || c != '-' && is_unicode_punctuation(c) {
1538                break;
1539            }
1540            seen = true;
1541            j += c.len_utf8();
1542        }
1543        if underscore_last || underscore_last_last || !seen {
1544            return None;
1545        }
1546        Some(j)
1547    }
1548
1549    /// An autolink literal's path from k: where it ends (trailing punctuation left out, parentheses balanced).
1550    fn path(&self, k: usize, to: usize) -> usize {
1551        let v = self.v;
1552        let mut j = k;
1553        let (mut open, mut close) = (0, 0);
1554        while j < to {
1555            let Some(c) = v[j..].chars().next() else { break };
1556            if c == '(' {
1557                open += 1;
1558                j += 1;
1559                continue;
1560            }
1561            if c == ')' && close < open {
1562                close += 1;
1563                j += 1;
1564                continue;
1565            }
1566            if matches!(c, '!' | '"' | '&' | '\'' | ')' | '*' | ',' | '.' | ':' | ';' | '<' | '?' | ']' | '_' | '~') {
1567                if self.trail(j, to) {
1568                    break;
1569                }
1570                if c == ')' {
1571                    close += 1;
1572                }
1573                j += 1;
1574                continue;
1575            }
1576            if c.is_whitespace() {
1577                break;
1578            }
1579            j += c.len_utf8();
1580        }
1581        j
1582    }
1583
1584    /// Is j the start of trailing punctuation (then the end of the literal)?
1585    fn trail(&self, j: usize, to: usize) -> bool {
1586        let v = self.v;
1587        let b = v.as_bytes();
1588        let mut k = j;
1589        loop {
1590            if k >= to {
1591                return true;
1592            }
1593            let c = b[k];
1594            match c {
1595                b'!' | b'"' | b'\'' | b')' | b'*' | b',' | b'.' | b':' | b';' | b'?' | b'_' | b'~' => k += 1,
1596                b'&' => {
1597                    k += 1;
1598                    if !(k < to && b[k].is_ascii_alphabetic()) {
1599                        return false;
1600                    }
1601                    while k < to && b[k].is_ascii_alphabetic() {
1602                        k += 1;
1603                    }
1604                    if !(k < to && b[k] == b';') {
1605                        return false;
1606                    }
1607                    k += 1;
1608                }
1609                b']' => {
1610                    k += 1;
1611                    if k >= to || matches!(b[k], b'(' | b'[' | b' ' | b'\t' | b'\n' | b'\r') {
1612                        return true;
1613                    }
1614                    let n = v[k..].chars().next().unwrap_or(' ');
1615                    if n.is_whitespace() {
1616                        return true;
1617                    }
1618                }
1619                b'<' | b' ' | b'\t' | b'\n' | b'\r' => return true,
1620                _ => {
1621                    let n = v[k..].chars().next().unwrap_or(' ');
1622                    return n.is_whitespace();
1623                }
1624            }
1625        }
1626    }
1627}
1628
1629/// Unicode punctuation as micromark reads it (Unicode's P and S categories, approximated: ASCII punctuation and the
1630/// non-alphanumeric, non-space symbols).
1631fn is_unicode_punctuation(c: char) -> bool {
1632    if c.is_ascii() {
1633        return c.is_ascii_punctuation();
1634    }
1635    !c.is_alphanumeric() && !c.is_whitespace() && !matches!(c, '\u{300}'..='\u{36f}')
1636}