Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

71.6 KB
Raw Blame
"""Strip prior-turn citation markers from assistant-authored chat text.

Follow-up research renumbers citations against its own freshly-found sources,
so a marker carried over from a prior turn's answer dangles or collides. The
recognized shapes are based on what ``CitationFormatter``
(text_optimization/citation_formatter.py) writes into a synthesized answer:

- ``number_hyperlinks``:            ``[[1]](url)``; comma groups re-emit as
                                    adjacent links with no separator
- ``domain_hyperlinks``:            ``[[arxiv.org]](url)``, ``[[slug]](url)``
- ``domain_id_hyperlinks`` (+always): ``[[arxiv.org-1]](url)``
- ``source_tagged_hyperlinks``:     ``[[tag-N]](url)`` when the source URL is
                                    http(s), PLAIN ``[local-1]`` /
                                    ``[personal-notes-3]`` otherwise; comma
                                    groups concatenate the two with no
                                    separator (``[[web-1]](url)[local-2]``).
                                    A collection slug can be one character
                                    (``AI`` -> ``[ai-1]``, ``X`` ->
                                    ``[x-1]``), so slug length proves
                                    nothing; ``arr[i-1]`` survives through
                                    the word-glue guard below instead
- ``no_hyperlinks``:                ``[1]``, ``[1, 2]``
- lenticular (some models):         ``【1】``, ``【1, 2】``

Every hyperlinked citation the formatter emits uses DOUBLE brackets. One
single-bracket shape is also stripped, for a model that re-renders a
citation with one bracket pair: a domain label that names the link's OWN
host as ``CitationFormatter`` would label it
(``[arxiv.org](https://arxiv.org/x)``,
``[arxiv.org-1](https://www.arxiv.org/x)``). Any other single-bracket
link survives — numeric labels included, since those are years, ranks
and anchors far more often than citations (``[1998](url)``,
``[1](#step-1)``, ``[10](url)``) — as do ``[config.yaml](url)``,
``[README.md](README.md)``, ``[v1.2](url)``,
``[example.com](https://other.org/)`` and ``[dataset](url)``. Double-bracket
labels match case-insensitively: ``CitationFormatter _extract_domain``
preserves URL case, so an uppercase one-label authority
(``[[LOCALHOST]](http://LOCALHOST/a)``, ``[[LOCALHOST-1]](...)``) strips
too. Inside ``[[...]](url)`` an all-digit hyphenated label strips too
(``[[2024-01-15]]``, ``[[1-2-3]]``, ``[[2023-2024-26]]``): the title and
collection slugs produce them. A destination may hold Unicode spaces
(only ASCII spaces end one, as in CommonMark), be up to 16384 characters
long and nest balanced parentheses 16 deep.

Bare numeric ranges such as ``[2020-2024]`` are preserved: without source
provenance they cannot be distinguished from numeric collection tags.
Hyperlinked numeric collection tags remain recognizable. A consequence:
a bare numeric tag (``[2023-2024-27]``) stays, and so does a bare token
glued right after it (``[9-4][5]``), which reads as an index chain.

The scanner bounds citation lookups and advances over failed bracket runs.
Text is segmented as:

1. fenced code blocks — an unclosed fence ends with its containing
   list/blockquote or document — and indented code blocks: emitted
   verbatim (a fence glyph inside an open HTML code element below is
   element content, never a fence). "Indented" is measured from the
   content column of the containers opened on the same line — a list
   marker's content sits one space past the marker, and tabs advance to
   the next multiple-of-4 column — so ``-     [1]`` and ``   \t[1]`` are
   code while ``-    [1]`` is a paragraph continuation that strips. A
   later line of a list item is measured from column 0, so a paragraph
   indented four or more columns after a blank line is kept verbatim
   as indented code even inside the item;
2. HTML ``<pre>``/``<code>`` elements (nesting-aware, may span lines) and
   link reference definitions (``[label]: dest``, optionally inside a
   blockquote): emitted verbatim — the destination is validated per
   CommonMark (``<...>`` or one whitespace-free token, optionally a
   well-formed title), so a pseudo definition whose "destination" is
   prose (``[Note]: See [1] for details.``) has its markers stripped.
   The destination and the title may each sit on a following line at any
   indentation (a lazy line included) unless that line opens a fence or
   ``<pre>``;
3. prose regions: CommonMark inline code spans (a run of N backticks
   closes at the next run of exactly N backticks, CommonMark-style, and
   may span lines within a paragraph — where only a bullet or an
   ordered marker numbered 1 can interrupt the paragraph, and a ``>``
   re-entry after a lazy continuation line is still that paragraph) and
   complete
   Markdown links/images — relative destinations and labels that merely
   LOOK citation-shaped (``[see [1]](url)``, ``![alt\n[1]](pic.jpg)``)
   included — are kept verbatim; the remaining prose is scanned left to
   right for citation *runs*: a run is one or more adjacent citation
   tokens (``[local-1][personal-notes-2]``, ``[[web-1]](url)[local-2]``,
   ``[1][2]``), optionally separated by horizontal whitespace. The run is
   removed together with ONE of its flanking horizontal-whitespace
   separators: mid-line, the whitespace before it (one separator is kept
   when a word or punctuation other than closing punctuation follows; a
   glued run is replaced by one space only when a word follows it and
   a word or word-ending punctuation precedes it — see ``_joins_words``
   — so ``Claim.[[1]](url)Next`` becomes ``Claim. Next`` while
   ``3.[[1]](url)5``, ``**[[1]](url)Bold**`` and ``/a/[1]/b`` stay
   joined, and no space is added next to text in a script written
   without spaces, such as CJK; parentheses left empty mid-line, as in
   ``Claim ([[1]](url)).``, are removed with the space before them); at the
   end of a line, the whitespace before it; at the start of a line, the
   whitespace after it, so indentation before the run (a list item's
   continuation paragraph) is kept verbatim and the following text is not
   re-indented into a code block. No other whitespace is touched.

A BARE bracket token (no URL) preceded by a word character, ``]`` or ``{``
is an indexing expression, not a citation: ``results[1]``, ``arr[i-1]``,
``x[1][2]`` and the LaTeX subscript ``x_{[i-1]}`` survive. Lenticular
``【1】`` is never an index and strips even when glued (``2020【1】``). Letters of scripts written without spaces (CJK,
kana, Thai, ...) are not word glue: ``首都です[1]`` strips. Hyperlinked
tokens strip regardless of the preceding character — the formatter
rewrites ``word[1]`` into ``word[[1]](url)`` when the source has a URL,
and that must not leak. A preceding ``.`` is not a guard: footnote-style
``holds.[local-1]`` strips.

Two more positions keep a token: inside a bare URL (``https://x.org/a[1]``;
the URL ends at whitespace, ``<``, ``>``, a quote, or a ``)`` with no
matching ``(`` in it, such as the one closing a Markdown link
destination), and a bare token followed by ``:`` that opens a line's
content after only indentation, blockquote, list or ATX-heading markers
(``[1]: Smith 2020``, ``- [1]: ...``, ``## [1]: ...``) — a bibliography
entry. A full reference link ``[text][label]`` is kept only when the
second label holds no unescaped bracket, so ``[Table 2][[1]](url)`` is
text followed by a citation.
"""

import re
from bisect import bisect_right
from ipaddress import ip_address
from urllib.parse import urlsplit

# Bounds that keep every scan linear on adversarial input. Real labels and
# URLs are far below them; past a bound the text simply is not a citation.
_LABEL_MAX = 256  # longest label text scanned between brackets
_LINK_LABEL_MAX = 1000  # longest link label scanned for verbatim links
_URL_MAX = 16384  # longest URL scanned after "("
_URL_MAX_DEPTH = 16  # deepest balanced-paren nesting accepted in a URL

# Citation-shaped labels (validated with fullmatch on the bracketed text).
_NUM_LIST_RE = re.compile(r"\d+(?:\s*,\s*\d+)*")  # "1", "1, 2"
_TOKEN_RE = re.compile(
    r"[A-Za-z0-9][A-Za-z0-9._-]*"
)  # "arxiv.org", "LOCALHOST"
# Plain tag form ``<slug>-N``. ``_slugify_collection`` can emit a slug of
# any length ("AI" -> "ai", "X" -> "x"), so no length floor applies;
# ``arr[i-1]`` indexing is protected by the word-glue guard instead.
_TAG_ID_RE = re.compile(r"[a-z0-9][a-z0-9._-]*-\d+")  # "x-1", "local-1"
# All-digit hyphenated label, accepted only inside ``[[...]](url)``:
# ``_slugify_title``/``_slugify_collection`` turn "2024-01-15", "1.2.3" and
# "2023/2024" into "2024-01-15", "1-2-3" and "2023-2024", and the id modes
# append "-N". A BARE ``[2020-2024]`` stays prose (see module docstring).
_NUMERIC_TAG_ID_RE = re.compile(r"\d+(?:-\d+)+")
_AUTHORITY_TOKEN_RE = re.compile(
    r"[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?(?::\d{1,5})?"
)

