Skip to content

Commit

Permalink
Merge pull request #2344 from 6vision/markdown_format_display
Browse files Browse the repository at this point in the history
Optimize markdown format display
  • Loading branch information
6vision authored Oct 21, 2024
2 parents da60831 + 9cc4d0e commit 8418fa7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from common.log import logger
from common.singleton import singleton
from common.time_check import time_checker
from common.utils import convert_webp_to_png
from common.utils import convert_webp_to_png, remove_markdown_symbol
from config import conf, get_appdata_dir
from lib import itchat
from lib.itchat.content import *
Expand Down Expand Up @@ -213,9 +213,11 @@ def handle_group(self, cmsg: ChatMessage):
def send(self, reply: Reply, context: Context):
receiver = context["receiver"]
if reply.type == ReplyType.TEXT:
reply.content = remove_markdown_symbol(reply.content)
itchat.send(reply.content, toUserName=receiver)
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
reply.content = remove_markdown_symbol(reply.content)
itchat.send(reply.content, toUserName=receiver)
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
elif reply.type == ReplyType.VOICE:
Expand Down
4 changes: 2 additions & 2 deletions channel/wechatcom/wechatcomapp_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
from common.log import logger
from common.singleton import singleton
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png
from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png, remove_markdown_symbol
from config import conf, subscribe_msg
from voice.audio_convert import any_to_amr, split_audio

Expand Down Expand Up @@ -52,7 +52,7 @@ def startup(self):
def send(self, reply: Reply, context: Context):
receiver = context["receiver"]
if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]:
reply_text = reply.content
reply_text = remove_markdown_symbol(reply.content)
texts = split_string_by_utf8_length(reply_text, MAX_UTF8_LEN)
if len(texts) > 1:
logger.info("[wechatcom] text too long, split into {} parts".format(len(texts)))
Expand Down
4 changes: 2 additions & 2 deletions channel/wechatmp/wechatmp_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from channel.wechatmp.wechatmp_client import WechatMPClient
from common.log import logger
from common.singleton import singleton
from common.utils import split_string_by_utf8_length
from common.utils import split_string_by_utf8_length, remove_markdown_symbol
from config import conf
from voice.audio_convert import any_to_mp3, split_audio

Expand Down Expand Up @@ -81,7 +81,7 @@ def send(self, reply: Reply, context: Context):
receiver = context["receiver"]
if self.passive_reply:
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
reply_text = reply.content
reply_text = remove_markdown_symbol(reply.content)
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
self.cache_dict[receiver].append(("text", reply_text))
elif reply.type == ReplyType.VOICE:
Expand Down
7 changes: 7 additions & 0 deletions common/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import re
from urllib.parse import urlparse
from PIL import Image
from common.log import logger
Expand Down Expand Up @@ -68,3 +69,9 @@ def convert_webp_to_png(webp_image):
except Exception as e:
logger.error(f"Failed to convert WEBP to PNG: {e}")
raise

def remove_markdown_symbol(text: str):
# 移除markdown格式,目前先移除**
if not text:
return text
return re.sub(r'\*\*(.*?)\*\*', r'\1', text)

0 comments on commit 8418fa7

Please sign in to comment.