fix(markdown): decode a percent-encoded link before checking that it exists

Каждый markdown-рендерер процентно кодирует ссылку на файл с пробелами или
не-ASCII в имени. Проверка сравнивала закодированную форму с файловой системой и
объявляла отсутствующим файл, который лежит на месте:

  MARKDOWN_LINK_TARGET_MISSING: GitLab%2017.10.0%20%E2%86%92%20Docker%20Migration.md

Ложное срабатывание блокировало `validate` на репозитории Infra/Docker. Ссылка
декодируется перед резолвом; тест закрепляет и пробел, и то, что настоящая
битая ссылка по-прежнему находится.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Alexander Baranov
2026-08-27 12:16:05 +03:00
co-authored by Claude Opus 5
parent 8b6c01e942
commit c607a14f8b
2 changed files with 15 additions and 1 deletions
+6 -1
View File
@@ -10,6 +10,8 @@ from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from urllib.parse import unquote
from .io import ResourceError from .io import ResourceError
from .types import Finding 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] = [] findings: list[Finding] = []
base_dir = (repo_root / rel_path).parent base_dir = (repo_root / rel_path).parent
for match in _LINK_RE.finditer(mask_code(text)): 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:", "#")): if target.startswith(("http://", "https://", "mailto:", "#")):
continue continue
target_path_part = target.split("#", 1)[0] target_path_part = target.split("#", 1)[0]
+9
View File
@@ -316,6 +316,15 @@ def test_a_real_broken_link_is_still_reported(tmp_path: Path) -> None:
assert codes == ["MARKDOWN_LINK_TARGET_MISSING"] 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 ------------------ # --- path confinement: "." is a whole path, never a segment ------------------
def test_repository_root_is_a_legal_content_root_path() -> None: def test_repository_root_is_a_legal_content_root_path() -> None: