Glimmer / website · glimmertoday.ai / build.mjs
| 1 | // Builds landing/*.html from site/pages and site/partials. No dependencies: `node site/build.mjs`. |
| 2 | // Pages start with a comment of `key: value` lines (title, description, path, family, robots, |
| 3 | // shareTitle, shareDescription, shareImage, shareImageAlt). The default share card is site/og-card.html, |
| 4 | // saved as landing/img/social-card.png; the Toolkit page uses site/og-toolkit.html, saved as landing/img/toolkit-card.png. |
| 5 | // {{release…}} values come from landing/updates/latest.json, which the Mac release script writes. |
| 6 | import { readFileSync, readdirSync, writeFileSync } from "node:fs"; |
| 7 | |
| 8 | const root = new URL("./", import.meta.url); |
| 9 | const read = path => readFileSync(new URL(path, root), "utf8"); |
| 10 | const partial = name => read(`partials/${name}.html`).trimEnd(); |
| 11 | const include = html => html.replace(/\{\{> (\w+)\}\}/g, (_, name) => include(partial(name))); |
| 12 | const fill = (html, values) => html.replace(/\{\{(\w+)\}\}/g, (_, key) => values[key] ?? ""); |
| 13 | |
| 14 | const latest = JSON.parse(read("../landing/updates/latest.json")); |
| 15 | const escape = text => text.replace(/&/g, "&").replace(/</g, "<").replace(/"/g, """); |
| 16 | const release = { |
| 17 | releaseVersion: latest.version, |
| 18 | releaseFile: latest.file, |
| 19 | releaseFileName: latest.file.split("/").pop(), |
| 20 | releaseSize: String(Math.max(1, Math.round(latest.bytes / 1048576))), |
| 21 | releaseSystem: latest.minimumSystem, |
| 22 | releaseSha: latest.sha256, |
| 23 | releaseNotes: escape(latest.notes), |
| 24 | }; |
| 25 | |
| 26 | for (const file of readdirSync(new URL("pages/", root)).filter(name => name.endsWith(".html"))) { |
| 27 | const [, front, body] = read(`pages/${file}`).match(/^<!--\n([\s\S]*?)\n-->\n([\s\S]*)$/); |
| 28 | const page = Object.fromEntries(front.split("\n").map(line => line.split(/:\s*(.*)/s).slice(0, 2))); |
| 29 | const values = { |
| 30 | ...release, |
| 31 | ...page, |
| 32 | counterAssets: page.path === "/" |
| 33 | ? '<link rel="stylesheet" href="/assets/visitor-odometer.v1.css">\n<script defer src="/assets/visitor-odometer.v1.js"></script>' |
| 34 | : "", |
| 35 | content: fill(include(body.trimEnd()), release), |
| 36 | family: page.family === "true" ? partial("family") + "\n\n" : "", |
| 37 | homeBand: page.path === "/" ? partial("branchband") + "\n\n" : "", |
| 38 | robots: page.robots ? `<meta name="robots" content="${page.robots}">\n` : "", |
| 39 | shareTitle: page.shareTitle ?? page.title, |
| 40 | shareDescription: page.shareDescription ?? page.description, |
| 41 | shareImage: page.shareImage ?? "/img/social-card.png?v=people-decide", |
| 42 | shareImageAlt: page.shareImageAlt ?? "Glimmer. Our social network, where people decide.", |
| 43 | jsonld: page.family === "true" ? partial("jsonld") + "\n" : "", |
| 44 | }; |
| 45 | const html = fill(include(partial("layout")), values); |
| 46 | writeFileSync(new URL(`../landing/${file}`, root), html + "\n"); |
| 47 | console.log(`landing/${file}`); |
| 48 | } |