From 91926d24b717e54fe31382e14515638c68ff535d Mon Sep 17 00:00:00 2001 From: binary-husky Date: Wed, 24 Jan 2024 01:38:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=80=E4=B8=AAcore=5Ffunc?= =?UTF-8?q?tional.py=E4=B8=AD=E5=87=BA=E7=8E=B0=E7=9A=84mermaid=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E7=89=B9=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared_utils/advanced_markdown_format.py | 29 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/shared_utils/advanced_markdown_format.py b/shared_utils/advanced_markdown_format.py index 653cf07..a015fd6 100644 --- a/shared_utils/advanced_markdown_format.py +++ b/shared_utils/advanced_markdown_format.py @@ -292,13 +292,25 @@ def close_up_code_segment_during_stream(gpt_reply): return gpt_reply +def special_render_issues_for_mermaid(text): + # 用不太优雅的方式处理一个core_functional.py中出现的mermaid渲染特例: + # 我不希望"总结绘制脑图"prompt中的mermaid渲染出来 + @lru_cache(maxsize=1) + def get_special_case(): + from core_functional import get_core_functions + special_case = get_core_functions()["总结绘制脑图"]["Suffix"] + return special_case + if text.endswith(get_special_case()): text = text.replace("```mermaid", "```") + return text + + def compat_non_markdown_input(text): """ 改善非markdown输入的显示效果,例如将空格转换为 ,将换行符转换为
等。 """ - if "```" in text: # careful input:markdown输入 + text = special_render_issues_for_mermaid(text) # 处理特殊的渲染问题 return text elif "" in text: # careful input:html输入 @@ -313,19 +325,18 @@ def compat_non_markdown_input(text): @lru_cache(maxsize=128) # 使用lru缓存 -def simple_markdown_convertion(txt): +def simple_markdown_convertion(text): pre = '
' suf = "
" - if txt.startswith(pre) and txt.endswith(suf): - return txt # 已经被转化过,不需要再次转化 - - txt = compat_non_markdown_input(txt) # 兼容非markdown输入 - txt = markdown.markdown( - txt, + if text.startswith(pre) and text.endswith(suf): + return text # 已经被转化过,不需要再次转化 + text = compat_non_markdown_input(text) # 兼容非markdown输入 + text = markdown.markdown( + text, extensions=["pymdownx.superfences", "tables", "pymdownx.highlight"], extension_configs=code_highlight_configs, ) - return pre + txt + suf + return pre + text + suf def format_io(self, y):