Skip to content

Commit

Permalink
fix: only HTML-escape codespan in HTML render
Browse files Browse the repository at this point in the history
  • Loading branch information
mentalisttraceur committed Oct 31, 2024
1 parent 0772c78 commit 245c006
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/mistune/inline_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
parse_link_text,
unescape_char,
)
from .util import escape, escape_url, unikey
from .util import escape_url, unikey

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

Expand Down Expand Up @@ -310,7 +310,7 @@ def parse_codespan(self, m: Match[str], state: InlineState) -> int:
if len(code.strip()):
if code.startswith(' ') and code.endswith(' '):
code = code[1:-1]
state.append_token({'type': 'codespan', 'raw': escape(code)})
state.append_token({'type': 'codespan', 'raw': code})
return end_pos
else:
state.append_token({'type': 'text', 'raw': marker})
Expand Down
2 changes: 1 addition & 1 deletion src/mistune/renderers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def image(self, text: str, url: str, title: Optional[str] = None) -> str:
return s + ' />'

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

def linebreak(self) -> str:
return '<br />\n'
Expand Down
9 changes: 8 additions & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_markdown_func(self):

def test_ast_output(self):
md = mistune.create_markdown(escape=False, renderer=None)
text = '# h1\n\nfoo **bar**'
text = '# h1\n\nfoo **bar**\n\n`&<>"`'
result = md(text)
expected = [
{
Expand All @@ -94,6 +94,13 @@ def test_ast_output(self):
{'type': 'strong', 'children': [{'type': 'text', 'raw': 'bar'}]}
]
},
{'type': 'blank_line'},
{
'type': 'paragraph',
'children': [
{'type': 'codespan', 'raw': '&<>"'},
]
},
]
self.assertEqual(result, expected)

Expand Down

0 comments on commit 245c006

Please sign in to comment.