@@ -176,6 +176,35 @@ def solve_links(filename: str, fileToLab: dict) -> str:
176
176
f .write (text )
177
177
178
178
179
+ def find_broken_links ():
180
+ """
181
+ Find potentially broken links in the markdown file.
182
+ """
183
+ prefixes = ["lab" , "media" , "tasks" , "Questions" , "reading" , "guides" , "http" ]
184
+
185
+ for root , _ , files in os .walk (viewDir ):
186
+ for f in files :
187
+ if "lab" in f : # Skip lab files, check source files only
188
+ continue
189
+
190
+ if f .endswith (".md" ):
191
+ with open (os .path .join (root , f )) as f :
192
+ text = f .read ()
193
+
194
+ # Find all links that do not point to a markdown file
195
+ matches = re .findall (r"\[[^\]]*\]\(([^\)]+)\)" , text )
196
+ for link in matches :
197
+ # Questions media links corner case
198
+ isValidQMediaLink = "questions" in root and link .startswith (
199
+ "../media/"
200
+ )
201
+
202
+ if (
203
+ not any ([link .startswith (p ) for p in prefixes ])
204
+ ) and not isValidQMediaLink :
205
+ print (f"Possibly broken link in { f .name } : ({ link } )" )
206
+
207
+
179
208
class Lab :
180
209
def __init__ (self , title : str , filename : str , content : List [str ]):
181
210
self .text = f"# { title } \n \n "
@@ -266,6 +295,9 @@ def main():
266
295
if f .endswith (".md" ):
267
296
solve_links (os .path .join (root , f ), config .get_file_to_lab_dict ())
268
297
298
+ # Check for broken links
299
+ find_broken_links ()
300
+
269
301
270
302
if __name__ == "__main__" :
271
303
main ()
0 commit comments