From 7867122326beb97f65e81a242f16fa1a68e91bb7 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 11 May 2026 10:51:11 +0100 Subject: [PATCH] Fix for match starts inside one link and continues into the next opening link. --- add_links/match.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/add_links/match.py b/add_links/match.py index 9d00404..6c3a12e 100644 --- a/add_links/match.py +++ b/add_links/match.py @@ -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()