From 79e6e80d82e9118a9fc6159acfa37ed91b8d4a64 Mon Sep 17 00:00:00 2001 From: bobo Date: Mon, 27 May 2024 14:12:21 +0800 Subject: [PATCH] Refactor TextEditor to handle block flags in text --- lib/chatmark/widgets.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/chatmark/widgets.py b/lib/chatmark/widgets.py index e1e9987..459285e 100644 --- a/lib/chatmark/widgets.py +++ b/lib/chatmark/widgets.py @@ -202,14 +202,22 @@ def __init__( super().__init__(submit_button_name, cancel_button_name) self._title = title - self._text = text + self._text = self._handle_block_flag(text) self._editor_key = self.gen_id(self._id_prefix, 0) self._new_text: Optional[str] = None + def _handle_block_flag(self, text): + """convert \\ to \\, and ` to \\`""" + return text.replace("\\", "\\\\").replace("`", "\\`") + + def _remove_block_flag(self, text): + """convert \\ to \\, and \\` to `""" + return text.replace("\\`", "`").replace("\\\\", "\\") + @property def new_text(self): - return self._new_text + return self._remove_block_flag(self._new_text) def _in_chatmark(self) -> str: """