mirror of https://gitlab.com/bashrc2/epicyon
Don't replace links within code sections
parent
e65196a897
commit
ab969ffb64
22
markdown.py
22
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 '<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
|
||||
|
||||
|
||||
|
|
6
tests.py
6
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 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():
|
||||
|
|
Loading…
Reference in New Issue