diff --git a/markdown.py b/markdown.py
index c3c7bfff5..f64672a39 100644
--- a/markdown.py
+++ b/markdown.py
@@ -141,8 +141,28 @@ def _markdown_replace_links(markdown: str, images: bool = False) -> str:
markdown_link.split(start_chars)[1].split(']')[0] + \
'" />'
text = text.split(')', 1)[1]
+
for md_link, html_link in replace_links.items():
- markdown = markdown.replace(md_link, html_link)
+ lines = markdown.split('\n')
+ markdown = ''
+ code_section = False
+ ctr = 0
+ for line in lines:
+ if ctr > 0:
+ markdown += '\n'
+ # avoid code sections
+ if not code_section:
+ if '' in line:
+ code_section = True
+ else:
+ if '
' in line:
+ code_section = False
+ if code_section:
+ markdown += line
+ ctr += 1
+ continue
+ markdown += line.replace(md_link, html_link)
+ ctr += 1
return markdown
diff --git a/tests.py b/tests.py
index cf71f0806..b35654c49 100644
--- a/tests.py
+++ b/tests.py
@@ -5771,7 +5771,7 @@ def _test_markdown_to_html():
'This is [a link](https://something.somewhere) to something.\n' + \
'And [something else](https://cat.pic).\n' + \
'Or ![pounce](/cat.jpg).'
- assert markdown_to_html(markdown) == \
+ expected = \
'This is ' + \
'a link to something.
' + \
@@ -5779,6 +5779,10 @@ def _test_markdown_to_html():
'target="_blank" rel="nofollow noopener noreferrer">' + \
'something else.
' + \
'Or .'
+ result = markdown_to_html(markdown)
+ if result != expected:
+ print(result)
+ assert result == expected
def _test_extract_text_fields_from_post():