# Prose scanning: only these characters can begin (or precede) a citation.
_INTERESTING_RE = re.compile(r"[\[\u3010 \t]")
# URL scanning: jump between the only characters that affect paren balance
# or terminate a URL. CommonMark ends a destination at an ASCII space or
# control character only; ``_safe_link_destination`` leaves Unicode spaces
# (U+00A0, U+3000) in place, so they are URL characters here.
_URL_EVENT_RE = re.compile(r"[()\x00-\x20\x7f]")
# A bare (autolinked) URL: it runs to whitespace, ``<``, ``>`` or a quote,
# and a ``)`` with no matching ``(`` inside it ends it (the ``)`` closing
# a Markdown link destination). Each match consumes its characters, so
# one finditer over a chunk is linear.
_BARE_URL_RE = re.compile(
    r"(?<![A-Za-z0-9+.-])https?://[^\s<>\"']*", re.IGNORECASE
)
_PAREN_RE = re.compile(r"[()]")
# What may precede a ``[1]:`` bibliography-style entry on its own line:
# indentation, blockquote markers, a list marker or an ATX heading opener.
# Each marker must be followed by whitespace (``>`` excepted), so a run
# like ``####`` has exactly one parse and the match stays linear.
_LINE_LEAD_RE = re.compile(
    r"[ \t]*(?:(?:>|(?:[-+*]|\d{1,9}[.)]|#{1,6})(?=[ \t]))[ \t]*)*"
)
_LINE_LEAD_MAX = 64  # longest such prefix considered
_CLOSING_TAG_RE = re.compile(r"</[A-Za-z][A-Za-z0-9-]*[ \t]*>")
_CLOSING_TAG_MAX = 64  # longest closing tag looked back over
# Everything up to the next unescaped ``[`` or ``]`` (captured), reading
# backslash escapes pairwise; an unrolled loop, so a failed match is linear.
_UNESCAPED_BRACKET_RE = re.compile(r"[^\[\]\\]*(?:\\[\s\S][^\[\]\\]*)*([\[\]])")
_HTML_CODE_TAG_RE = re.compile(r"</?(?:code|pre)(?:\s[^>]*)?>", re.IGNORECASE)
_HTML_COMMENT_RE = re.compile(r"<!--.*?(?:-->|$)", re.DOTALL)
# A code span / link label may cross newlines but never a blank line.
_BLANK_LINE_RE = re.compile(r"\r?\n[ \t]*\r?\n")
# Link reference definition, optionally inside blockquotes or a list item.
_REFERENCE_DEF_RE = re.compile(
    r"^[ \t]{0,3}(?P<quotes>(?:>[ \t]?)*)(?P<list>"
    r"(?:[-+*]|\d{1,9}[.)])[ \t]+)?\[(?P<label>[^\]]*)\]:(?P<destination>.*)$"
)
_CONTAINER_PREFIX_RE = re.compile(
    r"^(?P<indent>[ \t]{0,3})(?P<quotes>(?:>[ \t]?)*)(?P<body>.*)$"
)
_REFERENCE_TITLE_RE = re.compile(
    r'(?:(?:"(?:[^"\\]|\\.)*")|(?:\'(?:[^\'\\]|\\.)*\')|(?:\([^)]*\)))'
)
_ATX_HEADING_RE = re.compile(r"^[ ]{0,3}#{1,6}(?:[ \t]+|$)")
_THEMATIC_BREAK_RE = re.compile(
    r"^[ ]{0,3}(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})$"
)
_HTML_BLOCK_TAG_RE = re.compile(
    r"^[ ]{0,3}</?(?:address|article|aside|base|basefont|blockquote|body|caption|"
    r"center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|"
    r"figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|"
    r"li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|"
    r"section|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:[ \t/>]|$)",
    re.IGNORECASE,
)


def _has_letter(label: str) -> bool:
    return any("a" <= ch.lower() <= "z" for ch in label)


def _is_num_list(label: str) -> bool:
    return _NUM_LIST_RE.fullmatch(label) is not None


def _is_letter_token(label: str) -> bool:
    """``True`` for slug/domain/tag labels like ``arxiv.org``/``web-1``."""
    return _TOKEN_RE.fullmatch(label) is not None and _has_letter(label)


def _is_ip_token(label: str) -> bool:
    try:
        ip_address(label)
    except ValueError:
        host, separator, port = label.rpartition(":")
        if not separator or not port.isdigit():
            return False
        try:
            ip_address(host)
        except ValueError:
            return False
    return True


def _is_authority_token(label: str) -> bool:
    return (
        (":" in label or "." in label)
        and _AUTHORITY_TOKEN_RE.fullmatch(label) is not None
        and (_has_letter(label) or _is_ip_token(label))
    )


def _is_formatter_ipv4_label(label: str, url: str) -> bool:
    try:
        parsed = urlsplit(url)
        host = parsed.hostname
        if host is None or ip_address(host).version != 4:
            return False
    except ValueError:
        return False
    return label == ".".join(parsed.netloc.split(".")[-2:])


def _is_authority_citation(label: str, url: str) -> bool:
    try:
        authority = urlsplit(url).netloc
    except ValueError:
        return False
    if not authority:
        return False
    if authority.startswith("www."):
        authority = authority[4:]
    display = ".".join(authority.split(".")[-2:])
    base, separator, suffix = label.rpartition("-")
    return label == display or bool(
        separator and suffix.isdigit() and base == display
    )


def _is_dotted_token(label: str) -> bool:
    """``True`` for domain labels — the only single-bracket link shape."""
    return _is_letter_token(label) and "." in label


def _is_tag_id(label: str) -> bool:
    """``True`` for the plain source-tag form ``<slug>-N``."""
    return _TAG_ID_RE.fullmatch(label) is not None and _has_letter(label)


def _parse_url(text: str, pos: int) -> int | None:
    """Parse a balanced-parenthesis URL whose ``(`` is at ``text[pos]``.

    Returns the index just past the closing ``()``, or ``None`` when the
    parens never balance within ``_URL_MAX`` characters and ``_URL_MAX_DEPTH``
    nesting levels, or the candidate contains an ASCII space or control
    character. Jumps between ``(`` / ``)`` / space events so long URL-free stretches cost one
    C-level scan, never a per-character Python loop.
    """
    limit = pos + _URL_MAX
    depth = 0
    i = pos
    while True:
        m = _URL_EVENT_RE.search(text, i, limit + 1)
        if m is None or m.start() > limit:
            return None
        ch = m.group()
        if ch == "(":
            depth += 1
            if depth > _URL_MAX_DEPTH:
                return None
        elif ch == ")":
            depth -= 1
            if depth == 0:
                return m.end()
        else:  # whitespace inside a URL
            return None
        i = m.end()


def _parse_inline_destination(text: str, pos: int) -> int | None:
    """Parse a Markdown inline destination and optional title."""
    limit = min(len(text), pos + _URL_MAX)
    i = pos + 1
    if text[i : i + 1] == "<":
        destination_end = text.find(">", i + 1, limit)
        if destination_end == -1 or "\n" in text[i:destination_end]:
            return None
        i = destination_end + 1
    else:
        depth = 0
        while i < limit:
            ch = text[i]
            if ch == "\\" and i + 1 < limit:
                i += 2
                continue
            if ch == "(":
                depth += 1
                if depth > _URL_MAX_DEPTH:
                    return None
            elif ch == ")":
                if depth == 0:
                    return i + 1
                depth -= 1
            elif ch <= " " or ch == "\x7f":  # CommonMark: ASCII only
                break
            i += 1
        else:
            return None

    if i < limit and text[i] == ")":
        return i + 1

    while i < limit and text[i].isspace():
        i += 1
    title = _REFERENCE_TITLE_RE.match(text, i, limit)
    if title is None:
        return None
    i = title.end()
    while i < limit and text[i].isspace():
        i += 1
    return i + 1 if i < limit and text[i] == ")" else None


