The best way to control Claude Code, Codex and more from any computer or phone
| 1 | #!/usr/bin/env python3 |
| 2 | """Check a release blog post before handing it over. |
| 3 | |
| 4 | python3 -I .agents/skills/release-blog/scripts/check-post.py website/src/content/docs/blog/<post>.md [pr-dir] |
| 5 | |
| 6 | - every `](/path)` and `](/path#anchor)` resolves to a page and heading under website/src/content/docs |
| 7 | - every `](#anchor)` matches a heading in the post |
| 8 | - every ```diff block renders with even indentation (the website build's odd-indent check) |
| 9 | - with pr-dir (collect-prs.sh's output), every feat, fix, perf or breaking PR is cited |
| 10 | """ |
| 11 | import json |
| 12 | import os |
| 13 | import re |
| 14 | import sys |
| 15 | |
| 16 | ROOT = "website/src/content/docs" |
| 17 | |
| 18 | |
| 19 | def slug(heading: str) -> str: |
| 20 | h = re.sub(r"`", "", heading.strip().lower()) |
| 21 | h = re.sub(r"[^\w\- ]", "", h) |
| 22 | return h.replace(" ", "-") |
| 23 | |
| 24 | |
| 25 | def headings(text: str) -> set[str]: |
| 26 | return {slug(h) for h in re.findall(r"^#+ (.*)$", text, re.M)} |
| 27 | |
| 28 | |
| 29 | def check_links(post: str) -> list[str]: |
| 30 | problems = [] |
| 31 | own = headings(post) |
| 32 | for link in sorted(set(re.findall(r"\]\((/[^)\s]*)\)", post))): |
| 33 | path, _, anchor = link.partition("#") |
| 34 | path = path.strip("/") |
| 35 | if re.search(r"\.\w+$", path): # a static asset such as an image |
| 36 | if not os.path.exists(f"website/public/{path}"): |
| 37 | problems.append(f"missing asset: {link}") |
| 38 | continue |
| 39 | candidates = [f"{ROOT}/{path}{ext}" for ext in (".md", ".mdx", "/index.md", "/index.mdx")] |
| 40 | page = next((c for c in candidates if os.path.exists(c)), None) |
| 41 | if page is None: |
| 42 | problems.append(f"missing page: {link}") |
| 43 | elif anchor and anchor not in headings(open(page).read()): |
| 44 | problems.append(f"missing anchor: {link}") |
| 45 | for anchor in re.findall(r"\]\(#([^)]+)\)", post): |
| 46 | if anchor not in own: |
| 47 | problems.append(f"missing anchor in post: #{anchor}") |
| 48 | return problems |
| 49 | |
| 50 | |
| 51 | def check_diffs(post: str) -> list[str]: |
| 52 | problems = [] |
| 53 | lines = post.split("\n") |
| 54 | i = 0 |
| 55 | while i < len(lines): |
| 56 | fence = re.match(r"^(\s*)```diff", lines[i]) |
| 57 | if fence: |
| 58 | indent = len(fence.group(1)) |
| 59 | j = i + 1 |
| 60 | body = [] |
| 61 | while not lines[j].strip().startswith("```"): |
| 62 | body.append(lines[j][indent:]) |
| 63 | j += 1 |
| 64 | nonblank = [l for l in body if l.strip()] |
| 65 | least = min(len(l) - len(l.lstrip(" ")) for l in nonblank) |
| 66 | if not any(l[:1] not in "+-" and len(l) - len(l.lstrip(" ")) == 0 for l in nonblank): |
| 67 | problems.append(f"line {i + 1}: diff block has no column-0 context line") |
| 68 | for l in nonblank: |
| 69 | rendered = l[least:] |
| 70 | if rendered[:1] in "+-": |
| 71 | rendered = rendered[1:] |
| 72 | if (len(rendered) - len(rendered.lstrip(" "))) % 2: |
| 73 | problems.append(f"line {i + 1}: odd indentation: {l!r}") |
| 74 | i = j |
| 75 | i += 1 |
| 76 | return problems |
| 77 | |
| 78 | |
| 79 | def check_coverage(post: str, pr_dir: str) -> list[str]: |
| 80 | cited = set(re.findall(r"/pull/(\d+)", post)) |
| 81 | problems = [] |
| 82 | for n in open(f"{pr_dir}/nums.txt").read().split(): |
| 83 | if n in cited: |
| 84 | continue |
| 85 | pr = json.load(open(f"{pr_dir}/{n}.json")) |
| 86 | title = pr["title"] |
| 87 | if re.match(r"(feat|fix|perf)\b", title) or re.match(r"\w+(\([^)]*\))?!:", title): |
| 88 | problems.append(f"uncited: #{n} {title}") |
| 89 | return problems |
| 90 | |
| 91 | |
| 92 | def main() -> None: |
| 93 | post = open(sys.argv[1]).read() |
| 94 | problems = check_links(post) + check_diffs(post) |
| 95 | # uncited PRs are for review, not failures: each must be a deliberate skip |
| 96 | for line in check_coverage(post, sys.argv[2]) if len(sys.argv) > 2 else []: |
| 97 | print(line) |
| 98 | for p in problems: |
| 99 | print(p) |
| 100 | print("ok" if not problems else f"{len(problems)} problem(s)") |
| 101 | sys.exit(1 if problems else 0) |
| 102 | |
| 103 | |
| 104 | if __name__ == "__main__": |
| 105 | main() |