-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown_formatter.py
44 lines (33 loc) · 1.31 KB
/
markdown_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import re
import base64
from pathlib import Path
import os
def markdown_images(markdown):
# example image markdown:
# ![Test image](images/test.png "Alternate text")
images = re.findall(
r'(!\[(?P<image_title>[^\]]+)\]\((?P<image_path>[^\)"\s]+)\s*([^\)]*)\))',
markdown,
)
return images
def img_to_bytes(img_path):
img_bytes = Path(img_path).read_bytes()
encoded = base64.b64encode(img_bytes).decode()
return encoded
def img_to_html(img_path, img_alt, session_id):
img_format = img_path.split(".")[-1]
#img_html = f'<img src="data:stories/{session_id}/{img_format.lower()};base64,{img_to_bytes(img_path)}" alt="{img_alt}" style="max-width: 100%;">'
img_html = f'<img src="data:stories/{session_id}{img_format.lower()};base64,{img_to_bytes(img_path)}" alt="{img_alt}" style="max-width: 100%;">'
return img_html
def markdown_insert_images(markdown, session_id):
images = markdown_images(markdown)
for image in images:
image_markdown = image[0]
image_alt = image[1]
image_path = image[2]
image_path = f"stories/{session_id}/{image_path}"
if os.path.exists(image_path):
markdown = markdown.replace(
image_markdown, img_to_html(image_path, image_alt, session_id)
)
return markdown