Don't replace links within code sections

main
Bob Mottram 2022-06-27 12:14:42 +01:00
parent e65196a897
commit ab969ffb64
2 changed files with 26 additions and 2 deletions

View File

@ -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 '<code>' in line:
code_section = True
else:
if '</code>' in line:
code_section = False
if code_section:
markdown += line
ctr += 1
continue
markdown += line.replace(md_link, html_link)
ctr += 1
return markdown

View File

@ -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 href="https://something.somewhere" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
'a link</a> to something.<br>' + \
@ -5779,6 +5779,10 @@ def _test_markdown_to_html():
'target="_blank" rel="nofollow noopener noreferrer">' + \
'something else</a>.<br>' + \
'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.'
result = markdown_to_html(markdown)
if result != expected:
print(result)
assert result == expected
def _test_extract_text_fields_from_post():