/* ═══════════════════════════════════════════════════════════════
PAGE METADATA
What a page's HTML should say about it before any JavaScript
runs: title, description, preview image, canonical address,
whether search engines may index it, and structured data. Link
previews (Facebook, iMessage, Slack, Discord, X) never run the
app's JavaScript, so this is the only way a shared event shows
the event rather than the site's defaults.
routes/pages.js puts it into the built index.html for each
request, and serves /sitemap.xml from the same rules.
── Where it comes from ──
/site.json (SITE_CONFIG): the site's name, address, default
description and image, icons, and each static page's title and
description. Laid over it: Panel → Site settings (the
site_settings table, 035) and Pages → Navigation's page titles
(site_nav_pages, 075), where anything set wins and anything
blank falls back to site.json. siteFor(db) does the merge. The
pages read the same merged values (renderPage writes them into
the HTML as window.__SITE__, see src/lib/site.ts), so a
heading and its search title can't drift.
The database: records' names, taglines, first paragraphs and
images. A blank site description falls back to the front page's
subhead.
── Rules ──
published, listed indexed, in the sitemap
published, unlisted noindex: reachable by link, not by search
unpublished, missing 404 and noindex, with the site defaults
a hidden static page 404 and noindex below viewer, out of the
sitemap (siteNav.js)
/admin noindex
Image paths resolve like src/lib/media.ts does: a bare filename
is in the uploads directory, anything starting with / or a scheme
is used as it is.
An SVG isn't used as a preview image (the big sites won't show
one), so it falls back to the site default.
═══════════════════════════════════════════════════════════════ */
import { readFileSync, statSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { loadBlocks, paragraphs } from "./shape.js";
import { themeCss } from "./theme.js";
import { driveReady } from "./googleService.js";
import { hiddenFrom, hiddenPaths, navFor } from "./siteNav.js";
const here = path.dirname(fileURLToPath(import.meta.url));
export const SITE_CONFIG = path.resolve(process.env.SITE_CONFIG ?? path.join(here, "../../site.json"));
const DESCRIPTION_CHARS = 160;
/* Where a bare filename lives: record images are uploads, whatever
kind they are. Mirrors src/lib/media.ts. */
const UPLOADS = "/uploads";
/* ── site.json, reread when it changes ─────────────────────── */
let cached = { mtime: 0, site: null };
export function loadSite() {
const mtime = statSync(SITE_CONFIG).mtimeMs;
if (cached.site && cached.mtime === mtime) return cached.site;
const site = JSON.parse(readFileSync(SITE_CONFIG, "utf8"));
cached = { mtime, site };
return site;
}
const origin = (site) => (site.url ?? "").replace(/\/$/, "");
/* site.json with the admin's Site settings laid over it: a value set
there wins, a blank one falls back. indexing 'on' / 'off' overrides
robots.index for this database only. Page overrides (Pages →
Navigation) apply only to pages site.json knows. */
export function siteFor(db) {
const base = loadSite();
const row = db.prepare(`SELECT * FROM site_settings WHERE id = 'site'`).get() ?? {};
const pages = { ...(base.pages ?? {}) };
const overrides = db
.prepare(`SELECT path, title, description FROM site_nav_pages WHERE nav_id = 'site'`)
.all();
for (const page of overrides) {
const known = pages[page.path];
if (!known) continue;
pages[page.path] = {
title: page.title || known.title,
description: page.description || known.description,
};
}
return {
...base,
title: row.site_name || base.title,
description: row.description || base.description,
openGraph: { ...base.openGraph, image: row.preview_image || base.openGraph?.image },
icons: {
...base.icons,
icon: row.favicon || base.icons?.icon,
apple: row.apple_touch_icon || base.icons?.apple,
},
robots: {
...base.robots,
index: row.indexing ? row.indexing === "on" : base.robots?.index !== false,
},
privacyEmail: row.privacy_email || base.privacyEmail || "",
// The admin's colors (theme.js): only #rrggbb values and fixed
// text, so safe inside `,
analyticsTag(site),
].filter(Boolean);
// A blocked site (Site settings indexing off, or site.json's
// robots.index false) stays blocked on every page, whatever the
// record says.
if (site.robots?.index === false && !meta.noindex) {
tags.splice(2, 0, ``);
}
// A function, so a $& or $` in record text isn't read as a pattern.
return out.replace(/<\/head>/i, () => ` ${tags.join("\n ")}\n `);
}
/* ── Sitemap ───────────────────────────────────────────────── */
export function sitemap(db) {
const site = siteFor(db);
const base = origin(site);
const day = (value) => (value ? String(value).slice(0, 10) : null);
const hidden = hiddenPaths(db);
const entries = [
{ loc: "/" },
...Object.keys(site.pages ?? {})
.filter((loc) => !hidden.has(loc))
.map((loc) => ({ loc })),
...db
.prepare(`SELECT id, updated_at FROM events WHERE is_published = 1 AND is_listed = 1`)
.all()
.map((r) => ({ loc: `/events/${r.id}`, lastmod: day(r.updated_at) })),
...db
.prepare(
`SELECT id, kind, updated_at FROM organizations
WHERE is_published = 1 AND is_listed = 1 AND kind <> 'ministry'`,
)
.all()
.map((r) => ({ loc: `${ORG_ROUTES[r.kind] ?? "/organizations"}/${r.id}`, lastmod: day(r.updated_at) })),
...db
.prepare(
`SELECT t.id, t.updated_at FROM teams t
JOIN organizations o ON o.id = t.org_id AND o.is_published = 1
WHERE t.is_published = 1 AND t.is_listed = 1`,
)
.all()
.map((r) => ({ loc: `/teams/${r.id}`, lastmod: day(r.updated_at) })),
...db
.prepare(`SELECT id FROM awards WHERE is_published = 1 AND is_listed = 1`)
.all()
.map((r) => ({ loc: `/awards/${r.id}` })),
...db
.prepare(`SELECT id, updated_at FROM people WHERE is_published = 1 AND is_listed = 1`)
.all()
.map((r) => ({ loc: `/people/${r.id}`, lastmod: day(r.updated_at) })),
...db
.prepare(`SELECT id, updated_at FROM blog_posts WHERE is_published = 1 AND is_listed = 1`)
.all()
.map((r) => ({ loc: `/blog/${r.id}`, lastmod: day(r.updated_at) })),
...db
.prepare(`SELECT id FROM person_tag_types`)
.all()
.map((r) => ({ loc: `/directory/${r.id}` })),
];
const xml = entries
.map(
(e) =>
` ${escapeHtml(base + e.loc.split("/").map(encodeURIComponent).join("/"))}` +
(e.lastmod ? `${e.lastmod}` : "") +
``,
)
.join("\n");
return `\n\n${xml}\n\n`;
}