def _match_citation_token(text: str, j: int) -> tuple[int | None, bool, int]:
    """Match ONE citation token starting at ``text[j]`` (a ``[`` or ``【``).

    Returns ``(end_index_or_None, is_hyperlink, opener_run_end)``. A
    non-``None`` end means ``text[j:end]`` is a citation-shaped token;
    ``opener_run_end`` is always the index past the opening bracket run, so
    a FAILED parse still advances the caller past the whole run — the
    formatter never emits a citation starting mid-bracket-run, and that rule
    is what keeps unclosed-bracket floods linear.
    """
    n = len(text)
    if text[j] == "\u3010":  # lenticular 【1】 / 【1, 2】 — plain numbers only
        for closer in ("\u3011", "]"):
            close = text.find(closer, j + 1, j + 1 + _LABEL_MAX)
            if close != -1 and _NUM_LIST_RE.fullmatch(text, j + 1, close):
                return (close + 1, False, j + 1)
        return (None, False, j + 1)
    k = j
    while k < n and text[k] == "[":
        k += 1
    # Outer double brackets contain an IPv6 label's own brackets.
    if k - j >= 2:
        outer_close = text.find("]](", j + 2, j + 2 + _LABEL_MAX)
        if outer_close != -1:
            url_end = _parse_url(text, outer_close + 2)
            candidate = text[j + 2 : outer_close]
            if url_end is not None and _is_authority_citation(
                candidate, text[outer_close + 3 : url_end - 1]
            ):
                return (url_end, True, k)
    close = text.find("]", k, k + _LABEL_MAX)
    if close == -1:
        return (None, False, k)
    q = close
    while q < n and text[q] == "]":
        q += 1
    label = text[k:close]
    double = k - j >= 2 and q - close >= 2
    if (
        k - j >= 3
        and _is_ip_token(label)
        and text[close + 1 : close + 2] == ":"
    ):
        port_end = close + 2
        while port_end < n and text[port_end].isdigit():
            port_end += 1
        q = port_end
        while q < n and text[q] == "]":
            q += 1
        double = port_end > close + 2 and q - port_end >= 2
    if q < n and text[q] == "(":
        url_end = _parse_url(text, q)
        if url_end is not None:
            if double:
                ok = (
                    _is_num_list(label)
                    or _is_tag_id(label)
                    or _NUMERIC_TAG_ID_RE.fullmatch(label) is not None
                    or _is_letter_token(label)
                    or _is_authority_token(label)
                    or _is_ip_token(label)
                    or _is_formatter_ipv4_label(
                        label, text[q + 1 : url_end - 1]
                    )
                )
            else:
                # The formatter only emits double-bracket links; a single
                # pair is a citation only when it is a domain label naming
                # the link's own host. A dotted label alone is ordinary
                # link text (``[config.yaml](url)``), and a numeric one is
                # a year, rank or anchor (``[1998](url)``, ``[1](#s1)``).
                ok = _is_dotted_token(label) and _is_authority_citation(
                    label, text[q + 1 : url_end - 1]
                )
            return (url_end, True, k) if ok else (None, False, k)
        if _parse_inline_destination(text, q) is not None:
            return (None, False, k)
        # A malformed destination may still follow a bare citation.
    if double:
        return (None, False, k)  # the formatter never emits a bare [[label]]
    if _is_num_list(label) or _is_tag_id(label):
        return (q, False, k)
    return (None, False, k)


def _reference_label(label: str) -> str:
    return " ".join(label.split()).casefold()


def _is_reference_link(
    text: str, start: int, token_end: int, references: frozenset[str]
) -> bool:
    own_label = _reference_label(text[start + 1 : token_end - 1])
    if text[token_end : token_end + 1] != "[":
        return (
            own_label in references and text[token_end : token_end + 1] != "("
        )
    close = text.find("]", token_end + 1, token_end + _LINK_LABEL_MAX)
    if close == -1:
        return False
    label = text[token_end + 1 : close] or text[start + 1 : token_end - 1]
    return _reference_label(label) in references


def _match_citation_run(
    text: str, first_end: int, references: frozenset[str]
) -> int:
    """Extend a matched token across adjacent citation tokens.

    ``_replace_comma_citations`` joins formatted parts with no separator,
    so tokens arrive glued (``[local-1][personal-notes-2]``,
    ``[[web-1]](url)[local-2]``) or horizontally spaced. Continuation
    tokens skip the word-glue guard: they follow a citation, not an
    identifier. Horizontal whitespace is consumed only when another token
    actually follows it.
    """
    n = len(text)
    end = first_end
    while end < n:
        if text[end] in "[\u3010":
            tok = _match_citation_token(text, end)
            if tok[0] is None or _is_reference_link(
                text, end, tok[0], references
            ):
                return end
            end = tok[0]
            continue
        k = end
        while k < n and text[k] in " \t":
            k += 1
        if k == end or k >= n or text[k] not in "[\u3010":
            return end
        tok = _match_citation_token(text, k)
        if tok[0] is None or _is_reference_link(text, k, tok[0], references):
            return end
        end = tok[0]
    return end


def _bare_url_spans(text: str) -> tuple[list[int], list[int]]:
    """Return ``(starts, ends)`` of the bare URLs in ``text``, in order."""
    starts: list[int] = []
    ends: list[int] = []
    for match in _BARE_URL_RE.finditer(text):
        start, end = match.span()
        depth = 0
        for paren in _PAREN_RE.finditer(text, start, end):
            if paren.group() == "(":
                depth += 1
            elif depth:
                depth -= 1
            else:
                end = paren.start()
                break
        starts.append(start)
        ends.append(end)
    return (starts, ends)


def _inside_bare_url(spans: tuple[list[int], list[int]], j: int) -> bool:
    starts, ends = spans
    idx = bisect_right(starts, j) - 1
    return idx >= 0 and j < ends[idx]


def _is_word_glue(ch: str) -> bool:
    """Whether ``ch`` glues a following ``[...]`` into an index expression.

    Letters and digits of space-separated scripts do (``results[1]``).
    Scripts written without spaces (CJK, kana, Thai, ...) do not: a
    citation sits directly against their text (``首都です[1]``).
    """
    return (ch.isalnum() or ch == "_") and not _is_unspaced(ch)


def _is_unspaced(ch: str) -> bool:
    """``True`` for a character of a script written without word spaces,
    including its punctuation (``。``, ``、``, fullwidth forms)."""
    code = ord(ch) if len(ch) == 1 else 0
    return any(low <= code <= high for low, high in _UNSPACED_RANGES)


def _line_lead(text: str, j: int, left: str) -> bool:
    """Whether only container/heading markers precede ``text[j]`` on its
    line. ``left`` is the character before ``text[0]`` (``""`` at the
    start of a prose block, which starts a line)."""
    line_start = max(text.rfind("\n", 0, j), text.rfind("\r", 0, j)) + 1
    if line_start == 0 and left not in _LINE_START:
        return False
    if j - line_start > _LINE_LEAD_MAX:
        return False
    return _LINE_LEAD_RE.fullmatch(text, line_start, j) is not None


def _try_strip_run(
    text: str,
    j: int,
    guard_prev: bool,
    references: frozenset[str],
    url_spans: tuple[list[int], list[int]],
    prev_char: str = "",
    left: str = "",
) -> tuple[int | None, int]:
    """Try to strip one citation run whose first token starts at ``j``.

    Returns ``(strip_end_or_None, skip_to)``. ``guard_prev`` applies the
    bare-token word-glue guard (see module docstring); it is skipped when
    the run starts after horizontal whitespace, where a real citation may
    legally sit. ``prev_char`` supplies the source character immediately
    before ``text[0]`` when ``j`` is 0, so a chunk starting right after a
    protected span still honours the guard. ``left`` is the character
    before ``text[0]`` (``""`` when ``text`` starts a line). ``url_spans``
    is ``_bare_url_spans(text)``. On any failure ``skip_to`` is the
    opener-run end, so the caller never retries mid-run.
    """
    tok = _match_citation_token(text, j)
    if tok[0] is None:
        return (None, tok[2])
    end, is_hyperlink = tok[0], tok[1]
    if _is_escaped(text, j) or _is_reference_link(text, j, end, references):
        return (None, tok[2])
    if j > 0 and text[j - 1] == "!":
        return (None, tok[2])
    if (
        not is_hyperlink
        and text[end : end + 1] == ":"
        and _line_lead(text, j, left)
    ):
        # ``[1]: Smith 2020`` opening a line is a bibliography entry.
        return (None, tok[2])
    if _inside_bare_url(url_spans, j):
        return (None, tok[2])
    if (
        guard_prev
        and not is_hyperlink
        and text[j] != "\u3010"  # nobody indexes with ``x【1】``
        and (j > 0 or prev_char)
    ):
        prev = text[j - 1] if j > 0 else prev_char
        # ``{`` covers LaTeX subscripts (``x_{[i-1]}``).
        if _is_word_glue(prev) or prev in "]{":
            return (None, tok[2])
    return (_match_citation_run(text, end, references), tok[2])


