Skip to content

Commit 1e0fbf2

Browse files
Merge pull request #595 from Crozzers/fix-img-alt-text
Fix img alt text being processed as markdown (#594)
2 parents 8d3a65b + 424a349 commit 1e0fbf2

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [pull #590] Fix underscores within bold text getting emphasized (#589)
66
- [pull #591] Add Alerts extra
7+
- [pull #595] Fix img alt text being processed as markdown (#594)
78

89

910
## python-markdown2 2.5.0

lib/markdown2.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ def convert(self, text: str) -> 'UnicodeWithAttrs':
516516

517517
text = self._unescape_special_chars(text)
518518

519+
text = self._unhash_html_spans(text)
519520
if self.safe_mode:
520-
text = self._unhash_html_spans(text)
521521
# return the removed text warning to its markdown.py compatible form
522522
text = text.replace(self.html_removed_text, self.html_removed_text_compat)
523523

@@ -1336,24 +1336,19 @@ def _is_comment(token):
13361336
return
13371337
return re.match(r'(<!--)(.*)(-->)', token)
13381338

1339-
def _hash(token):
1340-
key = _hash_text(token)
1341-
self.html_spans[key] = token
1342-
return key
1343-
13441339
tokens = []
13451340
split_tokens = self._sorta_html_tokenize_re.split(text)
13461341
is_html_markup = False
13471342
for index, token in enumerate(split_tokens):
13481343
if is_html_markup and not _is_auto_link(token) and not _is_code_span(index, token):
13491344
is_comment = _is_comment(token)
13501345
if is_comment:
1351-
tokens.append(_hash(self._sanitize_html(is_comment.group(1))))
1346+
tokens.append(self._hash_span(self._sanitize_html(is_comment.group(1))))
13521347
# sanitise but leave comment body intact for further markdown processing
13531348
tokens.append(self._sanitize_html(is_comment.group(2)))
1354-
tokens.append(_hash(self._sanitize_html(is_comment.group(3))))
1349+
tokens.append(self._hash_span(self._sanitize_html(is_comment.group(3))))
13551350
else:
1356-
tokens.append(_hash(self._sanitize_html(token)))
1351+
tokens.append(self._hash_span(self._sanitize_html(token)))
13571352
else:
13581353
tokens.append(self._encode_incomplete_tags(token))
13591354
is_html_markup = not is_html_markup
@@ -1600,7 +1595,7 @@ def _do_links(self, text: str) -> str:
16001595
img_class_str = self._html_class_str_from_tag("img")
16011596
result = '<img src="%s" alt="%s"%s%s%s' \
16021597
% (self._protect_url(url),
1603-
_xml_escape_attr(link_text),
1598+
self._hash_span(_xml_escape_attr(link_text)),
16041599
title_str,
16051600
img_class_str,
16061601
self.empty_element_suffix)
@@ -1657,7 +1652,7 @@ def _do_links(self, text: str) -> str:
16571652
img_class_str = self._html_class_str_from_tag("img")
16581653
result = '<img src="%s" alt="%s"%s%s%s' \
16591654
% (self._protect_url(url),
1660-
_xml_escape_attr(link_text),
1655+
self._hash_span(_xml_escape_attr(link_text)),
16611656
title_str,
16621657
img_class_str,
16631658
self.empty_element_suffix)
@@ -2422,6 +2417,15 @@ def _outdent(self, text: str) -> str:
24222417
# Remove one level of line-leading tabs or spaces
24232418
return self._outdent_re.sub('', text)
24242419

2420+
def _hash_span(self, text: str) -> str:
2421+
'''
2422+
Wrapper around `_hash_text` that also adds the hash to `self.hash_spans`,
2423+
meaning it will be automatically unhashed during conversion.
2424+
'''
2425+
key = _hash_text(text)
2426+
self.html_spans[key] = text
2427+
return key
2428+
24252429
@staticmethod
24262430
def _uniform_outdent(
24272431
text: str,

test/tm-cases/img_alt_text.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><img src="d" alt="a*b*c" /></p>

test/tm-cases/img_alt_text.text

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![a*b*c](d)

0 commit comments

Comments
 (0)