|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Build GitHub Pages site from catalog markdown frontmatter. |
| 4 | + * Add/edit one catalog/<category>/<id>.md → rebuild regenerates everything. |
| 5 | + */ |
| 6 | +import fs from "node:fs"; |
| 7 | +import path from "node:path"; |
| 8 | +import { fileURLToPath } from "node:url"; |
| 9 | + |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | +const ROOT = path.resolve(__dirname, ".."); |
| 12 | +const CATALOG = path.join(ROOT, "catalog"); |
| 13 | +const PUBLIC = path.join(__dirname, "public"); |
| 14 | +const DIST = path.join(__dirname, "dist"); |
| 15 | +const CONFIG_PATH = path.join(__dirname, "config.json"); |
| 16 | + |
| 17 | +function parseScalar(raw) { |
| 18 | + const v = raw.trim(); |
| 19 | + if (v === "true") return true; |
| 20 | + if (v === "false") return false; |
| 21 | + if (v === "null" || v === "~" || v === "") return null; |
| 22 | + if ( |
| 23 | + (v.startsWith('"') && v.endsWith('"')) || |
| 24 | + (v.startsWith("'") && v.endsWith("'")) |
| 25 | + ) { |
| 26 | + return v.slice(1, -1); |
| 27 | + } |
| 28 | + if (v.startsWith("[") && v.endsWith("]")) { |
| 29 | + const inner = v.slice(1, -1).trim(); |
| 30 | + if (!inner) return []; |
| 31 | + return inner.split(",").map((part) => { |
| 32 | + const s = part.trim(); |
| 33 | + if ( |
| 34 | + (s.startsWith('"') && s.endsWith('"')) || |
| 35 | + (s.startsWith("'") && s.endsWith("'")) |
| 36 | + ) { |
| 37 | + return s.slice(1, -1); |
| 38 | + } |
| 39 | + return s; |
| 40 | + }); |
| 41 | + } |
| 42 | + return v; |
| 43 | +} |
| 44 | + |
| 45 | +function parseFrontmatter(text) { |
| 46 | + const match = text.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/); |
| 47 | + if (!match) return null; |
| 48 | + const meta = {}; |
| 49 | + for (const line of match[1].split(/\r?\n/)) { |
| 50 | + if (!line.trim() || line.trim().startsWith("#")) continue; |
| 51 | + const idx = line.indexOf(":"); |
| 52 | + if (idx === -1) continue; |
| 53 | + const key = line.slice(0, idx).trim(); |
| 54 | + meta[key] = parseScalar(line.slice(idx + 1)); |
| 55 | + } |
| 56 | + return { meta, body: match[2].trim() }; |
| 57 | +} |
| 58 | + |
| 59 | +function stripMd(text) { |
| 60 | + return text |
| 61 | + .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1") |
| 62 | + .replace(/[*_`#]/g, "") |
| 63 | + .replace(/\s+/g, " ") |
| 64 | + .trim(); |
| 65 | +} |
| 66 | + |
| 67 | +function summaryFromBody(body) { |
| 68 | + const chunks = body |
| 69 | + .split(/\n\s*\n/) |
| 70 | + .map((p) => p.trim()) |
| 71 | + .filter((p) => p && !p.startsWith("#")); |
| 72 | + const first = chunks[0] || ""; |
| 73 | + const clean = stripMd(first); |
| 74 | + return clean.length > 220 ? `${clean.slice(0, 217)}…` : clean; |
| 75 | +} |
| 76 | + |
| 77 | +function walkMarkdown(dir, out = []) { |
| 78 | + if (!fs.existsSync(dir)) return out; |
| 79 | + for (const name of fs.readdirSync(dir)) { |
| 80 | + const full = path.join(dir, name); |
| 81 | + const stat = fs.statSync(full); |
| 82 | + if (stat.isDirectory()) { |
| 83 | + walkMarkdown(full, out); |
| 84 | + continue; |
| 85 | + } |
| 86 | + if (!name.endsWith(".md")) continue; |
| 87 | + if (name === "README.md" || name === "TEMPLATE.md") continue; |
| 88 | + out.push(full); |
| 89 | + } |
| 90 | + return out; |
| 91 | +} |
| 92 | + |
| 93 | +function loadEntries() { |
| 94 | + const files = walkMarkdown(CATALOG); |
| 95 | + const entries = []; |
| 96 | + const errors = []; |
| 97 | + |
| 98 | + for (const file of files) { |
| 99 | + const text = fs.readFileSync(file, "utf8"); |
| 100 | + const parsed = parseFrontmatter(text); |
| 101 | + if (!parsed) { |
| 102 | + errors.push(`No frontmatter: ${path.relative(ROOT, file)}`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + const { meta, body } = parsed; |
| 106 | + const rel = path.relative(ROOT, file).split(path.sep).join("/"); |
| 107 | + const required = ["id", "name", "url", "category", "license", "status"]; |
| 108 | + const missing = required.filter((k) => meta[k] === undefined || meta[k] === null || meta[k] === ""); |
| 109 | + if (missing.length) { |
| 110 | + errors.push(`${rel} missing: ${missing.join(", ")}`); |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + entries.push({ |
| 115 | + id: String(meta.id), |
| 116 | + name: String(meta.name), |
| 117 | + url: String(meta.url), |
| 118 | + category: String(meta.category), |
| 119 | + subcategories: Array.isArray(meta.subcategories) ? meta.subcategories : [], |
| 120 | + license: String(meta.license), |
| 121 | + commercial: meta.commercial === true ? true : meta.commercial === false ? false : "unknown", |
| 122 | + attribution_required: |
| 123 | + meta.attribution_required === true |
| 124 | + ? true |
| 125 | + : meta.attribution_required === false |
| 126 | + ? false |
| 127 | + : "unknown", |
| 128 | + formats: Array.isArray(meta.formats) ? meta.formats : [], |
| 129 | + tags: Array.isArray(meta.tags) ? meta.tags : [], |
| 130 | + verified: meta.verified ? String(meta.verified) : null, |
| 131 | + status: String(meta.status), |
| 132 | + path: rel, |
| 133 | + summary: summaryFromBody(body), |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + entries.sort((a, b) => a.name.localeCompare(b.name)); |
| 138 | + return { entries, errors }; |
| 139 | +} |
| 140 | + |
| 141 | +function copyDir(src, dest) { |
| 142 | + fs.mkdirSync(dest, { recursive: true }); |
| 143 | + for (const name of fs.readdirSync(src)) { |
| 144 | + const from = path.join(src, name); |
| 145 | + const to = path.join(dest, name); |
| 146 | + if (fs.statSync(from).isDirectory()) copyDir(from, to); |
| 147 | + else fs.copyFileSync(from, to); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +function main() { |
| 152 | + const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8")); |
| 153 | + const { entries, errors } = loadEntries(); |
| 154 | + |
| 155 | + if (errors.length) { |
| 156 | + console.error("Build warnings:"); |
| 157 | + for (const e of errors) console.error(` - ${e}`); |
| 158 | + } |
| 159 | + |
| 160 | + const featuredRaw = config.featured || []; |
| 161 | + const featured = featuredRaw |
| 162 | + .map((item) => { |
| 163 | + const id = typeof item === "string" ? item : item?.id; |
| 164 | + const need = typeof item === "string" ? null : item?.need || null; |
| 165 | + const entry = entries.find((e) => e.id === id); |
| 166 | + if (!entry) return null; |
| 167 | + return { ...entry, need }; |
| 168 | + }) |
| 169 | + .filter(Boolean); |
| 170 | + |
| 171 | + const stats = { |
| 172 | + total: entries.length, |
| 173 | + active: entries.filter((e) => e.status === "active").length, |
| 174 | + needsReview: entries.filter((e) => e.status === "needs-review").length, |
| 175 | + commercialOk: entries.filter((e) => e.commercial === true).length, |
| 176 | + categories: Object.keys(config.categories).length, |
| 177 | + }; |
| 178 | + |
| 179 | + const payload = { |
| 180 | + generatedAt: new Date().toISOString(), |
| 181 | + site: config.site, |
| 182 | + categories: config.categories, |
| 183 | + guides: config.guides, |
| 184 | + featured, |
| 185 | + stats, |
| 186 | + entries, |
| 187 | + }; |
| 188 | + |
| 189 | + if (fs.existsSync(DIST)) fs.rmSync(DIST, { recursive: true, force: true }); |
| 190 | + copyDir(PUBLIC, DIST); |
| 191 | + fs.writeFileSync(path.join(DIST, "data.json"), JSON.stringify(payload, null, 2)); |
| 192 | + fs.writeFileSync( |
| 193 | + path.join(DIST, "data.js"), |
| 194 | + `window.__CATALOG__ = ${JSON.stringify(payload)};\n` |
| 195 | + ); |
| 196 | + |
| 197 | + console.log( |
| 198 | + `Built ${entries.length} entries → site/dist (${stats.active} active, ${stats.commercialOk} commercial-ok)` |
| 199 | + ); |
| 200 | + if (errors.length) process.exitCode = 0; // soft-fail missing fields as warnings |
| 201 | +} |
| 202 | + |
| 203 | +main(); |
0 commit comments