def _match_verbatim_link(text: str, j: int) -> int | None:
    """Match a complete Markdown link/image whose ``[`` is at ``text[j]``.

    Returns the index past an inline or reference-style destination, or past
    the label's ``]`` for a destination-less image alt, or ``None``. The caller
    keeps a matched structure verbatim: its label
    may contain brackets that look citation-shaped (``[see [1]](url)``)
    and may span lines, but not a blank line. Scanning is bounded by
    ``_LINK_LABEL_MAX`` and jumps between brackets with C-level finds, so
    adversarial input stays linear.
    """
    n = len(text)
    limit = min(n, j + _LINK_LABEL_MAX)
    is_image = j > 0 and text[j - 1] == "!"
    if (
        not is_image
        and text.find("](", j + 1, limit + 1) == -1
        and text.find("][", j + 1, limit + 1) == -1
    ):
        return None
    depth = 1
    i = j + 1
    while i < limit:
        # ``i`` always sits just past a bracket, so backslash pairing
        # from ``i`` is aligned; escaped brackets are skipped at C level
        # instead of being re-found and re-checked once per nesting step.
        bracket = _UNESCAPED_BRACKET_RE.match(text, i, limit)
        if bracket is None:
            return None
        close = bracket.start(1)
        if text[close] == "[":
            depth += 1
            i = close + 1
            continue
        depth -= 1
        if depth == 0:
            if _BLANK_LINE_RE.search(text[j : close + 1]) is not None:
                return None
            suffix = close + 1
            if text[suffix : suffix + 1] == "(":
                dest_end = _parse_inline_destination(text, suffix)
                if dest_end is not None:
                    return dest_end
            if text[suffix : suffix + 1] == "[":
                # A reference label holds no unescaped bracket, so
                # ``[Table 2][[1]](url)`` is text followed by a citation.
                reference = _UNESCAPED_BRACKET_RE.match(text, suffix + 1, limit)
                if reference is not None and text[reference.start(1)] == "]":
                    return reference.start(1) + 1
            return close + 1 if is_image else None
        i = close + 1
    return None


def _is_escaped(text: str, pos: int) -> bool:
    backslashes = 0
    pos -= 1
    while pos >= 0 and text[pos] == "\\":
        backslashes += 1
        pos -= 1
    return backslashes % 2 == 1


def _html_tag_spans(text: str) -> list[tuple[int, int]]:
    """Return spans of HTML tags while respecting quoted attribute values."""
    spans: list[tuple[int, int]] = []
    pos = 0
    while True:
        start = text.find("<", pos)
        if start == -1 or start + 1 == len(text):
            return spans
        marker = text[start + 1]
        name_pos = start + 2 if marker == "/" else start + 1
        if name_pos >= len(text) or not text[name_pos].isalpha():
            pos = start + 1
            continue
        quote = ""
        i = name_pos + 1
        while i < len(text):
            ch = text[i]
            if quote:
                if ch == quote:
                    quote = ""
            elif ch in "\"'":
                quote = ch
            elif ch == ">":
                spans.append((start, i + 1))
                pos = i + 1
                break
            elif ch == "<":
                pos = i
                break
            i += 1
        else:
            return spans


# Characters after a stripped run that need no separator before them.
_NO_SEPARATOR_BEFORE = " \t\r\n.,;:!?)]}"
# Scripts written without word spaces, with their punctuation: Thai and
# Lao, Tibetan, Myanmar, Khmer, CJK symbols/punctuation, kana, CJK
# ideographs (with extensions and compatibility forms), Yi, fullwidth and
# halfwidth forms.
_UNSPACED_RANGES = (
    (0x0E00, 0x0EFF),
    (0x0F00, 0x0FFF),
    (0x1000, 0x109F),
    (0x1780, 0x17FF),
    (0x2E80, 0x2FDF),
    (0x3000, 0x31FF),
    (0x3400, 0x4DBF),
    (0x4E00, 0x9FFF),
    (0xA000, 0xA4CF),
    (0xF900, 0xFAFF),
    (0xFE30, 0xFE4F),
    (0xFF00, 0xFFEF),
    (0x20000, 0x3134F),
)


def _ends_closing_tag(text: str, j: int) -> bool:
    """Whether ``text[:j]`` ends with a closing HTML tag (``</span>``)."""
    if j == 0 or text[j - 1] != ">":
        return False
    start = text.rfind("<", max(0, j - _CLOSING_TAG_MAX), j - 1)
    return start != -1 and _CLOSING_TAG_RE.fullmatch(text, start, j) is not None


def _joins_words(
    text: str, j: int, end: int, left: str, tag_end: int = -1
) -> bool:
    """Whether removing ``text[j:end]`` would join two words, so a space
    must replace it (``word[[1]](url)next``, ``Claim.[[1]](url)Next``).

    Only a word may follow (not ``_``, and not a script written without
    spaces). Before the run there must be a word character, or
    punctuation that ends a word: ``)``, ``]``, ``}``, ``>``, ``!``, ``?``,
    ``;``, ``:``; ``.`` or ``,`` unless it sits between digits
    (``3.[[1]](u)5``); ``*``, ``"`` or ``'`` only when it CLOSES, i.e. its
    run follows a non-space character (``**bold**[[1]](u)Next`` but not
    ``**[[1]](u)Smith**``). Anything else — ``/page/[1]/x``, an opening
    bracket — stays joined. ``left`` is the character before ``text[0]``.

    A following ``[`` (a link label) or ``<`` (an inline tag) renders as
    the start of the next word, so it counts as one when a word character
    or a closing tag precedes the run: ``word[[1]](u)[dataset](v)`` and
    ``</span>[[1]](u)<span>`` keep a space. ``tag_end`` is a position
    of ``text`` known to follow a closing tag outside ``text``.
    """
    nxt = text[end]
    prev = text[j - 1] if j > 0 else left
    if nxt in "[<":
        return _is_word_glue(prev) or j == tag_end or _ends_closing_tag(text, j)
    if nxt == "_" or not _is_word_glue(nxt):
        return False
    if _is_word_glue(prev) or prev in ")]}>!?;:":
        return True
    if prev in ".,":
        before = text[j - 2] if j > 1 else (left if j == 1 else "")
        return not (before.isdigit() and nxt.isdigit())
    if prev in "*\"'":
        if j == 0:
            return False  # the run's start lies in another chunk
        k = j - 1
        while k > 0 and j - k < 3 and text[k - 1] == prev:
            k -= 1
        before = text[k - 1] if k > 0 else left
        return bool(before) and not before.isspace() and before != prev
    return False


# ``prev`` values meaning "nothing precedes this position on its line"
# (``""`` is the start of a prose block, which always starts a line).
_LINE_START = ("", "\n", "\r")


def _skip_horizontal(text: str, pos: int) -> int:
    while pos < len(text) and text[pos] in " \t":
        pos += 1
    return pos


def _drop_open_paren(out: list[str]) -> None:
    """Remove the ``(`` that ends ``out`` and the one space before it. The
    caller has checked that both lie in the current chunk, mid-line."""
    for _ in range(2):
        while out and not out[-1]:
            out.pop()
        if not out:
            return
        out[-1] = out[-1][:-1]


