From ae4a9c6d96eda4980660549f1d28c11549f4ac3e Mon Sep 17 00:00:00 2001 From: ump45nose <52391318+ump45nose@users.noreply.github.com> Date: Mon, 24 Aug 2026 00:31:05 +0800 Subject: [PATCH] fix: strip embedded markup before text conversion --- apps/common/tests.py | 16 ++++++++++++++++ apps/common/utils/common.py | 19 +++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 apps/common/tests.py diff --git a/apps/common/tests.py b/apps/common/tests.py new file mode 100644 index 00000000000..113d6ec6279 --- /dev/null +++ b/apps/common/tests.py @@ -0,0 +1,16 @@ +from django.test import SimpleTestCase + +from common.utils.common import markdown_to_plain_text + + +class MarkdownToPlainTextTestCase(SimpleTestCase): + def test_removes_embedded_markup_contents(self): + cases = { + 'before after': "before after", + 'before after': "before after", + 'before {"label":"private"} after': "before after", + } + + for markup, expected in cases.items(): + with self.subTest(markup=markup): + self.assertEqual(markdown_to_plain_text(markup), expected) diff --git a/apps/common/utils/common.py b/apps/common/utils/common.py index 22edd5af280..c13098f8e63 100644 --- a/apps/common/utils/common.py +++ b/apps/common/utils/common.py @@ -159,8 +159,18 @@ def _remove_empty_lines(text): def markdown_to_plain_text(md: str) -> str: + # 先移除特定媒体标签(优先级高于通用 Markdown 和 HTML 处理) + text = re.sub( + r"<(audio|video)(?:\s+[^>]*)?>.*?", + "", + md, + flags=re.DOTALL | re.IGNORECASE, + ) + text = re.sub(r"]*>", "", text) # 匹配图片标签 + # 去除表单渲染 + text = re.sub(r".*?<\/form_rander>", "", text, flags=re.DOTALL) # 移除图片 ![alt](url) - text = re.sub(r"!\[.*?\]\(.*?\)", "", md) + text = re.sub(r"!\[.*?\]\(.*?\)", "", text) # 移除链接 [text](url) text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text) # 移除 Markdown 标题符号 (#, ##, ###) @@ -179,15 +189,8 @@ def markdown_to_plain_text(md: str) -> str: text = re.sub(r"\n{2,}", "\n", text) # 使用正则表达式去除所有 HTML 标签 text = re.sub(r"<[^>]+>", "", text) - # 先移除特定媒体标签(优先级高于通用HTML标签移除) - text = re.sub( - r"<(?:audio|video)(?:\s+[^>]*)?>.*?(?:)?", "", text, flags=re.DOTALL | re.IGNORECASE - ) - text = re.sub(r"]*>", "", text) # 匹配图片标签 # 去除多余的空白字符(包括换行符、制表符等) text = re.sub(r"\s+", " ", text) - # 去除表单渲染 - text = re.sub(r".*?<\/form_rander>", "", text, flags=re.DOTALL) # 去除首尾空格 text = text.strip() return text