/**
* blogSeo.js — ADR-190: request-time SSR-lite for /blog/* + sitemap/robots.
*
* The marketing blog lives at SPA routes /blog and /blog/:slug. nginx hands
* crawlers the static dist/index.html (a ~2.6 KB generic shell) because every
* per-post
, meta, canonical, OG and article body is injected
* client-side after React hydration (src/features/public/usePublicSeo.ts).
* Bots that do not execute JS see nothing → the blog is invisible to search.
*
* This module intercepts /blog, /blog/:slug, /sitemap.xml and /robots.txt
* BEFORE the SPA catch-all (backend/server.js `app.get('*')`), reads the
* already-public published mirror (table 100245, space `s/help`) via the shared
* public read helper (fetchPublicRows — no new SQL, no new API), and injects a
* crawler-readable (title/description/canonical/OG/twitter) + JSON-LD
* BlogPosting + the rendered article body into the index.html template. React
* still mounts on top for humans (progressive enhancement, Zone C).
*
* SECURITY (ADR-190 §Consequences — HARD, non-negotiable): every row-derived
* value is authored content and is treated as untrusted. Text/attributes are
* HTML-escaped (`esc`); URLs are scheme-filtered (`safeUrl` → http(s) or
* root-relative only); the markdown body is rendered with markdown-it
* `html:false` (raw HTML escaped, javascript:/vbscript:/data: links blocked by
* markdown-it's default validateLink); JSON-LD is escaped against a
* `` breakout. No new markdown dependency — markdown-it already ships
* in package.json.
*/
import fs from 'fs';
import path from 'path';
import MarkdownIt from 'markdown-it';
import { fetchPublicRows } from '../routes/v3/public.js';
import { apiLogger } from '../utils/logger.js';
/** Published-post mirror table (s/help space). See useBuildingInPublic.ts. */
const BLOG_TABLE_ID = 100245;
/**
* Canonical origin for absolute URLs (AC-A3). Overridable for staging.
* Exported as `DEFAULT_SITE_ORIGIN` because ADR-190-A D5 makes the origin
* per-space data: `public_seo.site_origin` → `PUBLIC_SITE_ORIGIN` → this.
* The blog itself is not space-scoped, so it stays on steps 2–3 (AC-8).
*/
const SITE_ORIGIN = (process.env.PUBLIC_SITE_ORIGIN || 'https://app.godcrm.ai').replace(/\/+$/, '');
export const DEFAULT_SITE_ORIGIN = SITE_ORIGIN;
const SITE_NAME = 'GOD CRM';
const log = apiLogger.child({ module: 'blog-seo' });
// html:false → raw HTML inside a post body is escaped, not executed;
// markdown-it's default validateLink blocks javascript:/vbscript:/data: URIs.
const md = new MarkdownIt({ html: false, linkify: true, breaks: false });
// linkify-it prepends `http://` to every schemeless match (`godcrm.ai/s/help/mcp`,
// `www.foo.bar`). On an https page that ships crawlers an insecure href that only
// resolves through a 301 — for our own CTAs the redirect is a downgrade signal, and
// the SPA (react-markdown + remark-gfm) does not autolink schemeless hosts at all,
// so the two surfaces disagreed. Default schemeless autolinks to https instead;
// an explicitly authored `http://…` keeps its scheme (match.schema is non-empty).
const linkifyNormalize = md.linkify.normalize.bind(md.linkify);
md.linkify.normalize = (match) => {
linkifyNormalize(match);
if (!match.schema && match.url.startsWith('http://')) {
match.url = `https://${match.url.slice('http://'.length)}`;
}
};
/**
* Render authored markdown to HTML under the ADR-190 escaping contract.
* Exported so publicDocsSeo renders document atoms through the *same*
* configured instance — a second `new MarkdownIt()` elsewhere is exactly how
* `html:false` gets silently dropped on one surface (ADR-190-A AC-9).
*/
export function renderMarkdown(text) {
return md.render(String(text ?? ''));
}
// ── escaping helpers ────────────────────────────────────────────────────────
const HTML_ESCAPE = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
/** HTML-escape a value for use in text or a double-quoted attribute. */
export function esc(value) {
return String(value ?? '').replace(/[&<>"']/g, (c) => HTML_ESCAPE[c]);
}
/** Only allow http(s) or root-relative URLs in href/src/canonical; else ''. */
export function safeUrl(value) {
const s = String(value ?? '').trim();
if (!s) return '';
if (/^https?:\/\//i.test(s) || s.startsWith('/')) return s;
return '';
}
/** JSON-LD payload: neutralise the chars that could break out of `,
].filter(Boolean).join('\n ');
}
function postBody(post) {
const bodyHtml = post.body
? md.render(post.body)
: (post.excerpt ? `