-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_images.py
More file actions
37 lines (29 loc) · 877 Bytes
/
test_images.py
File metadata and controls
37 lines (29 loc) · 877 Bytes
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
import re
import os
import glob
# Find all markdown files
md_files = glob.glob('*.md')
md_files += glob.glob('*/*.md')
pattern = re.compile(r'!\[.*?\]\((.*?)\)')
broken = []
for md in md_files:
with open(md, 'r', encoding='utf-8') as f:
content = f.read()
links = pattern.findall(content)
for link in links:
path = link.strip()
# if the path is remote, skip
if path.startswith('http'):
continue
# remove fragments/query
if '?' in path:
path = path.split('?')[0]
if '#' in path:
path = path.split('#')[0]
# resolve path relative to md file
dir_path = os.path.dirname(md)
full_path = os.path.join(dir_path, path)
if not os.path.exists(full_path):
broken.append((md, link))
for b in broken:
print(b)