Skip to content

Commit 4ca331f

Browse files
committed
prevent html rendering for input
1 parent 9487829 commit 4ca331f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

shared_utils/advanced_markdown_format.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import os
44
import math
5+
import html
56

67
from loguru import logger
78
from textwrap import dedent
@@ -421,6 +422,14 @@ def get_special_case():
421422
return text
422423

423424

425+
def contain_html_tag(text):
426+
"""
427+
判断文本中是否包含HTML标签。
428+
"""
429+
pattern = r'</?([a-zA-Z0-9_]{3,16})>|<script\s+[^>]*src=["\']([^"\']+)["\'][^>]*>'
430+
return re.search(pattern, text) is not None
431+
432+
424433
def compat_non_markdown_input(text):
425434
"""
426435
改善非markdown输入的显示效果,例如将空格转换为&nbsp;,将换行符转换为</br>等。
@@ -429,9 +438,10 @@ def compat_non_markdown_input(text):
429438
# careful input:markdown输入
430439
text = special_render_issues_for_mermaid(text) # 处理特殊的渲染问题
431440
return text
432-
elif "</div>" in text:
441+
elif ("<" in text) and (">" in text) and contain_html_tag(text):
433442
# careful input:html输入
434-
return text
443+
escaped_text = html.escape(text)
444+
return escaped_text
435445
else:
436446
# whatever input:非markdown输入
437447
lines = text.split("\n")

0 commit comments

Comments
 (0)