Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions scripts/check_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,24 @@ def get_file_ids(path):

parsed = urlparse(link)
if parsed.scheme and parsed.scheme in ("http", "https"):
if not link.startswith(SITE_URL):
parsed_site = urlparse(SITE_URL)
if parsed.hostname == parsed_site.hostname:
path = parsed.path if parsed.path else "/"
site_path = parsed_site.path
site_path_no_slash = site_path.rstrip("/")
if (path == site_path_no_slash) or path.startswith(site_path):
if path.startswith(site_path):
rel_link_path = "/" + path[len(site_path) :]
else:
rel_link_path = "/"
query_part = f"?{parsed.query}" if parsed.query else ""
fragment_part = f"#{parsed.fragment}" if parsed.fragment else ""
link = rel_link_path + query_part + fragment_part
parsed = urlparse(link)
else:
continue # External link
else:
continue # External link
# Internal absolute URL (e.g. https://ucp.dev/foo) -> /foo
link = link[len(SITE_URL) - 1 :] # Keep the leading slash

path_part = parsed.path
anchor_part = parsed.fragment
Expand All @@ -176,6 +190,7 @@ def get_file_ids(path):
path_part = "/" + path_part[len(SITE_BASE_PATH) :]

target_file = None
is_unbuilt_version = False

# Resolve Target File
if not path_part:
Expand All @@ -197,7 +212,14 @@ def get_file_ids(path):
)
and not (ROOT_DIR / parts[0]).exists()
):
rel_path = parts[1]
is_unbuilt_version = True
stripped_path = parts[1]
if (
(ROOT_DIR / stripped_path).exists()
or (ROOT_DIR / (stripped_path + ".html")).exists()
or (ROOT_DIR / stripped_path / "index.html").exists()
):
rel_path = stripped_path

target_file = ROOT_DIR / rel_path
else:
Expand All @@ -218,11 +240,15 @@ def get_file_ids(path):
if candidate.exists():
target_file = candidate
else:
if is_unbuilt_version:
continue
errors_by_version[version][str(file_path)].append(
f" Link: {original_link}\n Target: {target_file} (Not Found)"
)
continue
else:
if is_unbuilt_version:
continue
errors_by_version[version][str(file_path)].append(
f" Link: {original_link}\n Target: {target_file} (Not Found)"
)
Expand Down
Loading