mirror of
https://git.quad4.io/RNS-Things/MeshChatX.git
synced 2026-05-24 18:05:16 +00:00
Refactor MarkdownRenderer regex patterns for improved matching
- Updated regex patterns in the MarkdownRenderer to use non-greedy matching for better accuracy in text rendering. - Enhanced the test for header rendering to ensure correct HTML structure and added assertions for escaping content without markdown special characters.
This commit is contained in:
@@ -70,12 +70,12 @@ class MarkdownRenderer:
|
||||
)
|
||||
|
||||
# Bold and Italic
|
||||
text = re.sub(r"\*\*\*(.*?)\*\*\*", r"<strong><em>\1</em></strong>", text)
|
||||
text = re.sub(r"\*\*(.*?)\*\*", r"<strong>\1</strong>", text)
|
||||
text = re.sub(r"\*(?!\s)(.*?)(?<!\s)\*", r"<em>\1</em>", text)
|
||||
text = re.sub(r"___(.*?)___", r"<strong><em>\1</em></strong>", text)
|
||||
text = re.sub(r"__(.*?)__", r"<strong>\1</strong>", text)
|
||||
text = re.sub(r"_(?!\s)(.*?)(?<!\s)_", r"<em>\1</em>", text)
|
||||
text = re.sub(r"\*\*\*(.+?)\*\*\*", r"<strong><em>\1</em></strong>", text)
|
||||
text = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", text)
|
||||
text = re.sub(r"\*(?!\s)(.+?)(?<!\s)\*", r"<em>\1</em>", text)
|
||||
text = re.sub(r"___(.+?)___", r"<strong><em>\1</em></strong>", text)
|
||||
text = re.sub(r"__(.+?)__", r"<strong>\1</strong>", text)
|
||||
text = re.sub(r"_(?!\s)(.+?)(?<!\s)_", r"<em>\1</em>", text)
|
||||
|
||||
# Strikethrough
|
||||
text = re.sub(r"~~(.*?)~~", r"<del>\1</del>", text)
|
||||
|
||||
@@ -333,12 +333,18 @@ def test_markdown_renderer_xss_protection(text):
|
||||
assert "<script>" in result
|
||||
|
||||
|
||||
@given(content=st.text())
|
||||
@given(content=st.text().filter(lambda x: x and "\n" not in x and "#" not in x))
|
||||
def test_markdown_renderer_headers(content):
|
||||
if content and "\n" not in content:
|
||||
input_text = f"# {content}"
|
||||
result = MarkdownRenderer.render(input_text)
|
||||
assert "<h1" in result
|
||||
input_text = f"# {content}"
|
||||
result = MarkdownRenderer.render(input_text)
|
||||
assert "<h1" in result
|
||||
# Check that it's correctly wrapped in h1
|
||||
assert result.startswith('<h1')
|
||||
assert result.endswith('</h1>')
|
||||
|
||||
# If the content doesn't contain markdown special chars, we can expect it to be there escaped
|
||||
# This is a safer assertion for property-based testing
|
||||
if not any(c in content for c in "*_~`[]()"):
|
||||
assert html.escape(content) in result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user