@@ -24,7 +24,10 @@ def load_mapping():
24
24
def convert_relative_links (content , source_path ):
25
25
"""Convert relative links in markdown content to absolute URLs."""
26
26
# Find markdown links with regex pattern [text](url)
27
- pattern = r'\[([^\]]+)\]\(([^)]+)\)'
27
+ md_pattern = r'\[([^\]]+)\]\(([^)]+)\)'
28
+
29
+ # Find HTML img tags
30
+ img_pattern = r'<img\s+src=[\'"]([^\'"]+)[\'"]'
28
31
29
32
def replace_link (match ):
30
33
link_text = match .group (1 )
@@ -51,8 +54,37 @@ def replace_link(match):
51
54
github_url = f"{ REPO_URL } /blob/main{ abs_path } "
52
55
return f"[{ link_text } ]({ github_url } )"
53
56
54
- # Replace all links
55
- return re .sub (pattern , replace_link , content )
57
+ def replace_img_src (match ):
58
+ img_src = match .group (1 )
59
+
60
+ # Skip if already absolute URL
61
+ if img_src .startswith (('http://' , 'https://' )):
62
+ return match .group (0 )
63
+
64
+ # Get the directory of the source file
65
+ source_dir = os .path .dirname (source_path )
66
+
67
+ # Create absolute path from repository root
68
+ if img_src .startswith ('/' ):
69
+ # If link starts with /, it's already relative to repo root
70
+ abs_path = img_src
71
+ else :
72
+ # Otherwise, it's relative to the file location
73
+ abs_path = os .path .normpath (os .path .join (source_dir , img_src ))
74
+ if not abs_path .startswith ('/' ):
75
+ abs_path = '/' + abs_path
76
+
77
+ # Convert to GitHub URL (use raw URL for images)
78
+ github_url = f"{ REPO_URL } /raw/main{ abs_path } "
79
+ return f'<img src="{ github_url } "'
80
+
81
+ # Replace all markdown links
82
+ content = re .sub (md_pattern , replace_link , content )
83
+
84
+ # Replace all img src tags
85
+ content = re .sub (img_pattern , replace_img_src , content )
86
+
87
+ return content
56
88
57
89
def process_file (source_file , target_wiki_page ):
58
90
"""Process a markdown file and copy its contents to a wiki page."""
0 commit comments