| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | # The flags are one string so the formatter app and this check cannot |
| 5 | # drift; split it back into argv. |
| 6 | read -ra fmtArgs <<< "$proseFmtArgs" |
| 7 | cd "$tree" || exit 1 |
| 8 | find . -name '*.md' -print0 | xargs -0 --no-run-if-empty \ |
| 9 | prettier "${fmtArgs[@]}" --check |
| 10 | |
| 11 | python3 - <<'PY' |
| 12 | import glob |
| 13 | import os |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | LINK = re.compile(r"\]\(\s*([^)\s]+)(?:\s+\"[^\"]*\")?\s*\)") |
| 18 | INLINE_CODE = re.compile(r"(`+)[^`\n]*?\1") |
| 19 | FENCE_OPEN = re.compile(r"^\s*(`{3,}|~{3,})") |
| 20 | HEADING = re.compile(r"^#{1,6}\s+(.*?)\s*#*?\s*$") |
| 21 | MD_LINK_IN_HEADING = re.compile(r"\[([^\]]*)\]\([^)]*\)") |
| 22 | NOT_IN_SLUG = re.compile(r"[^\w\s-]") |
| 23 | EXTERNAL = re.compile(r"^[a-zA-Z][a-zA-Z0-9+.-]*:") |
| 24 | |
| 25 | |
| 26 | def outside_fences(lines): |
| 27 | fence = None |
| 28 | for number, line in enumerate(lines, start=1): |
| 29 | if fence is None: |
| 30 | m = FENCE_OPEN.match(line) |
| 31 | if m: |
| 32 | fence = m.group(1) |
| 33 | else: |
| 34 | yield number, line |
| 35 | else: |
| 36 | if re.match(rf"^\s*{fence[0]}{{{len(fence)},}}\s*$", line): |
| 37 | fence = None |
| 38 | |
| 39 | |
| 40 | def anchors(path, cache): |
| 41 | if path in cache: |
| 42 | return cache[path] |
| 43 | found, counts = set(), {} |
| 44 | with open(path, encoding="utf-8") as handle: |
| 45 | for _, line in outside_fences(handle.read().splitlines()): |
| 46 | heading = HEADING.match(line) |
| 47 | if not heading: |
| 48 | continue |
| 49 | title = MD_LINK_IN_HEADING.sub(r"\1", heading.group(1)) |
| 50 | title = title.replace("`", "").lower() |
| 51 | slug = NOT_IN_SLUG.sub("", title).strip().replace(" ", "-") |
| 52 | counts[slug] = counts.get(slug, 0) + 1 |
| 53 | found.add(slug if counts[slug] == 1 else f"{slug}-{counts[slug] - 1}") |
| 54 | cache[path] = found |
| 55 | return found |
| 56 | |
| 57 | |
| 58 | findings = [] |
| 59 | cache = {} |
| 60 | documents = [p for p in ["README.md", "AGENTS.md"] if os.path.exists(p)] + sorted( |
| 61 | glob.glob("docs/*.md") |
| 62 | ) |
| 63 | |
| 64 | for path in documents: |
| 65 | with open(path, encoding="utf-8") as handle: |
| 66 | lines = handle.read().splitlines() |
| 67 | for number, line in outside_fences(lines): |
| 68 | prose = INLINE_CODE.sub(" ", line) |
| 69 | for target in LINK.findall(prose): |
| 70 | if EXTERNAL.match(target): |
| 71 | continue |
| 72 | relative, has_hash, anchor = target.partition("#") |
| 73 | if not relative: |
| 74 | resolved = path |
| 75 | else: |
| 76 | resolved = os.path.normpath( |
| 77 | os.path.join(os.path.dirname(path), relative) |
| 78 | ) |
| 79 | if resolved == ".." or resolved.startswith("../"): |
| 80 | findings.append( |
| 81 | f"{path}:{number}: {target} points outside the repository" |
| 82 | ) |
| 83 | continue |
| 84 | if not os.path.exists(resolved): |
| 85 | findings.append(f"{path}:{number}: {target} does not exist") |
| 86 | continue |
| 87 | if has_hash: |
| 88 | if not anchor: |
| 89 | findings.append( |
| 90 | f"{path}:{number}: {target} has an empty heading anchor" |
| 91 | ) |
| 92 | elif not os.path.isfile(resolved) or not resolved.endswith(".md"): |
| 93 | findings.append( |
| 94 | f"{path}:{number}: {target} names a heading in non-markdown target {resolved}" |
| 95 | ) |
| 96 | elif anchor not in anchors(resolved, cache): |
| 97 | findings.append( |
| 98 | f"{path}:{number}: {target} names no heading in {resolved}" |
| 99 | ) |
| 100 | |
| 101 | if findings: |
| 102 | print( |
| 103 | f"prose-format: {len(findings)} broken link(s) or anchor(s):", |
| 104 | file=sys.stderr, |
| 105 | ) |
| 106 | for item in findings: |
| 107 | print(f" {item}", file=sys.stderr) |
| 108 | sys.exit(1) |
| 109 | PY |
| 110 | |
| 111 | touch "$out" |