Fix for match starts inside one link and continues into the next opening link.

This commit is contained in:
Edward Betts 2026-05-11 10:51:11 +01:00
parent fad1ef9e49
commit 7867122326

View file

@ -252,6 +252,14 @@ def add_link(m: re.Match[str], replacement: str, text: str) -> str:
if matched_text.startswith("[[") and matched_text.endswith("|"):
return m.re.sub(lambda m: f"[[{replacement}|", text, count=1)
split_links = matched_text.find("]] [[")
if split_links > 0 and m.start() >= 2 and text[m.start() - 2 : m.start()] == "[[":
# Match starts inside one link and continues into the next opening link.
# Link only the text from the first link span and leave the second link as-is.
link_dest = replacement.split("|")[0] if "|" in replacement else replacement
visible = matched_text[:split_links]
return text[: m.start() - 2] + f"[[{link_dest}|{visible}]]" + text[m.start() + split_links + 2 :]
inner_bracket = matched_text.find("[[")
if inner_bracket > 0:
prefix = matched_text[:inner_bracket].rstrip()