fix(website): images missing from website

TinyTapeout/tinytapeout_www#84
This commit is contained in:
Uri Shaked
2024-04-22 00:08:35 +03:00
parent 1f66d890ef
commit 7f23cab1de
2 changed files with 38 additions and 1 deletions
+32
View File
@@ -1,4 +1,5 @@
import os
import shutil
from typing import Any, Dict
import mistune
@@ -42,3 +43,34 @@ class ImagePathRewriterRenderer(MarkdownRenderer):
def rewrite_image_paths(source: str, prefix: str) -> str:
markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix))
return markdown(source)
class WebsiteImagePathRewriterRenderer(MarkdownRenderer):
def __init__(self, source_dir: str, target_dir: str):
super().__init__()
self.source_dir = source_dir
self.target_dir = target_dir
def image(self, token, state):
url = token["attrs"]["url"]
if ".." in url:
token["attrs"]["url"] = ""
elif "://" not in url and not url.startswith("/"):
filename = os.path.basename(url)
if url.startswith("/"):
url = "../" + url[1:]
shutil.copyfile(
os.path.join(self.source_dir, url),
os.path.join(self.target_dir, filename),
)
token["attrs"]["url"] = f"images/{filename}"
return super().image(token, state)
def rewrite_image_paths_for_website(
source: str, source_dir: str, target_dir: str
) -> str:
markdown = mistune.create_markdown(
renderer=WebsiteImagePathRewriterRenderer(source_dir, target_dir)
)
return markdown(source)