Skip to content

Commit

Permalink
prevent html rendering for input
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-husky committed Jan 5, 2025
1 parent 9487829 commit 4ca331f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions shared_utils/advanced_markdown_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import os
import math
import html

from loguru import logger
from textwrap import dedent
Expand Down Expand Up @@ -421,6 +422,14 @@ def get_special_case():
return text


def contain_html_tag(text):
"""
判断文本中是否包含HTML标签。
"""
pattern = r'</?([a-zA-Z0-9_]{3,16})>|<script\s+[^>]*src=["\']([^"\']+)["\'][^>]*>'
return re.search(pattern, text) is not None


def compat_non_markdown_input(text):
"""
改善非markdown输入的显示效果,例如将空格转换为&nbsp;,将换行符转换为</br>等。
Expand All @@ -429,9 +438,10 @@ def compat_non_markdown_input(text):
# careful input:markdown输入
text = special_render_issues_for_mermaid(text) # 处理特殊的渲染问题
return text
elif "</div>" in text:
elif ("<" in text) and (">" in text) and contain_html_tag(text):
# careful input:html输入
return text
escaped_text = html.escape(text)
return escaped_text
else:
# whatever input:非markdown输入
lines = text.split("\n")
Expand Down

0 comments on commit 4ca331f

Please sign in to comment.