Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(telegram): parse nested emphasis safely

cyh9828abfde5bf
diff --git a/modules/im/formatters/telegram_formatter.py b/modules/im/formatters/telegram_formatter.py
index 79676b123..43e912d17 100644
--- a/modules/im/formatters/telegram_formatter.py
+++ b/modules/im/formatters/telegram_formatter.py
@@ -17,9 +17,7 @@ from .base_formatter import BaseMarkdownFormatter
class TelegramFormatter(BaseMarkdownFormatter):
_CODE_BLOCK_RE = re.compile(r"```(?:([\w.+-]+)\n)?(.*?)```", re.DOTALL)
_INLINE_CODE_RE = re.compile(r"`([^`\n]+)`")
- _BOLD_RE = re.compile(r"\*\*(.+?)\*\*", re.DOTALL)
- _ITALIC_RE = re.compile(r"(?<!\*)\*([^*\n]+)\*(?!\*)")
- _STRIKE_RE = re.compile(r"~~(.+?)~~", re.DOTALL)
+ _INLINE_TOKENS = (("**", "b"), ("~~", "s"), ("*", "i"))
def format_bold(self, text: str) -> str:
return f"**{text}**"
@@ -101,11 +99,39 @@ class TelegramFormatter(BaseMarkdownFormatter):
return "".join(rendered_parts)
+ def _render_inline_segment(self, text: str, start: int = 0, stop_token: str | None = None) -> tuple[str, int, bool]:
+ rendered_parts: list[str] = []
+ index = start
+
+ while index < len(text):
+ if stop_token and text.startswith(stop_token, index):
+ return "".join(rendered_parts), index + len(stop_token), True
+
+ matched_token = False
+ for token, tag in self._INLINE_TOKENS:
+ if not text.startswith(token, index):
+ continue
+ inner, next_index, closed = self._render_inline_segment(text, index + len(token), token)
+ if closed and inner:
+ rendered_parts.append(f"<{tag}>{inner}</{tag}>")
+ index = next_index
+ else:
+ rendered_parts.append(token)
+ index += len(token)
+ matched_token = True
+ break
+
+ if matched_token:
+ continue
+
+ rendered_parts.append(text[index])
+ index += 1
+
+ return "".join(rendered_parts), index, False
+
def _apply_inline_formatting(self, text: str) -> str:
- text = self._BOLD_RE.sub(r"<b>\1</b>", text)
- text = self._STRIKE_RE.sub(r"<s>\1</s>", text)
- text = self._ITALIC_RE.sub(r"<i>\1</i>", text)
- return text
+ rendered, _, _ = self._render_inline_segment(text)
+ return rendered
def render(self, text: str) -> str:
if not text:
diff --git a/tests/test_telegram_formatter.py b/tests/test_telegram_formatter.py
index d6d8f33d6..0b5fa16cb 100644
--- a/tests/test_telegram_formatter.py
+++ b/tests/test_telegram_formatter.py
@@ -46,3 +46,11 @@ def test_render_preserves_literal_placeholder_like_text() -> None:
rendered = formatter.render("literal @@TG0@@ and `code`")
assert rendered == "literal @@TG0@@ and <code>code</code>"
+
+
+def test_render_preserves_valid_nested_emphasis_structure() -> None:
+ formatter = TelegramFormatter()
+
+ rendered = formatter.render("**bold *italic***")
+
+ assert rendered == "<b>bold <i>italic</i></b>"