def _strip_citations_from_prose(
    text: str, references: frozenset[str], left: str = "", tag_end: int = -1
) -> str:
    """Strip citation runs from one code-span-free prose string.

    ``left`` is the single source character preceding ``text[0]`` (when
    the chunk starts right after a protected span); it supplies the
    word-glue guard and separator decision for a run at position 0.
    ``tag_end`` is the position in ``text`` right after a closing HTML
    tag that lies outside ``text`` (``-1`` when none).
    """
    out: list[str] = []
    pos = 0
    n = len(text)
    url_spans = _bare_url_spans(text)
    while True:
        m = _INTERESTING_RE.search(text, pos)
        if m is None:
            out.append(text[pos:])
            return "".join(out)
        j = m.start()
        out.append(text[pos:j])
        if text[j] in "[\u3010":
            prev = text[j - 1] if j > 0 else left
            end, skip_to = _try_strip_run(
                text,
                j,
                guard_prev=True,
                references=references,
                url_spans=url_spans,
                prev_char=prev,
                left=left,
            )
            if end is not None:
                if prev in _LINE_START:
                    # Nothing precedes the run on its line: its separator
                    # is the whitespace AFTER it, which would otherwise
                    # indent the following text.
                    end = _skip_horizontal(text, end)
                elif (
                    prev == "("
                    and text[end : end + 1] == ")"
                    and j >= 3
                    and text[j - 2] in " \t"
                    and text[j - 3] not in " \t\r\n"
                    and not (end + 1 < n and _is_word_glue(text[end + 1]))
                ):
                    # ``Claim ([[1]](u)).``: the parentheses held only the
                    # citation; drop them with the space before them.
                    _drop_open_paren(out)
                    end += 1
                elif end < n and _joins_words(text, j, end, left, tag_end):
                    out.append(" ")
                pos = end
                continue
            link_end = _match_verbatim_link(text, j)
            if link_end is not None:
                out.append(text[j:link_end])
                pos = link_end
                continue
            out.append(text[j:skip_to])
            pos = skip_to
        else:  # horizontal whitespace: a run may start after it
            k = m.end()
            while k < n and text[k] in " \t":
                k += 1
            if k < n and text[k] in "[\u3010":
                end, skip_to = _try_strip_run(
                    text,
                    k,
                    guard_prev=False,
                    references=references,
                    url_spans=url_spans,
                    left=left,
                )
                if end is not None:
                    after = _skip_horizontal(text, end)
                    if after == n or text[after] in "\r\n":
                        # The run ends its line: drop the whitespace
                        # before it (no trailing whitespace is created);
                        # whitespace after it is kept verbatim.
                        pass
                    elif (text[j - 1] if j > 0 else left) in _LINE_START:
                        # Indentation before a line-leading run is kept
                        # verbatim; the run's separator is the whitespace
                        # after it.
                        out.append(text[j:k])
                        end = after
                    elif text[end] not in _NO_SEPARATOR_BEFORE and not (
                        _is_unspaced(text[end])
                    ):
                        out.append(text[j:k])
                    pos = end
                    continue
                link_end = _match_verbatim_link(text, k)
                if link_end is not None:
                    out.append(text[j:link_end])
                    pos = link_end
                    continue
                out.append(text[j:skip_to])
                pos = skip_to
            else:
                out.append(text[j:k])
                pos = k


def _backtick_runs(text: str) -> list[tuple[int, int, int]]:
    """Backtick spans grouped by their enclosing paragraph or heading."""
    runs: list[tuple[int, int, int]] = []
    offset = 0
    paragraph = 0
    paragraph_depth = 0
    previous_blank = True
    previous_hard = False
    for line in text.splitlines(keepends=True):
        body, depth = _container_body(line)
        blank = not body.strip()
        hard = _ends_markdown_block(body)
        # A setext heading underline only underlines a paragraph line,
        # but it ends that paragraph — a code span cannot pair across it.
        hard = hard or (
            not previous_blank
            and not previous_hard
            and _SETEXT_UNDERLINE_RE.fullmatch(body.rstrip("\r\n")) is not None
        )
        # CommonMark: a list marker interrupts a paragraph only when it is
        # a bullet or an ordered marker numbered 1 — "2. text" is a lazy
        # continuation line, so it must not split an open code-span
        # paragraph (leaving stray unpaired backticks behind).
        marker = _LIST_MARKER_RE.match(body)
        starts_list = marker is not None and (
            marker.group("bullet") is not None or marker.group("ordered") == "1"
        )
        # ``paragraph_depth`` is the container depth the open paragraph
        # lives at; a lazy continuation line (a paragraph line without
        # its ``>`` prefix) keeps that paragraph — and its container —
        # open, so a ``>`` re-entry at the paragraph's own depth is
        # paragraph continuation text, not a new block. Only a DEEPER
        # container interrupts the paragraph (a quote start may interrupt
        # one); a shallower line is itself a lazy continuation.
        if (
            previous_blank
            or blank
            or hard
            or previous_hard
            or starts_list
            or depth > paragraph_depth
        ):
            paragraph += 1
            paragraph_depth = depth
        for match in re.finditer(r"`+", line):
            runs.append(
                (offset + match.start(), offset + match.end(), paragraph)
            )
        offset += len(line)
        previous_blank = blank
        previous_hard = hard
    return runs


def _inline_spans(
    text: str,
) -> tuple[list[tuple[int, int]], list[tuple[int, int]]]:
    """Recognize code and comments in order; each protects its own contents.

    A backtick or ``<!--`` inside an HTML tag is attribute text, not an
    opener: ``_html_tag_spans`` is quote-aware, so every position it
    covers is skipped before it can start a phantom span reaching past
    the tag and shielding later prose.
    """
    runs = _backtick_runs(text)
    tags = _html_tag_spans(text)
    tag_index = 0
    outside_tags: list[tuple[int, int, int]] = []
    for run in runs:
        while tag_index < len(tags) and tags[tag_index][1] <= run[0]:
            tag_index += 1
        if tag_index < len(tags) and tags[tag_index][0] <= run[0]:
            continue
        outside_tags.append(run)
    runs = outside_tags
    next_same: list[int | None] = [None] * len(runs)
    next_shorter: list[int | None] = [None] * len(runs)
    next_by_key: dict[tuple[int, int], int] = {}
    for idx in range(len(runs) - 1, -1, -1):
        start, end, paragraph = runs[idx]
        next_same[idx] = next_by_key.get((paragraph, end - start))
        next_shorter[idx] = next_by_key.get((paragraph, end - start - 1))
        next_by_key[(paragraph, end - start)] = idx
    codes: list[tuple[int, int]] = []
    comments: list[tuple[int, int]] = []
    comment_index = 0

    def next_comment(from_pos: int) -> int:
        nonlocal comment_index
        candidate = text.find("<!--", from_pos)
        while candidate != -1:
            if _is_escaped(text, candidate):
                candidate = text.find("<!--", candidate + 4)
                continue
            while (
                comment_index < len(tags)
                and tags[comment_index][1] <= candidate
            ):
                comment_index += 1
            if (
                comment_index < len(tags)
                and tags[comment_index][0] <= candidate
            ):
                candidate = text.find("<!--", tags[comment_index][1])
                continue
            break
        return candidate

    pos = 0
    i = 0
    comment = next_comment(0)
    while pos < len(text):
        while i < len(runs) and runs[i][0] < pos:
            i += 1
        if comment != -1 and comment < pos:
            comment = next_comment(pos)
        tick = runs[i][0] if i < len(runs) else len(text)
        if comment != -1 and comment < tick:
            close = text.find("-->", comment + 4)
            pos = len(text) if close == -1 else close + 3
            comments.append((comment, pos))
            continue
        if i == len(runs):
            break
        start, end, _ = runs[i]
        if _is_escaped(text, start):
            start += 1
            close_idx = next_shorter[i] if start < end else None
        else:
            close_idx = next_same[i]
        if close_idx is not None:
            pos = runs[close_idx][1]
            codes.append((start, pos))
            i = close_idx + 1
        else:
            pos = end
            i += 1
    return (codes, comments)


def _code_spans(text: str) -> list[tuple[int, int]]:
    return _inline_spans(text)[0]


