Skip to content

Commit 9cc4d0e

Browse files
committed
Optimize markdown format display
1 parent 0773174 commit 9cc4d0e

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

channel/wechat/wechat_channel.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from common.log import logger
2121
from common.singleton import singleton
2222
from common.time_check import time_checker
23-
from common.utils import convert_webp_to_png
23+
from common.utils import convert_webp_to_png, remove_markdown_symbol
2424
from config import conf, get_appdata_dir
2525
from lib import itchat
2626
from lib.itchat.content import *
@@ -213,9 +213,11 @@ def handle_group(self, cmsg: ChatMessage):
213213
def send(self, reply: Reply, context: Context):
214214
receiver = context["receiver"]
215215
if reply.type == ReplyType.TEXT:
216+
reply.content = remove_markdown_symbol(reply.content)
216217
itchat.send(reply.content, toUserName=receiver)
217218
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
218219
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
220+
reply.content = remove_markdown_symbol(reply.content)
219221
itchat.send(reply.content, toUserName=receiver)
220222
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
221223
elif reply.type == ReplyType.VOICE:

channel/wechatcom/wechatcomapp_channel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
1818
from common.log import logger
1919
from common.singleton import singleton
20-
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png
20+
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png, remove_markdown_symbol
2121
from config import conf, subscribe_msg
2222
from voice.audio_convert import any_to_amr, split_audio
2323

@@ -52,7 +52,7 @@ def startup(self):
5252
def send(self, reply: Reply, context: Context):
5353
receiver = context["receiver"]
5454
if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]:
55-
reply_text = reply.content
55+
reply_text = remove_markdown_symbol(reply.content)
5656
texts = split_string_by_utf8_length(reply_text, MAX_UTF8_LEN)
5757
if len(texts) > 1:
5858
logger.info("[wechatcom] text too long, split into {} parts".format(len(texts)))

channel/wechatmp/wechatmp_channel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from channel.wechatmp.wechatmp_client import WechatMPClient
2020
from common.log import logger
2121
from common.singleton import singleton
22-
from common.utils import split_string_by_utf8_length
22+
from common.utils import split_string_by_utf8_length, remove_markdown_symbol
2323
from config import conf
2424
from voice.audio_convert import any_to_mp3, split_audio
2525

@@ -81,7 +81,7 @@ def send(self, reply: Reply, context: Context):
8181
receiver = context["receiver"]
8282
if self.passive_reply:
8383
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
84-
reply_text = reply.content
84+
reply_text = remove_markdown_symbol(reply.content)
8585
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
8686
self.cache_dict[receiver].append(("text", reply_text))
8787
elif reply.type == ReplyType.VOICE:

common/utils.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io
22
import os
3+
import re
34
from urllib.parse import urlparse
45
from PIL import Image
56
from common.log import logger
@@ -68,3 +69,9 @@ def convert_webp_to_png(webp_image):
6869
except Exception as e:
6970
logger.error(f"Failed to convert WEBP to PNG: {e}")
7071
raise
72+
73+
def remove_markdown_symbol(text: str):
74+
# 移除markdown格式,目前先移除**
75+
if not text:
76+
return text
77+
return re.sub(r'\*\*(.*?)\*\*', r'\1', text)

0 commit comments

Comments
 (0)