Skip to content

Commit 8702b27

Browse files
committed
Convert relative links to raw GitHub links [automated]
1 parent 6891807 commit 8702b27

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

replace_links.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
import re
33

44
def main():
5-
if len(sys.argv) != 5:
6-
print("Usage: python3 script.py file repo_owner repo_name commit_sha")
5+
if len(sys.argv) != 6:
6+
print("Usage: python3 script.py file repo_owner repo_name commit_sha branch")
77
sys.exit(1)
88

99
file_path = sys.argv[1]
1010
repo_owner = sys.argv[2]
1111
repo_name = sys.argv[3]
1212
commit_sha = sys.argv[4]
13+
branch = sys.argv[5]
1314

14-
base_url = f"https://raw.githubusercontent.com/{repo_owner}/{repo_name}/{commit_sha}/"
15+
base_url = f"https://github.com/{repo_owner}/{repo_name}/blob/{branch}/"
1516

1617
try:
1718
with open(file_path, 'r', encoding='utf-8') as f:
@@ -22,21 +23,21 @@ def main():
2223
# Pattern 1: HTML img tags with relative paths
2324
content = re.sub(
2425
r'<img([^>]*?)src="(\.\.?/[^"]*)"',
25-
f'<img\\1src="{base_url}\\2"',
26+
f'<img\\1src="{base_url}\\2?raw=true"',
2627
content
2728
)
2829

2930
# Pattern 2: Markdown image syntax
3031
content = re.sub(
3132
r'!\[([^\]]*?)\]\((\.\.?/[^)]*?)\)',
32-
f'![\\1]({base_url}\\2)',
33+
f'![\\1]({base_url}\\2?raw=true)',
3334
content
3435
)
3536

3637
# Pattern 3: Markdown links to images
3738
content = re.sub(
3839
r'\[([^\]]*?)\]\((\.\.?/[^)]*?\.(png|jpg|jpeg|gif|svg|webp))\)',
39-
f'[\\1]({base_url}\\2)',
40+
f'[\\1]({base_url}\\2?raw=true)',
4041
content,
4142
flags=re.IGNORECASE
4243
)

0 commit comments

Comments
 (0)