|
| 1 | +""" |
| 2 | +check_seo_frontmatter.py — CI gate: every published page must set `description:`. |
| 3 | +
|
| 4 | +Per-page `description:` (and, where possible, `image:`) drives search snippets and |
| 5 | +social link-preview cards via jekyll-seo-tag. This script enforces the convention so |
| 6 | +new content (and in-flight branches) can't quietly regress to the generic site card. |
| 7 | +See the "SEO and social cards" section of website-dev.md. |
| 8 | +
|
| 9 | +Rules: |
| 10 | + - A "page" = a .md file with YAML front matter containing `layout:`. |
| 11 | + - Every page MUST have a non-empty `description:` — EXCEPT: |
| 12 | + * pages marked draft via `nav_exclude: true` or `search_exclude: true`, and |
| 13 | + * paths in IGNORE (contributor docs, deprecated pages). |
| 14 | + A draft becomes subject to the rule as soon as it's published (nav_exclude removed). |
| 15 | + - `image:` is ADVISORY: a page whose hero is an MP4 <video> but has no `image:` |
| 16 | + yet gets a non-fatal reminder to run scripts/generate_og_posters.py. Pages may |
| 17 | + legitimately have no image (they fall back to the site card). |
| 18 | +
|
| 19 | +Exit code: 1 if any required page is missing `description:` (fails the CI check); |
| 20 | +0 otherwise. Advisory image reminders never affect the exit code. |
| 21 | +
|
| 22 | +Usage: |
| 23 | + python scripts/check_seo_frontmatter.py |
| 24 | +""" |
| 25 | + |
| 26 | +import re |
| 27 | +import sys |
| 28 | +from pathlib import Path |
| 29 | + |
| 30 | +DOCS_DIR = "." |
| 31 | +SKIP_DIRS = {"_site", ".git", "node_modules", "vendor", ".jekyll-cache", |
| 32 | + "scripts", "_includes", "_layouts", "_data", "_sass", "assets"} |
| 33 | + |
| 34 | +# Pages exempt from the description: requirement (contributor docs, deprecated). |
| 35 | +IGNORE = { |
| 36 | + "website-dev.md", "website-install.md", "teaching-notes.md", |
| 37 | + "website-content-ideas.md", "README.md", "LICENSE.md", "CLAUDE.md", |
| 38 | + "404.md", "arduino/potentiometers-old.md", |
| 39 | +} |
| 40 | + |
| 41 | +FRONT_MATTER_RE = re.compile(r"^---\s*\n(.*?)\n---\s*\n", re.DOTALL) |
| 42 | +HTML_COMMENT_RE = re.compile(r"<!--.*?-->", re.DOTALL) |
| 43 | +MEDIA_TOKEN_RE = re.compile(r"<video\b|!\[|<img\b|<iframe\b", re.IGNORECASE) |
| 44 | +MP4_SOURCE_RE = re.compile(r'<source\b[^>]*\.mp4"', re.IGNORECASE) |
| 45 | + |
| 46 | + |
| 47 | +def front_matter(content): |
| 48 | + m = FRONT_MATTER_RE.match(content) |
| 49 | + return (m.group(1), content[m.end():]) if m else (None, content) |
| 50 | + |
| 51 | + |
| 52 | +def fm_has(fm, key): |
| 53 | + """True if front matter has a non-empty value for `key`.""" |
| 54 | + m = re.search(rf"^{key}:\s*(.+?)\s*$", fm, re.MULTILINE) |
| 55 | + return bool(m and m.group(1).strip() not in ("", '""', "''")) |
| 56 | + |
| 57 | + |
| 58 | +def fm_true(fm, key): |
| 59 | + return bool(re.search(rf"^{key}:\s*true\s*$", fm, re.MULTILINE | re.IGNORECASE)) |
| 60 | + |
| 61 | + |
| 62 | +def hero_is_mp4(body): |
| 63 | + visible = HTML_COMMENT_RE.sub("", body) |
| 64 | + first = MEDIA_TOKEN_RE.search(visible) |
| 65 | + if not first or not visible[first.start():first.end()].lower().startswith("<video"): |
| 66 | + return False |
| 67 | + return bool(MP4_SOURCE_RE.search(visible, first.start())) |
| 68 | + |
| 69 | + |
| 70 | +def rel(p): |
| 71 | + return str(p).replace("\\", "/").lstrip("./") |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + missing_desc = [] |
| 76 | + image_reminders = [] |
| 77 | + checked = 0 |
| 78 | + |
| 79 | + for path in sorted(Path(DOCS_DIR).rglob("*.md")): |
| 80 | + if any(part in SKIP_DIRS for part in path.parts): |
| 81 | + continue |
| 82 | + relpath = rel(path) |
| 83 | + if relpath in IGNORE: |
| 84 | + continue |
| 85 | + |
| 86 | + fm, body = front_matter(path.read_text(encoding="utf-8")) |
| 87 | + if fm is None or not re.search(r"^layout:", fm, re.MULTILINE): |
| 88 | + continue # not a page |
| 89 | + if fm_true(fm, "nav_exclude") or fm_true(fm, "search_exclude"): |
| 90 | + continue # draft / hidden |
| 91 | + |
| 92 | + checked += 1 |
| 93 | + if not fm_has(fm, "description"): |
| 94 | + missing_desc.append(relpath) |
| 95 | + if not fm_has(fm, "image") and hero_is_mp4(body): |
| 96 | + image_reminders.append(relpath) |
| 97 | + |
| 98 | + print(f"Checked {checked} published page(s).") |
| 99 | + |
| 100 | + if image_reminders: |
| 101 | + print(f"\nReminder ({len(image_reminders)}): MP4-hero page(s) with no `image:` " |
| 102 | + f"— run `python scripts/generate_og_posters.py --run`:") |
| 103 | + for p in image_reminders: |
| 104 | + print(f" - {p}") |
| 105 | + |
| 106 | + if missing_desc: |
| 107 | + print(f"\nERROR: {len(missing_desc)} published page(s) missing `description:` " |
| 108 | + f"front matter:") |
| 109 | + for p in missing_desc: |
| 110 | + print(f" - {p}") |
| 111 | + print("\nAdd a `description:` (see website-dev.md -> 'SEO and social cards'). " |
| 112 | + "Drafts can set `nav_exclude: true` to defer.") |
| 113 | + return 1 |
| 114 | + |
| 115 | + print("\nAll published pages have `description:`. OK") |
| 116 | + return 0 |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + sys.exit(main()) |
0 commit comments