fix(scripts): correctly resolve internal absolute root site URLs to root index.html in check_links.py - #709
Conversation
…oot index.html in check_links.py
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hi @yappermoar-boop, Thank you for this contribution! Fixing the absolute root URL resolution in the link checker is a great improvement. I have a couple of suggestions to make the internal link detection and resolution logic more robust: 1. Robust Internal Link DetectionThe current check We can make this more robust by parsing the URL and comparing the hostnames directly: parsed = urlparse(link)
is_absolute_internal = False
if parsed.scheme and parsed.scheme in ("http", "https"):
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):
is_absolute_internal = True
# Normalize link path to be relative to ROOT_DIR
if path.startswith(site_path):
rel_link_path = "/" + path[len(site_path):]
else: # path == site_path_no_slash
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)
if not is_absolute_internal:
continue # External link2. Redundant Code CleanupWith the normalization logic, internal absolute links will always have a path of at least We can revert this line back to its original state to keep it clean: # Resolve Target File
if not path_part:
target_file = file_pathThe root URL resolution (when path is What do you think? Thanks again for the PR! |
…ion per review feedback
|
Hi @damaz91 , Thank you for the thoughtful review and detailed feedback! I have updated the PR with your suggestions:
All tests pass cleanly. Thanks again! |
|
Thanks! LGTM - this PR actually uncovered some broken links - fixing those in #710 |
|
Let's wait for #710 to merge first, then we can go ahead with this |
Resend because google cla ,Updated check_links.py to correctly recognize internal absolute root site URLs (e.g. https://ucp.dev or https://ucp.dev/#anchor) and resolve them to ROOT_DIR/index.html instead of falling back to file_path. Also handles unslashed site URL matches.