def _strip_code_and_citations(
    text: str,
    references: frozenset[str],
    left: str = "",
    left_tag: bool = False,
) -> str:
    """Strip citations from prose, keeping inline code and comments verbatim.

    ``left`` is the source character immediately before ``text[0]``; it is
    threaded into every chunk that starts at ``pos`` so a citation run
    directly following a protected span keeps one separator instead of
    joining the neighbouring words. ``left_tag`` says ``text`` starts right
    after a closing HTML tag outside it.
    """

    def after_tag(at: int) -> bool:
        return left_tag if at == 0 else _ends_closing_tag(text, at)

    pieces: list[str] = []
    pos = 0
    code_spans, comment_spans = _inline_spans(text)
    element_ranges = _code_element_ranges(text)
    protected_spans: list[tuple[int, int]]
    if element_ranges:
        # Whole inline code elements swallow every smaller span inside
        # them (the tags themselves, code spans, comments); emitting an
        # inner span first would re-expose the element's content.
        kept: list[tuple[int, int]] = []
        idx = 0
        for span in sorted(comment_spans + _html_tag_spans(text) + code_spans):
            while (
                idx < len(element_ranges) and element_ranges[idx][1] <= span[0]
            ):
                idx += 1
            if idx < len(element_ranges) and element_ranges[idx][0] <= span[0]:
                continue
            kept.append(span)
        protected_spans = sorted(element_ranges + kept)
    else:
        protected_spans = sorted(
            comment_spans + _html_tag_spans(text) + code_spans
        )
    url_spans = _bare_url_spans(text) if protected_spans else ([], [])
    for start, end in protected_spans:
        if end <= pos:
            continue
        search_pos = pos
        while True:
            link_start = text.find("[", search_pos, start)
            if link_start == -1:
                break
            citation_end, skip_to = _try_strip_run(
                text,
                link_start,
                guard_prev=True,
                references=references,
                url_spans=url_spans,
                prev_char=left if link_start == 0 else "",
                left=left,
            )
            link_end = (
                _match_verbatim_link(text, link_start)
                if citation_end is None
                else None
            )
            if link_end is not None:
                pieces.append(
                    _strip_citations_from_prose(
                        text[pos:link_start],
                        references,
                        left=text[pos - 1] if pos else left,
                        tag_end=0 if after_tag(pos) else -1,
                    )
                )
                pieces.append(text[link_start:link_end])
                pos = link_end
                search_pos = pos
                continue
            search_pos = max(link_start + 1, citation_end or skip_to)
        if end <= pos:
            continue
        if start < pos:
            # Overlapping span: its opener sat inside already-emitted
            # verbatim content (an HTML tag or a Markdown link), so the
            # span is phantom — clamping to pos, the remainder is prose
            # and must never be re-emitted or shield citations.
            continue
        context_start = max(0, pos - 1)
        stripped = _strip_citations_from_prose(
            text[context_start:start] + text[start],
            references,
            left=left if pos == 0 else "",
            tag_end=pos - context_start if after_tag(pos) else -1,
        )
        pieces.append(stripped[pos - context_start : -1])
        pieces.append(text[start:end])
        pos = end
    pieces.append(
        _strip_citations_from_prose(
            text[pos:],
            references,
            left=text[pos - 1] if pos else left,
            tag_end=0 if after_tag(pos) else -1,
        )
    )
    return "".join(pieces)


def _html_code_tags(text: str) -> list[tuple[int, int, bool]]:
    tags: list[tuple[int, int, bool]] = []
    for start, end in _html_tag_spans(text):
        if re.match(
            r"</?(?:code|pre)(?=[\s/>])", text[start:end], re.IGNORECASE
        ):
            tags.append((start, end, text[start + 1 : start + 2] == "/"))
    return tags


def _code_element_ranges(text: str) -> list[tuple[int, int]]:
    """Whole ``<code>/<pre>`` element ranges: open-tag start through the
    matching close-tag end, nesting-aware.

    Used where the element is INLINE HTML inside a paragraph (CommonMark
    type 7): everything between the tags renders as code and must stay
    verbatim. Unclosed elements contribute nothing — the raw tag itself
    is already protected as an HTML tag span.
    """
    ranges: list[tuple[int, int]] = []
    stack: list[int] = []
    for start, end, closing in _html_code_tags(text):
        if closing and stack:
            open_start = stack.pop()
            if not stack:
                ranges.append((open_start, end))
        elif not closing:
            stack.append(start)
    return ranges


def _code_tags_start_block(
    tags: list[tuple[int, int, bool]], line: str, prose_open: bool
) -> bool:
    """Whether the line's code/pre tags open a CommonMark HTML *block*.

    ``<pre>`` is a type-1 start condition: it may interrupt a paragraph
    and its content spans blank lines. ``<code>`` is type 7: it may
    start a block only when no paragraph is open, the tag sits at the
    container's content-column start (up to three columns in), and only
    whitespace follows the complete open tag on the line. Any other
    ``<code>`` occurrence is inline HTML inside a paragraph — the line
    is prose, and the element's content is protected by
    ``_code_element_ranges`` instead of element state.
    """
    for start, end, closing in tags:
        if closing:
            continue
        tag = line[start : start + 5].lower()
        if tag.startswith("<pre"):
            return True
        if not tag.startswith("<code"):
            continue
        if prose_open:
            return False
        prefix = _CONTAINER_PREFIX_RE.match(line)
        content_at = (
            len(prefix.group("indent")) + len(prefix.group("quotes"))
            if prefix
            else 0
        )
        if not (
            content_at <= start <= content_at + 3
            and not line[content_at:start].strip()
        ):
            return False
        if end <= len(line) and line[end:].strip():
            return False
        return True
    return False


def _strip_html_code_line(
    line: str,
    tags: list[tuple[int, int, bool]],
    depth: int,
    references: frozenset[str],
) -> tuple[str, int]:
    """Preserve real code elements, ignoring tag-shaped attribute text.

    ``tags`` are the code/pre tags opening on this line (line-relative
    offsets, multi-line aware — a tag whose ``>`` sits on a later line
    ends past this line's length). Each prose segment is stripped with
    the character before it as left context so a citation directly after
    a tag keeps one separator.
    """
    pieces: list[str] = []
    pos = 0
    for start, end, closing in tags:
        if depth:
            pieces.append(line[pos:end])
            depth += -1 if closing else 1
        elif not closing:
            pieces.append(
                _strip_code_and_citations(
                    line[pos:start],
                    references,
                    left=line[pos - 1] if pos else "",
                    left_tag=_ends_closing_tag(line, pos),
                )
            )
            pieces.append(line[start:end])
            depth = 1
        else:
            pieces.append(
                _strip_code_and_citations(
                    line[pos:end],
                    references,
                    left=line[pos - 1] if pos else "",
                    left_tag=_ends_closing_tag(line, pos),
                )
            )
        pos = end
    tail = line[pos:]
    pieces.append(
        tail
        if depth
        else _strip_code_and_citations(
            tail,
            references,
            left=line[pos - 1] if 0 < pos <= len(line) else "",
            left_tag=0 < pos <= len(line) and _ends_closing_tag(line, pos),
        )
    )
    return ("".join(pieces), depth)


def _open_fence(line: str) -> tuple[str, int] | None:
    """Return ``(fence_char, run_length)`` when ``line`` opens a fence.

    CommonMark: up to three leading spaces, a run of 3+ backticks or
    tildes; a backtick fence's info string may not contain backticks.
    """
    head = line.lstrip(" ")
    if len(line) - len(head) > 3:
        return None
    for ch in ("`", "~"):
        if head.startswith(ch * 3):
            run = len(head) - len(head.lstrip(ch))
            if ch == "`" and "`" in head[run:]:
                return None
            return (ch, run)
    return None


def _container_body(line: str) -> tuple[str, int]:
    pos = 0
    depth = 0
    while match := _QUOTE_PREFIX_RE.match(line, pos):
        pos = match.end()
        depth += 1
    return (line[pos:], depth)


def _is_close_fence(line: str, ch: str, run: int) -> bool:
    """``True`` when ``line`` closes a ``(ch, run)`` fence: same character,
    at least the opening run length, nothing but whitespace after it."""
    head = line.lstrip(" ")
    if len(line) - len(head) > 3:
        return False
    stripped_run = len(head) - len(head.lstrip(ch))
    return stripped_run >= run and head[stripped_run:].strip() == ""


_QUOTE_PREFIX_RE = re.compile(r"[ ]{0,3}>[ \t]?")
_LIST_MARKER_RE = re.compile(
    r"[ ]{0,3}(?:(?P<bullet>[-+*])|(?P<ordered>\d{1,9})[.)])[ \t]+"
)
_SETEXT_UNDERLINE_RE = re.compile(r"[ ]{0,3}(?:=+|-+)[ \t]*$")


def _mask_comment_line(
    line: str, spans: list[tuple[int, int]], in_comment: bool
) -> tuple[str, bool]:
    """Mask comments outside inline code while preserving line positions.

    ``spans`` are this line's inline code spans (line-relative offsets,
    computed over the WHOLE text by the caller, exactly as
    ``_tag_scan_text`` computes them): a span opened on this line may
    close on a later one, so a per-line ``_code_spans`` would miss it
    and latch the comment state on an opener that is really span
    content — masking a genuinely fenced block further down.
    """
    span_index = 0
    scan = list(line)
    pos = 0
    while pos < len(line):
        if in_comment:
            close = line.find("-->", pos)
            end = len(line) if close == -1 else close + 3
            scan[pos:end] = [" " for _ in range(end - pos)]
            in_comment = close == -1
            pos = end
            continue
        start = line.find("<!--", pos)
        if start == -1:
            break
        while span_index < len(spans) and spans[span_index][1] <= start:
            span_index += 1
        if _is_escaped(line, start) or (
            span_index < len(spans)
            and spans[span_index][0] <= start < spans[span_index][1]
        ):
            pos = start + 4
            continue
        pos = start
        in_comment = True
    return ("".join(scan), in_comment)


