From 3cf186f3cb2e2239860e2b3e9801811ca282d553 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 2 May 2026 19:18:10 +0200 Subject: [PATCH] Handle link conversion in isolation --- RNS/Utilities/rngit/util.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/RNS/Utilities/rngit/util.py b/RNS/Utilities/rngit/util.py index 5a264bc1..e1483840 100644 --- a/RNS/Utilities/rngit/util.py +++ b/RNS/Utilities/rngit/util.py @@ -217,13 +217,26 @@ class MarkdownToMicron: code_blocks = [] def extract_code(match): code_blocks.append(match.group(1)) - return f"\x00{len(code_blocks)-1}\x00" + return f"\x00CODE{len(code_blocks)-1}\x00" + + links = [] + def extract_link(match): + links.append((match.group(1), match.group(2))) + return f"\x00LINK{len(links)-1}\x00" text = self.INLINE_CODE_RE.sub(extract_code, text) - text = self.LINK_RE.sub(self._link_sub, text) + text = self.LINK_RE.sub(extract_link, text) text = self.BOLD_RE.sub(self._bold_sub, text) text = self.ITALIC_RE.sub(self._italic_sub, text) + def restore_link(match): + idx = int(match.group(1)) + text, url = links[idx] + text = text.replace('`', '') + return f"`!`[{text}`{url}]`!" + + text = re.sub(r'\x00LINK(\d+)\x00', restore_link, text) + def restore_code(match): idx = int(match.group(1)) content = code_blocks[idx] @@ -232,11 +245,11 @@ class MarkdownToMicron: # highlighted = self._highlight_inline_code(content) # if highlighted: return highlighted - # Plain inline code formatting + # Use plain inline code formatting content = content.replace('`', '\\`') return f"{self.CODE_BG_INLINE}{self.CODE_FG}{content}{self.CODE_RESET}" - text = re.sub(r'\x00(\d+)\x00', restore_code, text) + text = re.sub(r'\x00CODE(\d+)\x00', restore_code, text) return text def _highlight_inline_code(self, content): @@ -251,15 +264,6 @@ class MarkdownToMicron: content = match.group(1) or match.group(2) return f"{self.ITALIC}{content}{self.ITALIC_END}" - def _link_sub(self, match): - text = match.group(1) - url = match.group(2) - # TODO: Evaluate best way to handle both normal and nomadnet URLs - text = text.replace('`', '') - # url = url.replace('`', '\\`') - mu_link = f"`!`[{text}`{url}]`!" - return mu_link - def _format_header(self, match): hashes = match.group(1) content = match.group(2)