3"""Structural Markdown inline-link parsing without hiding malformed prose."""
5from __future__
import annotations
8def split_link_destination(raw: str) -> str:
9 """Discard a Markdown link title while retaining an angle-wrapped target."""
13 if raw.startswith(
"<")
and ">" in raw:
14 return raw[1 : raw.index(
">")]
16 for index, char
in enumerate(raw):
19 elif char ==
")" and depth:
21 elif char.isspace()
and depth == 0:
26def _balanced_target_end(line: str, start: int) -> int |
None:
27 """Return the closing parenthesis for one proven-balanced destination."""
32 while index < len(line):
38 elif char ==
"<" and depth == 1:
40 elif char ==
">" and angle:
42 elif not angle
and char ==
"(":
44 elif not angle
and char ==
")":
52def inline_link_targets(line: str) -> list[str]:
53 """Parse ``](destination)`` pairs with balanced parentheses."""
54 targets: list[str] = []
57 opener = line.find(
"](", cursor)
61 end = _balanced_target_end(line, start)
63 targets.append(split_link_destination(line[start:end]))
64 cursor = end + 1
if end
is not None else start
68def mask_inline_link_targets(line: str) -> str:
69 """Blank balanced inline-link destinations before prose path scanning."""
73 opener = line.find(
"](", cursor)
77 end = _balanced_target_end(line, start)
79 masked[start:end] =
" " * (end - start)
80 cursor = end + 1
if end
is not None else start
81 return "".join(masked)