Skip to content

Commit 245c006

Browse files
fix: only HTML-escape codespan in HTML render
1 parent 0772c78 commit 245c006

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/mistune/inline_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
parse_link_text,
2020
unescape_char,
2121
)
22-
from .util import escape, escape_url, unikey
22+
from .util import escape_url, unikey
2323

2424
PAREN_END_RE = re.compile(r'\s*\)')
2525

@@ -310,7 +310,7 @@ def parse_codespan(self, m: Match[str], state: InlineState) -> int:
310310
if len(code.strip()):
311311
if code.startswith(' ') and code.endswith(' '):
312312
code = code[1:-1]
313-
state.append_token({'type': 'codespan', 'raw': escape(code)})
313+
state.append_token({'type': 'codespan', 'raw': code})
314314
return end_pos
315315
else:
316316
state.append_token({'type': 'text', 'raw': marker})

src/mistune/renderers/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def image(self, text: str, url: str, title: Optional[str] = None) -> str:
9191
return s + ' />'
9292

9393
def codespan(self, text: str) -> str:
94-
return '<code>' + text + '</code>'
94+
return '<code>' + escape_text(text) + '</code>'
9595

9696
def linebreak(self) -> str:
9797
return '<br />\n'

tests/test_misc.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_markdown_func(self):
7777

7878
def test_ast_output(self):
7979
md = mistune.create_markdown(escape=False, renderer=None)
80-
text = '# h1\n\nfoo **bar**'
80+
text = '# h1\n\nfoo **bar**\n\n`&<>"`'
8181
result = md(text)
8282
expected = [
8383
{
@@ -94,6 +94,13 @@ def test_ast_output(self):
9494
{'type': 'strong', 'children': [{'type': 'text', 'raw': 'bar'}]}
9595
]
9696
},
97+
{'type': 'blank_line'},
98+
{
99+
'type': 'paragraph',
100+
'children': [
101+
{'type': 'codespan', 'raw': '&<>"'},
102+
]
103+
},
97104
]
98105
self.assertEqual(result, expected)
99106

0 commit comments

Comments
 (0)