Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/common/tests.py
Original file line number Diff line number Diff line change
@@ -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 <audio src="clip.mp3">audio fallback</audio> after': "before after",
'before <video src="clip.mp4">video fallback</video> after': "before after",
'before <form_rander>{"label":"private"}</form_rander> after': "before after",
}

for markup, expected in cases.items():
with self.subTest(markup=markup):
self.assertEqual(markdown_to_plain_text(markup), expected)
19 changes: 11 additions & 8 deletions apps/common/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+[^>]*)?>.*?</\1>",
"",
md,
flags=re.DOTALL | re.IGNORECASE,
)
text = re.sub(r"<img[^>]*>", "", text) # 匹配图片标签
# 去除表单渲染
text = re.sub(r"<form_rander>.*?<\/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 标题符号 (#, ##, ###)
Expand All @@ -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+[^>]*)?>.*?(?:</(?:audio|video)>)?", "", text, flags=re.DOTALL | re.IGNORECASE
)
text = re.sub(r"<img[^>]*>", "", text) # 匹配图片标签
# 去除多余的空白字符(包括换行符、制表符等)
text = re.sub(r"\s+", " ", text)
# 去除表单渲染
text = re.sub(r"<form_rander>.*?<\/form_rander>", "", text, flags=re.DOTALL)
# 去除首尾空格
text = text.strip()
return text
Expand Down
Loading