-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_path_script.py
More file actions
42 lines (31 loc) · 1.69 KB
/
Copy pathimage_path_script.py
File metadata and controls
42 lines (31 loc) · 1.69 KB
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
import os
import re
import shutil
# Paths
posts_dir = "/Users/matthewpye/Documents/blog/mjpye.github.io/content/posts/"
attachments_dir = "/Users/matthewpye/Documents/Obsidian_Vault/Create 3 Robot/attachments/"
static_images_dir = "/Users/matthewpye/Documents/blog/mjpye.github.io/static/images/"
# Step 1: Process each markdown file in the posts directory
for filename in os.listdir(posts_dir):
if filename.endswith(".md"):
filepath = os.path.join(posts_dir, filename)
with open(filepath, "r") as file:
content = file.read()
# Step 2: Find all image links in the format ![[image.png]] or [[image.png]]
images = re.findall(r'!?\\?\\?\[\[([^\]]*\.png)\]\]', content)
# Alternate more readable version:
# images = re.findall(r'!?\[\[([^\]]+\.png)\]\]', content)
# Step 3: Replace image links and ensure URLs are correctly formatted
for image in images:
# Prepare the Markdown-compatible link
markdown_image = f"})"
# Replace both [[image.png]] and ![[image.png]] safely
content = re.sub(rf'!?\[\[{re.escape(image)}\]\]', markdown_image, content)
# Step 4: Copy the image to the Hugo static/images directory if it exists
image_source = os.path.join(attachments_dir, image)
if os.path.exists(image_source):
shutil.copy(image_source, static_images_dir)
# Step 5: Write the updated content back to the markdown file
with open(filepath, "w") as file:
file.write(content)
print("Markdown files processed and images copied successfully.")