Skip to content

Commit e041954

Browse files
committed
infra/gen-view: Show possibly broken links
Show links that do not match expected patterns after solving references. Signed-off-by: Alex Apostolescu <[email protected]>
1 parent 30e4d26 commit e041954

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

gen-view.py

+32
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ def solve_links(filename: str, fileToLab: dict) -> str:
176176
f.write(text)
177177

178178

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+
179208
class Lab:
180209
def __init__(self, title: str, filename: str, content: List[str]):
181210
self.text = f"# {title}\n\n"
@@ -266,6 +295,9 @@ def main():
266295
if f.endswith(".md"):
267296
solve_links(os.path.join(root, f), config.get_file_to_lab_dict())
268297

298+
# Check for broken links
299+
find_broken_links()
300+
269301

270302
if __name__ == "__main__":
271303
main()

0 commit comments

Comments
 (0)