diff --git a/src/stewardship/markdown.py b/src/stewardship/markdown.py index fb3ccf1..e90b20f 100644 --- a/src/stewardship/markdown.py +++ b/src/stewardship/markdown.py @@ -10,6 +10,8 @@ from dataclasses import dataclass from pathlib import Path from typing import Any +from urllib.parse import unquote + from .io import ResourceError from .types import Finding @@ -183,7 +185,10 @@ def _check_local_links(repo_root: Path, rel_path: str, text: str) -> list[Findin findings: list[Finding] = [] base_dir = (repo_root / rel_path).parent for match in _LINK_RE.finditer(mask_code(text)): - target = match.group(1) + # A link to a name with spaces or non-ASCII is percent-encoded by every + # Markdown renderer that produces one. Comparing the encoded form against + # the filesystem reports a file that is plainly there as missing. + target = unquote(match.group(1)) if target.startswith(("http://", "https://", "mailto:", "#")): continue target_path_part = target.split("#", 1)[0] diff --git a/tests/test_adoption_gates.py b/tests/test_adoption_gates.py index 6b5a148..b8189d7 100644 --- a/tests/test_adoption_gates.py +++ b/tests/test_adoption_gates.py @@ -316,6 +316,15 @@ def test_a_real_broken_link_is_still_reported(tmp_path: Path) -> None: assert codes == ["MARKDOWN_LINK_TARGET_MISSING"] +def test_percent_encoded_link_resolves_to_the_real_file(tmp_path) -> None: + """Renderers percent-encode spaces and non-ASCII; the file is still there.""" + from stewardship.markdown import check_markdown_file + + (tmp_path / "GitLab 17.10.0 Migration.md").write_text("# M" + chr(10), encoding="utf-8") + link = "See [m](GitLab%2017.10.0%20Migration.md)." + (tmp_path / "CLAUDE.md").write_text("# Doc" + chr(10) * 2 + link + chr(10), encoding="utf-8") + assert check_markdown_file(tmp_path, "CLAUDE.md") == [] + # --- path confinement: "." is a whole path, never a segment ------------------ def test_repository_root_is_a_legal_content_root_path() -> None: