Skip to content
Snippets Groups Projects
Verified Commit a25de5a1 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Change urlencode to quote (didn't read the docs) and fix replace bug

parent 7bf5923d
No related branches found
No related tags found
No related merge requests found
......@@ -3,10 +3,10 @@ import os
import logging
import shutil
import re
from urllib.parse import urlencode
from urllib.parse import quote
# MD_IMAGE_REGEX = r'!\[(?<altText>.*)\]\s*\((?<imageURL>.+)\)|img\s*src="(?<imageURL1>[^"]*)"\s*alt="(?<altText1>[^"]*)" \/>|img\s*alt="(?<altText2>[^"]*)"\s*src="(?<imageURL2>[^"]*)" \/>'
OBSIDIAN_IMAGE_REGEX = r'!\[\[(.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?\]\]'
OBSIDIAN_IMAGE_REGEX = r'!\[\[((.+\.(png|jpe?g|avif|bmp|gif|svg|webp))(?:\|[0-9]+(?:x[0-9]+)?)?)\]\]'
# Initialise logging
logging.basicConfig(level=logging.INFO)
......@@ -56,8 +56,12 @@ for filename in os.listdir(posts_target_dir):
images = re.findall(OBSIDIAN_IMAGE_REGEX, content)
for image in images:
# image[0] is the full match (e.g. filename.png|100x100)
# image[1] is the image name (e.g. filename.png)
# image[2] is the image extension (e.g. png)
# Get the image name
image_name = image[0]
image_name = image[1]
image_path = os.path.join(images_source_dir, image_name)
logger.debug(f"Processing image {image_name}")
......@@ -73,9 +77,9 @@ for filename in os.listdir(posts_target_dir):
shutil.copy(image_path, target_image_path)
# Replace the image path in the content
image_url_name = urlencode(image_name)
image_url_name = quote(image_name)
md_link = f"![{image_name}](/images/{image_url_name})"
content = content.replace(f"![[{image_name}]]", md_link)
content = content.replace(f"![[{image[0]}]]", md_link)
# Write the updated content back to the file
with open(filepath, "w") as file:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment