|
| 1 | +/** |
| 2 | + * Checks that every page of the build has its social card: each og:image and |
| 3 | + * twitter:image of each HTML page of dist/ is a file of dist/. Prints the count, |
| 4 | + * then each missing card, and exits with 1 when a card is missing. The deploy |
| 5 | + * workflow runs it after "Render the social cards". |
| 6 | + * |
| 7 | + * node scripts/check-og.mjs [dist] |
| 8 | + */ |
| 9 | +import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 10 | +import { join, relative } from "node:path"; |
| 11 | +import { pathToFileURL } from "node:url"; |
| 12 | + |
| 13 | +/** The address of the site: an image there is a file of dist/. */ |
| 14 | +export const SITE = "https://docs.phalcon.io"; |
| 15 | + |
| 16 | +/** |
| 17 | + * The image addresses of a page (og:image, twitter:image) as paths of the site, |
| 18 | + * each once, with no query and no fragment (a card can have a version, ?v=2). |
| 19 | + * The attributes of a meta tag can come in any order. An image on another host |
| 20 | + * is left out. |
| 21 | + * |
| 22 | + * @param {string} html |
| 23 | + * @returns {string[]} |
| 24 | + */ |
| 25 | +export function cardPaths(html) { |
| 26 | + const paths = new Set(); |
| 27 | + |
| 28 | + for (const [tag] of html.matchAll(/<meta\b[^>]*>/g)) { |
| 29 | + const image = /\b(?:property|name)="(?:og:image|twitter:image)"/.test(tag); |
| 30 | + const address = /\bcontent="([^"]*)"/.exec(tag)?.[1] ?? ""; |
| 31 | + const path = address.startsWith(`${SITE}/`) ? address.slice(SITE.length) : address; |
| 32 | + |
| 33 | + if (image && path.startsWith("/")) { |
| 34 | + paths.add(path.replace(/[?#].*$/, "")); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return [...paths]; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * The cards of the pages that are not files. |
| 43 | + * |
| 44 | + * @param {{ html: string, page: string }[]} pages |
| 45 | + * @param {(path: string) => boolean} exists |
| 46 | + * @returns {{ page: string, path: string }[]} |
| 47 | + */ |
| 48 | +export function missingCards(pages, exists) { |
| 49 | + return pages.flatMap(({ html, page }) => cardPaths(html).filter((path) => !exists(path)).map((path) => ({ page, path }))); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * The result of the check: the count of the different cards, the missing ones, |
| 54 | + * and whether the build passes. A build with no card at all fails: then the |
| 55 | + * check read nothing (for example, a new form of the meta tags), and must not |
| 56 | + * pass in silence. |
| 57 | + * |
| 58 | + * @param {{ html: string, page: string }[]} pages |
| 59 | + * @param {(path: string) => boolean} exists |
| 60 | + * @returns {{ cards: number, missing: { page: string, path: string }[], ok: boolean }} |
| 61 | + */ |
| 62 | +export function checkSummary(pages, exists) { |
| 63 | + const cards = new Set(pages.flatMap(({ html }) => cardPaths(html))).size; |
| 64 | + const missing = missingCards(pages, exists); |
| 65 | + |
| 66 | + return { cards, missing, ok: cards > 0 && missing.length === 0 }; |
| 67 | +} |
| 68 | + |
| 69 | +if (import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 70 | + const dist = process.argv[2] ?? "dist"; |
| 71 | + const files = readdirSync(dist, { recursive: true }).filter((file) => file.endsWith(".html")); |
| 72 | + const pages = files.map((file) => ({ html: readFileSync(join(dist, file), "utf8"), page: relative(dist, join(dist, file)) })); |
| 73 | + const { cards, missing, ok } = checkSummary(pages, (path) => existsSync(join(dist, decodeURIComponent(path)))); |
| 74 | + |
| 75 | + console.log(`pages ${pages.length}, cards ${cards}, missing ${missing.length}`); |
| 76 | + |
| 77 | + if (cards === 0) { |
| 78 | + console.log("no card: no page has an og:image or a twitter:image that the check can read"); |
| 79 | + } |
| 80 | + |
| 81 | + for (const { page, path } of missing.slice(0, 20)) { |
| 82 | + console.log(`missing ${path} (${page})`); |
| 83 | + } |
| 84 | + |
| 85 | + process.exitCode = ok ? 0 : 1; |
| 86 | +} |
0 commit comments