def _blank_spans(scan: list[str], spans: list[tuple[int, int]]) -> None:
    """Blank character spans in place, preserving line positions."""
    for start, end in spans:
        scan[start:end] = [
            "\n" if char == "\n" else " " for char in scan[start:end]
        ]


def _tag_scan_text(text: str) -> str:
    """Blank inline code spans and comments for HTML code-tag recognition.

    Used before fences are known (fence recognition itself needs code-tag
    awareness), so tag-shaped text inside an inline code span or comment
    cannot masquerade as a ``<code>``/``<pre>`` element.
    """
    scan = list(text)
    code_spans, comment_spans = _inline_spans(text)
    _blank_spans(scan, code_spans + comment_spans)
    return "".join(scan)


def _code_tags_by_line(
    scan_text: str, lines: list[str]
) -> list[list[tuple[int, int, bool]]]:
    """Bucket code/pre tags by the line they open on, multi-line aware.

    ``_html_tag_spans`` already matches tags whose ``>`` sits on a later
    line when given a whole string; per-line scanning missed them. Tag
    offsets are relative to their opening line, so a tag ending on a
    later line yields an end past the opening line's length.
    """
    buckets: list[list[tuple[int, int, bool]]] = [[] for _ in lines]
    line_index = 0
    offset = 0
    for start, end, closing in _html_code_tags(scan_text):
        while line_index + 1 < len(lines) and start > offset + len(
            lines[line_index]
        ):
            offset += len(lines[line_index]) + 1
            line_index += 1
        buckets[line_index].append((start - offset, end - offset, closing))
    return buckets


def _code_spans_by_line(
    spans: list[tuple[int, int]], lines: list[str]
) -> list[list[tuple[int, int]]]:
    """Clamp whole-text inline code spans to every line they cover.

    Unlike ``_code_tags_by_line`` (open-line only), a span opened on an
    earlier line keeps masking comment openers on each line inside it,
    so each intersected line receives the span clamped to its own
    bounds. Spans arrive sorted and disjoint, so the walk is linear.
    """
    buckets: list[list[tuple[int, int]]] = [[] for _ in lines]
    line_index = 0
    offset = 0
    for start, end in spans:
        while line_index + 1 < len(lines) and start > offset + len(
            lines[line_index]
        ):
            offset += len(lines[line_index]) + 1
            line_index += 1
        i = line_index
        line_offset = offset
        while i < len(lines) and line_offset < end:
            clamp_start = max(start, line_offset) - line_offset
            clamp_end = min(end, line_offset + len(lines[i])) - line_offset
            if clamp_start < clamp_end:
                buckets[i].append((clamp_start, clamp_end))
            line_offset += len(lines[i]) + 1
            i += 1
    return buckets


def _fence_ranges(
    lines: list[str], code_tags: list[list[tuple[int, int, bool]]]
) -> dict[int, int]:
    """Map fence openers to exclusive ends, including their container boundary.

    Each visited fence consumes its content once. Lists retain their content
    indentation so a continuation fence is distinct from indented code.
    ``code_tags`` (from ``_code_tags_by_line``) tracks open ``<code>``/
    ``<pre>`` elements: a fence glyph inside one is element content, not a
    fence, so an unclosed ``` inside a code element cannot swallow — and
    protect from stripping — the rest of the document. Tags inside a
    recognized fence are literal fence content and never change the depth.
    """
    ranges: dict[int, int] = {}
    list_indents: list[int] = []
    previous_depth = 0
    in_comment = False
    html_depth = 0
    # Comment masking needs code spans over the whole text (a span may
    # straddle lines), computed exactly as ``_tag_scan_text`` does.
    span_lines = _code_spans_by_line(_code_spans("\n".join(lines)), lines)
    i = 0
    while i < len(lines):
        comment_before = in_comment
        scan_line, in_comment = _mask_comment_line(
            lines[i], span_lines[i], in_comment
        )
        body, quote_depth = _container_body(scan_line)
        if quote_depth != previous_depth:
            list_indents.clear()
        previous_depth = quote_depth
        body = body.expandtabs(4)
        leading = len(body) - len(body.lstrip(" "))
        if body.strip():
            while list_indents and leading < list_indents[-1]:
                list_indents.pop()
        offset = list_indents[-1] if list_indents else 0
        while True:
            marker = _LIST_MARKER_RE.match(body, offset)
            if marker is not None:
                offset = marker.end()
                list_indents.append(offset)
                continue
            quote = _QUOTE_PREFIX_RE.match(body, offset)
            if quote is not None:
                quote_depth += 1
                body = body[quote.end() :]
                offset = 0
                list_indents.clear()
                continue
            break
        fence = _open_fence(body[offset:])
        if fence is not None and not html_depth:
            # Comments in a fence info string are literal fence content.
            in_comment = comment_before
            end = i + 1
            while end < len(lines):
                continuation, depth = _container_body(lines[end])
                if depth < quote_depth:
                    break
                continuation = continuation.expandtabs(4)
                if depth == quote_depth:
                    if continuation.strip() and not continuation.startswith(
                        " " * offset
                    ):
                        break
                    if _is_close_fence(continuation[offset:], *fence):
                        end += 1
                        break
                end += 1
            ranges[i] = end
            i = end
            continue
        for _, _, closing in code_tags[i]:
            html_depth = max(0, html_depth + (-1 if closing else 1))
        i += 1
    return ranges


def _indented_past_containers(body: str, quote_depth: int) -> tuple[bool, int]:
    """Decide indented code against the content column of the containers
    opened on this line; a list item opened on an earlier line is not
    tracked, so its later lines are measured from column 0.

    The same container walk ``_fence_ranges`` uses — blockquote body via
    ``_container_body``, ``expandtabs(4)`` columns, then ``_LIST_MARKER_RE``
    and inner quote prefixes — but a marker contributes its TRUE content
    column (one space past the marker itself, not past all its trailing
    whitespace), so ``-     [1]`` is a list item whose content is indented
    code while ``-    [1]`` (three columns past the content column) is a
    paragraph continuation that still strips. Tabs count toward the next
    multiple-of-4 column. Returns ``(indented, quote_depth)`` with any
    quote opened after a list marker added to the depth.
    """
    body = body.expandtabs(4)
    offset = 0
    while True:
        marker = _LIST_MARKER_RE.match(body, offset)
        if marker is not None:
            text = marker.group()
            offset = marker.end() - (len(text) - len(text.rstrip())) + 1
            continue
        quote = _QUOTE_PREFIX_RE.match(body, offset)
        if quote is not None:
            quote_depth += 1
            body = body[quote.end() :]
            offset = 0
            continue
        break
    return (body[offset:].startswith("    "), quote_depth)


def _html_scan_lines(
    text: str, lines: list[str], fences: dict[int, int]
) -> list[str]:
    """Mask fenced/inline code and comments before recognizing HTML code tags."""
    scan = list(text)
    offsets: list[int] = []
    offset = 0
    for line in lines:
        offsets.append(offset)
        offset += len(line) + 1

    for i, end_line in fences.items():
        start = offsets[i]
        end = len(text) if end_line == len(lines) else offsets[end_line] - 1
        _blank_spans(scan, [(start, end)])

    code_spans, comment_spans = _inline_spans("".join(scan))
    _blank_spans(scan, code_spans + comment_spans)
    return "".join(scan).split("\n")


def _reference_continuation(reference: re.Match[str], line: str) -> str | None:
    """Return ``line``'s body when it may continue the definition at
    ``reference`` (as its destination or title line), else ``None``.

    Such a line continues the definition's paragraph, and indented code
    cannot interrupt a paragraph, so CommonMark sets no indentation limit
    on it: indent 0, four spaces, a tab and any indent at, past or short
    of a list item's content column all qualify, as does a lazy line with
    fewer ``>`` markers. A blank line ends the paragraph, and a deeper
    ``>`` opens a nested blockquote, which interrupts it. Lines that open
    a fence or ``<pre>`` element are rejected by the caller.
    """
    continuation = _CONTAINER_PREFIX_RE.match(line)
    if continuation is None:
        return None
    if continuation.group("quotes").count(">") > reference.group(
        "quotes"
    ).count(">"):
        return None
    body = continuation.group("body")
    return body if body.strip() else None


