|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Validate llms.txt format and local link integrity. |
| 4 | +
|
| 5 | +Rules: |
| 6 | +- First non-empty line must be a single H1. |
| 7 | +- Section headings must be H2 (## ...). |
| 8 | +- List items must use Markdown links. |
| 9 | +- Internal docs links must target docs.starknet.io and resolve to local .mdx pages. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import re |
| 15 | +import sys |
| 16 | +from collections import Counter |
| 17 | +from pathlib import Path |
| 18 | +from urllib.parse import unquote, urlparse |
| 19 | + |
| 20 | + |
| 21 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 22 | +REPO_ROOT_RESOLVED = REPO_ROOT.resolve() |
| 23 | +LLMS_PATH = REPO_ROOT / "llms.txt" |
| 24 | +ALLOWED_NON_DOC_PATHS = { |
| 25 | + "/llms.txt", |
| 26 | + "/llms-full.txt", |
| 27 | + "/.well-known/llms.txt", |
| 28 | + "/.well-known/llms-full.txt", |
| 29 | + "/sitemap.xml", |
| 30 | +} |
| 31 | +ALLOWED_EXTERNAL_URLS = { |
| 32 | + "https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json", |
| 33 | +} |
| 34 | +ITEM_RE = re.compile(r"^- \[([^\]]+)\]\((https://[^\s)]+)\)(?:: .+)?$") |
| 35 | + |
| 36 | + |
| 37 | +def fail(message: str) -> None: |
| 38 | + print(f"ERROR: {message}", file=sys.stderr) |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + |
| 42 | +def validate_canonical_path(path: str, url: str) -> None: |
| 43 | + if not path.startswith("/"): |
| 44 | + fail(f"URL path must be absolute: {url}") |
| 45 | + if "//" in path: |
| 46 | + fail(f"URL path must be canonical (no duplicate slashes): {url}") |
| 47 | + |
| 48 | + segments = path.split("/") |
| 49 | + for segment in segments[1:]: |
| 50 | + if segment in {"", ".", ".."}: |
| 51 | + fail(f"URL path must be canonical (no dot-segments): {url}") |
| 52 | + |
| 53 | + decoded_segment = unquote(segment) |
| 54 | + if decoded_segment in {".", ".."}: |
| 55 | + fail(f"URL path must be canonical (no encoded dot-segments): {url}") |
| 56 | + if "/" in decoded_segment: |
| 57 | + fail(f"URL path must be canonical (no encoded separators): {url}") |
| 58 | + |
| 59 | + |
| 60 | +def normalize_to_local_mdx(path: str) -> Path: |
| 61 | + """ |
| 62 | + Convert docs URL path to local file path. |
| 63 | + Example: |
| 64 | + /index.md -> index.mdx |
| 65 | + /build/quickstart/overview.md -> build/quickstart/overview.mdx |
| 66 | + """ |
| 67 | + relative = path.lstrip("/") |
| 68 | + if relative == "index.md": |
| 69 | + candidate = REPO_ROOT / "index.mdx" |
| 70 | + elif relative.endswith(".md"): |
| 71 | + candidate = REPO_ROOT / f"{relative[:-3]}.mdx" |
| 72 | + else: |
| 73 | + candidate = REPO_ROOT / relative |
| 74 | + |
| 75 | + resolved = candidate.resolve(strict=False) |
| 76 | + if not resolved.is_relative_to(REPO_ROOT_RESOLVED): |
| 77 | + fail(f"URL path escapes repository root: {path}") |
| 78 | + return resolved |
| 79 | + |
| 80 | + |
| 81 | +def validate_headings(non_empty: list[tuple[int, str]]) -> int: |
| 82 | + h1_count = 0 |
| 83 | + saw_h2 = False |
| 84 | + for line_no, line in non_empty: |
| 85 | + stripped = line.strip() |
| 86 | + if stripped.startswith("# "): |
| 87 | + if not stripped[2:].strip(): |
| 88 | + fail(f"line {line_no}: H1 heading text cannot be empty") |
| 89 | + h1_count += 1 |
| 90 | + elif stripped.startswith("## "): |
| 91 | + if not stripped[3:].strip(): |
| 92 | + fail(f"line {line_no}: H2 heading text cannot be empty") |
| 93 | + saw_h2 = True |
| 94 | + elif stripped.startswith("#"): |
| 95 | + fail(f"line {line_no}: only H1/H2 headings are allowed, got: {stripped[:40]!r}") |
| 96 | + elif stripped.startswith("- "): |
| 97 | + pass |
| 98 | + elif not saw_h2: |
| 99 | + # Allow short prose intro only before the first section heading. |
| 100 | + pass |
| 101 | + else: |
| 102 | + fail( |
| 103 | + f"line {line_no}: unexpected line (not H1, H2, or list item): {stripped[:40]!r}" |
| 104 | + ) |
| 105 | + |
| 106 | + if h1_count != 1: |
| 107 | + fail(f"llms.txt must contain exactly one H1 heading, found {h1_count}") |
| 108 | + |
| 109 | + section_count = sum(1 for _, line in non_empty if line.strip().startswith("## ")) |
| 110 | + if section_count < 4: |
| 111 | + fail( |
| 112 | + f"llms.txt should contain multiple curated sections (found {section_count}, expected >= 4)" |
| 113 | + ) |
| 114 | + |
| 115 | + return section_count |
| 116 | + |
| 117 | + |
| 118 | +def parse_list_items(lines: list[str]) -> list[str]: |
| 119 | + urls: list[str] = [] |
| 120 | + |
| 121 | + for line_no, line in enumerate(lines, start=1): |
| 122 | + stripped = line.strip() |
| 123 | + if not stripped.startswith("- "): |
| 124 | + continue |
| 125 | + match = ITEM_RE.match(stripped) |
| 126 | + if not match: |
| 127 | + fail( |
| 128 | + f"line {line_no}: list entries must use '- [title](https://docs.starknet.io/...)[: description]'" |
| 129 | + ) |
| 130 | + title, url = match.group(1), match.group(2) |
| 131 | + if not title.strip(): |
| 132 | + fail(f"line {line_no}: link title cannot be empty") |
| 133 | + urls.append(url) |
| 134 | + |
| 135 | + if len(urls) < 20: |
| 136 | + fail(f"llms.txt should be curated but substantial (found {len(urls)} links, expected >= 20)") |
| 137 | + |
| 138 | + return urls |
| 139 | + |
| 140 | + |
| 141 | +def verify_urls(urls: list[str]) -> None: |
| 142 | + duplicates = sorted([u for u, count in Counter(urls).items() if count > 1]) |
| 143 | + if duplicates: |
| 144 | + fail(f"duplicate URLs found: {duplicates}") |
| 145 | + |
| 146 | + for url in urls: |
| 147 | + parsed = urlparse(url) |
| 148 | + if parsed.scheme != "https": |
| 149 | + fail(f"invalid URL scheme: {url}") |
| 150 | + if parsed.query or parsed.fragment: |
| 151 | + fail(f"URL must not include query parameters or fragments: {url}") |
| 152 | + |
| 153 | + if parsed.netloc == "docs.starknet.io": |
| 154 | + validate_canonical_path(parsed.path, url) |
| 155 | + if parsed.path in ALLOWED_NON_DOC_PATHS: |
| 156 | + continue |
| 157 | + |
| 158 | + if not parsed.path.endswith(".md"): |
| 159 | + fail( |
| 160 | + f"non-curated link must end in .md (or be allowlisted optional artifact): {url}" |
| 161 | + ) |
| 162 | + |
| 163 | + local_file = normalize_to_local_mdx(parsed.path) |
| 164 | + if not local_file.is_file(): |
| 165 | + fail(f"URL does not map to a local .mdx file: {url} -> {local_file}") |
| 166 | + continue |
| 167 | + |
| 168 | + if url not in ALLOWED_EXTERNAL_URLS: |
| 169 | + fail(f"external URL is not in allowlist: {url}") |
| 170 | + |
| 171 | + |
| 172 | +def main() -> None: |
| 173 | + if not LLMS_PATH.exists(): |
| 174 | + fail("llms.txt does not exist at repository root") |
| 175 | + |
| 176 | + text = LLMS_PATH.read_text(encoding="utf-8") |
| 177 | + lines = text.splitlines() |
| 178 | + |
| 179 | + non_empty = [(i + 1, line) for i, line in enumerate(lines) if line.strip()] |
| 180 | + if not non_empty: |
| 181 | + fail("llms.txt is empty") |
| 182 | + |
| 183 | + first_line_no, first_line = non_empty[0] |
| 184 | + if not first_line.strip().startswith("# "): |
| 185 | + fail(f"first non-empty line ({first_line_no}) must be an H1 heading") |
| 186 | + |
| 187 | + section_count = validate_headings(non_empty) |
| 188 | + urls = parse_list_items(lines) |
| 189 | + verify_urls(urls) |
| 190 | + |
| 191 | + print( |
| 192 | + f"llms.txt validation passed: {len(urls)} links, {section_count} sections, no format/link errors." |
| 193 | + ) |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + main() |
0 commit comments