Support for markdown images

merge-requests/21/head
Bob Mottram 2021-02-26 15:20:43 +00:00
parent 43e3a70d1e
commit 7dbaaa267a
2 changed files with 35 additions and 15 deletions

View File

@ -3306,14 +3306,16 @@ def testMarkdownToHtml():
markdown = \ markdown = \
'This is [a link](https://something.somewhere) to something.\n' + \ 'This is [a link](https://something.somewhere) to something.\n' + \
'And [something else](https://cat.pic).' 'And [something else](https://cat.pic).\n' + \
'Or ![pounce](/cat.jpg).'
assert markdownToHtml(markdown) == \ assert markdownToHtml(markdown) == \
'This is <a href="https://something.somewhere" ' + \ 'This is <a href="https://something.somewhere" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \ 'target="_blank" rel="nofollow noopener noreferrer">' + \
'a link</a> to something.<br>' + \ 'a link</a> to something.<br>' + \
'And <a href="https://cat.pic" ' + \ 'And <a href="https://cat.pic" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \ 'target="_blank" rel="nofollow noopener noreferrer">' + \
'something else</a>.' 'something else</a>.<br>' + \
'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.'
def runAllTests(): def runAllTests():

View File

@ -66,31 +66,49 @@ def _markdownEmphasisHtml(markdown: str) -> str:
return markdown return markdown
def markdownToHtml(markdown: str) -> str: def _markdownReplaceLinks(markdown: str, images=False) -> str:
"""Converts markdown formatted text to html """Replaces markdown links with html
""" """
markdown = _markdownEmphasisHtml(markdown)
# replace markdown style links with html links
replaceLinks = {} replaceLinks = {}
text = markdown text = markdown
while '[' in text: startChars = '['
if images:
startChars = '!['
while startChars in text:
if ')' not in text: if ')' not in text:
break break
text = text.split('[', 1)[1] text = text.split(startChars, 1)[1]
markdownLink = '[' + text.split(')')[0] + ')' markdownLink = startChars + text.split(')')[0] + ')'
if ']' not in markdownLink or \ if ']' not in markdownLink or \
'(' not in markdownLink: '(' not in markdownLink:
text = text.split(')', 1)[1] text = text.split(')', 1)[1]
continue continue
replaceLinks[markdownLink] = \ if not images:
'<a href="' + \ replaceLinks[markdownLink] = \
markdownLink.split('(')[1].split(')')[0] + \ '<a href="' + \
'" target="_blank" rel="nofollow noopener noreferrer">' + \ markdownLink.split('(')[1].split(')')[0] + \
markdownLink.split('[')[1].split(']')[0] + \ '" target="_blank" rel="nofollow noopener noreferrer">' + \
'</a>' markdownLink.split(startChars)[1].split(']')[0] + \
'</a>'
else:
replaceLinks[markdownLink] = \
'<img class="markdownImage" src="' + \
markdownLink.split('(')[1].split(')')[0] + \
'" alt="' + \
markdownLink.split(startChars)[1].split(']')[0] + \
'" />'
text = text.split(')', 1)[1] text = text.split(')', 1)[1]
for mdLink, htmlLink in replaceLinks.items(): for mdLink, htmlLink in replaceLinks.items():
markdown = markdown.replace(mdLink, htmlLink) markdown = markdown.replace(mdLink, htmlLink)
return markdown
def markdownToHtml(markdown: str) -> str:
"""Converts markdown formatted text to html
"""
markdown = _markdownEmphasisHtml(markdown)
markdown = _markdownReplaceLinks(markdown, True)
markdown = _markdownReplaceLinks(markdown)
# replace headers # replace headers
linesList = markdown.split('\n') linesList = markdown.split('\n')