def _valid_reference_destination(rest: str) -> bool:
    """``True`` when ``rest`` (the text after ``]:``) parses as a link
    destination plus an optional title, per CommonMark.

    A destination is ``<...>`` or a single whitespace-free token with
    balanced parentheses; a well-formed title (``_REFERENCE_TITLE_RE``)
    may follow, separated by whitespace, and nothing else may remain.
    A destination with spaces and no valid title ("[Note]: See [1] for
    details.") is therefore prose, not a reference definition.
    """
    s = rest.lstrip()
    if not s:
        return False
    if s.startswith("<"):
        close = s.find(">")
        while close != -1 and _is_escaped(s, close):
            close = s.find(">", close + 1)
        if close == -1:
            return False
        after = s[close + 1 :].strip()
    else:
        parts = s.split(None, 1)
        token = parts[0]
        depth = 0
        k = 0
        while k < len(token):
            ch = token[k]
            if ch == "\\":
                k += 2
                continue
            if ch == "(":
                depth += 1
            elif ch == ")":
                depth -= 1
                if depth < 0:
                    return False
            k += 1
        if depth != 0:
            return False
        after = parts[1].strip() if len(parts) > 1 else ""
    return not after or _REFERENCE_TITLE_RE.fullmatch(after) is not None


def _reference_definition_extent(
    reference: re.Match[str],
    lines: list[str],
    i: int,
    block_openers: frozenset[int],
) -> int | None:
    """Return the exclusive end of a GENUINE reference definition whose
    match is at ``lines[i]``, or ``None`` when the line is not one.

    The destination may sit on the definition line or on the next line;
    one further line may carry the title (``_reference_continuation``
    decides which lines can continue a definition). ``block_openers``
    holds the indexes of lines that open a fenced code block or a
    ``<pre>`` element: either interrupts the definition's paragraph, so
    it is never a continuation line — consuming one as a destination
    would expose the block's code to stripping. An invalid destination
    (see ``_valid_reference_destination``) makes the line prose, so its
    citation markers strip normally.
    """
    next_line = i + 1
    destination = reference.group("destination")
    if destination.strip():
        if not _valid_reference_destination(destination):
            return None
    else:
        if next_line >= len(lines) or next_line in block_openers:
            return None
        continuation_body = _reference_continuation(reference, lines[next_line])
        if continuation_body is None or not _valid_reference_destination(
            continuation_body.strip()
        ):
            return None
        next_line += 1
    if next_line < len(lines) and next_line not in block_openers:
        continuation_body = _reference_continuation(reference, lines[next_line])
        if (
            continuation_body is not None
            and _REFERENCE_TITLE_RE.fullmatch(continuation_body.strip())
            is not None
        ):
            next_line += 1
    return next_line


def _ends_markdown_block(
    line: str, previous_body: str = "", previous_break: bool = False
) -> bool:
    # ``_backtick_runs`` feeds keepends lines; the trailing line ending
    # must go before a fullmatch can see a bare thematic break.
    candidate = line.rstrip("\r\n")
    return (
        _ATX_HEADING_RE.match(candidate) is not None
        or _THEMATIC_BREAK_RE.fullmatch(candidate) is not None
        or _HTML_BLOCK_TAG_RE.match(candidate) is not None
        # Same condition as ``_backtick_runs``: a setext underline ends
        # the paragraph above it (so a following indented line is code),
        # but only when the previous line is a non-blank paragraph line
        # that did not itself end a block.
        or (
            bool(previous_body.strip())
            and not previous_break
            and _SETEXT_UNDERLINE_RE.fullmatch(candidate) is not None
        )
    )


def strip_prior_turn_citations(text: str) -> str:
    """Remove prior-turn citation markers from ``text`` (see module docstring
    for the covered shapes). Code blocks/elements, inline code spans,
    non-citation markdown links and all whitespace other than one separator
    flanking each stripped run (see the module docstring) pass through
    verbatim; ``""`` returns ``""``."""
    if not text:
        return text
    lines = text.split("\n")
    out: list[str] = []
    prose: list[str] = []
    # Fence recognition needs code-element awareness (a ``` inside an open
    # <code>/<pre> is element content), and code-tag recognition wants
    # inline code and comments blanked — which needs no fence knowledge.
    fence_tags = _code_tags_by_line(_tag_scan_text(text), lines)
    fences = _fence_ranges(lines, fence_tags)
    html_scan_lines = _html_scan_lines(text, lines, fences)
    code_tags = _code_tags_by_line("\n".join(html_scan_lines), lines)
    # A fence or <pre> element interrupts a paragraph, so neither opening
    # line can continue a reference definition. ``prose_open=True`` is the
    # state the line would meet after an unfinished ``[label]:`` line; a
    # <code> element (HTML block type 7) cannot interrupt and is excluded.
    block_openers = frozenset(fences).union(
        k
        for k, tags in enumerate(code_tags)
        if tags and _code_tags_start_block(tags, lines[k], prose_open=True)
    )
    # Definitions written as code or comments are examples, not link targets.
    reference_labels: set[str] = set()
    reference_code_depth = 0
    for i, scan_line in enumerate(html_scan_lines):
        if not reference_code_depth:
            reference_match = _REFERENCE_DEF_RE.match(scan_line)
            if (
                reference_match is not None
                and _reference_definition_extent(
                    reference_match, html_scan_lines, i, block_openers
                )
                is not None
            ):
                reference_labels.add(
                    _reference_label(reference_match.group("label"))
                )
        for _, _, closing in code_tags[i]:
            reference_code_depth = max(
                0, reference_code_depth + (-1 if closing else 1)
            )
    references = frozenset(reference_labels)

    def flush_prose() -> None:
        if prose:
            out.append(_strip_code_and_citations("\n".join(prose), references))
            prose.clear()

    i = 0
    in_html_code = 0
    # Outermost open element is <code> (HTML block type 7: its block
    # ends at the first blank line) vs <pre> (type 1: content spans
    # blank lines until the closing tag).
    html_code_type7 = False
    indented_quote_depth: int | None = None
    block_boundary = False
    while i < len(lines):
        line = lines[i]
        if in_html_code:
            if html_code_type7 and not line.strip():
                # A type-7 block ends at a blank line; the element's
                # content closed with it and the next paragraph is prose.
                in_html_code = 0
                html_code_type7 = False
                out.append(line)
                block_boundary = True
                i += 1
                continue
            stripped, in_html_code = _strip_html_code_line(
                line, code_tags[i], in_html_code, references
            )
            out.append(stripped)
            block_boundary = in_html_code == 0
            i += 1
            continue
        if i in fences:
            end = fences[i]
            flush_prose()
            out.append("\n".join(lines[i:end]))
            i = end
            block_boundary = True
            continue
        body, quote_depth = _container_body(line)
        # Indented code is measured from the content column of the
        # containers opened on this line (list markers, inner quotes, tab
        # stops), not column 0 of the quote-stripped body; a later line
        # of a list item carries no marker and is measured from column 0.
        indented, quote_depth = _indented_past_containers(body, quote_depth)
        if indented_quote_depth is not None:
            if quote_depth == indented_quote_depth and (
                indented or not body.strip()
            ):
                flush_prose()
                out.append(line)
                i += 1
                continue
            indented_quote_depth = None
        if indented and (not prose or not prose[-1].strip() or block_boundary):
            flush_prose()
            out.append(line)
            indented_quote_depth = quote_depth
            block_boundary = False
            i += 1
            continue
        reference = _REFERENCE_DEF_RE.match(line)
        if reference is not None:
            extent = _reference_definition_extent(
                reference, lines, i, block_openers
            )
            if extent is not None:
                flush_prose()
                out.extend(lines[i:extent])
                i = extent
                block_boundary = True
                continue
        if code_tags[i] and _code_tags_start_block(
            code_tags[i], line, prose_open=bool(prose and prose[-1].strip())
        ):
            flush_prose()
            stripped, in_html_code = _strip_html_code_line(
                line, code_tags[i], 0, references
            )
            first_open = next((tag for tag in code_tags[i] if not tag[2]), None)
            html_code_type7 = (
                in_html_code > 0
                and first_open is not None
                and line[first_open[0] : first_open[0] + 5]
                .lower()
                .startswith("<code")
            )
            out.append(stripped)
            block_boundary = in_html_code == 0
            i += 1
            continue
        prose.append(line)
        block_boundary = _ends_markdown_block(
            body,
            _container_body(prose[-2])[0] if len(prose) > 1 else "",
            block_boundary,
        )
        i += 1
    flush_prose()
    return "\n".join(out)