/** * Injects CMS editing hooks into src/site/siteMarkup.txt. * * data-cms="cNNNN" → editable rich text (the element's innerHTML) * data-cms-img="imgNNN" → replaceable image (the element's src) * data-cms-num="nNNN" → editable animated stat (the element's data-count/data-suffix) * * Idempotent: elements that already carry an attribute keep their id, and new ids * continue from the highest number already present. Re-run it after editing the * markup by hand: node scripts/inject-cms-ids.mjs * * Also writes src/cms/generated/cmsFields.json — the field manifest the admin uses * to list and jump to every editable region. */ import fs from 'node:fs' import path from 'node:path' const ROOT = path.resolve(import.meta.dirname, '..') const MARKUP = path.join(ROOT, 'src/site/siteMarkup.txt') const MANIFEST = path.join(ROOT, 'src/cms/generated/cmsFields.json') const VOID = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']) // Never touch anything inside these — their internals are structural, not copy. const SKIP_SUBTREE = new Set(['svg', 'script', 'style', 'template', 'noscript']) // An element containing any of these is a container, not a text field: descend instead. const BLOCK = new Set([ 'div', 'section', 'article', 'aside', 'header', 'footer', 'nav', 'main', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'form', 'figure', 'figcaption', 'blockquote', 'video', 'iframe', 'canvas', 'select', 'textarea', 'input', 'button', 'svg', ]) // Text holders worth exposing as a field. const CANDIDATE = new Set([ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'li', 'blockquote', 'cite', 'figcaption', 'td', 'th', 'label', 'button', 'a', 'span', 'strong', 'em', 'small', 'dt', 'dd', 'summary', 'div', ]) // Regions rewritten by the runtime (live threat feed, React mounts) — editing them // there would be silently discarded, so they are left alone. const SKIP_CLASS = /(?:^|\s)(?:feed-body|bd-mobile-demo-mount)(?:\s|$)/ const html = fs.readFileSync(MARKUP, 'utf8') // ── Parse into a light tree with source offsets ──────────────────────────────── const TOKEN = /|<\/([a-zA-Z][\w:-]*)\s*>|<([a-zA-Z][\w:-]*)((?:"[^"]*"|'[^']*'|[^>"'])*?)(\/?)>/g const root = { tag: '#root', children: [], parent: null, depth: 0 } const stack = [root] let last = 0 let m while ((m = TOKEN.exec(html))) { if (html.slice(last, m.index).trim()) stack[stack.length - 1].hasText = true last = TOKEN.lastIndex const [, closeTag, openTag, attrs = '', selfClose] = m if (closeTag) { const tag = closeTag.toLowerCase() for (let i = stack.length - 1; i > 0; i--) { if (stack[i].tag === tag) { stack[i].contentEnd = m.index stack.length = i break } } } else if (openTag) { const tag = openTag.toLowerCase() const parent = stack[stack.length - 1] const node = { tag, attrs, nameEnd: m.index + 1 + openTag.length, // insertion point for new attributes contentStart: TOKEN.lastIndex, contentEnd: TOKEN.lastIndex, children: [], parent, hasText: false, } parent.children.push(node) if (!VOID.has(tag) && !selfClose) stack.push(node) } } // ── Existing ids (idempotency) ───────────────────────────────────────────────── const attrOf = (node, name) => { const hit = new RegExp(`${name}="([^"]*)"`).exec(node.attrs) return hit ? hit[1] : null } let maxText = 0, maxImg = 0, maxNum = 0 const seen = { text: new Set(), img: new Set(), num: new Set() } const walkAll = (node, fn) => { fn(node) node.children.forEach((c) => walkAll(c, fn)) } walkAll(root, (node) => { if (!node.attrs) return const t = attrOf(node, 'data-cms'), i = attrOf(node, 'data-cms-img'), n = attrOf(node, 'data-cms-num') if (t) { seen.text.add(t); maxText = Math.max(maxText, +t.slice(1) || 0) } if (i) { seen.img.add(i); maxImg = Math.max(maxImg, +i.slice(3) || 0) } if (n) { seen.num.add(n); maxNum = Math.max(maxNum, +n.slice(1) || 0) } }) // ── Decide what gets a hook ──────────────────────────────────────────────────── const inserts = [] // { at, text } const fields = [] const textOf = (node) => html.slice(node.contentStart, node.contentEnd).replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim() const pageOf = (node) => { for (let p = node; p; p = p.parent) { const id = p.attrs ? attrOf(p, 'id') : null if (id && id.startsWith('pg-')) return id.slice(3) } return 'global' } const hasBlockDescendant = (node) => node.children.some((c) => BLOCK.has(c.tag) || hasBlockDescendant(c)) const hasTextDeep = (node) => node.hasText || node.children.some(hasTextDeep) function visit(node, { skipping = false, claimed = false } = {}) { for (const child of node.children) { const cls = child.attrs ? attrOf(child, 'class') || '' : '' const skip = skipping || SKIP_SUBTREE.has(child.tag) || SKIP_CLASS.test(cls) if (!skip) { if (child.tag === 'img') { let id = attrOf(child, 'data-cms-img') if (!id) { id = 'img' + String(++maxImg).padStart(3, '0') inserts.push({ at: child.nameEnd, text: ` data-cms-img="${id}"` }) } fields.push({ id, kind: 'image', page: pageOf(child), tag: 'img', preview: attrOf(child, 'alt') || attrOf(child, 'src') || '' }) continue } const isStat = child.attrs && attrOf(child, 'data-count') !== null if (isStat) { let id = attrOf(child, 'data-cms-num') if (!id) { id = 'n' + String(++maxNum).padStart(3, '0') inserts.push({ at: child.nameEnd, text: ` data-cms-num="${id}"` }) } fields.push({ id, kind: 'number', page: pageOf(child), tag: child.tag, preview: (attrOf(child, 'data-count') || '') + (attrOf(child, 'data-suffix') || ''), }) continue // the animated value is the field; its text node is not } const editable = !claimed && CANDIDATE.has(child.tag) && hasTextDeep(child) && !hasBlockDescendant(child) if (editable) { let id = attrOf(child, 'data-cms') if (!id) { id = 'c' + String(++maxText).padStart(4, '0') inserts.push({ at: child.nameEnd, text: ` data-cms="${id}"` }) } fields.push({ id, kind: 'text', page: pageOf(child), tag: child.tag, preview: textOf(child).slice(0, 140) }) visit(child, { skipping: skip, claimed: true }) continue } } visit(child, { skipping: skip, claimed }) } } visit(root) // ── Write ────────────────────────────────────────────────────────────────────── let out = html for (const ins of inserts.sort((a, b) => b.at - a.at)) { out = out.slice(0, ins.at) + ins.text + out.slice(ins.at) } fs.writeFileSync(MARKUP, out) fs.mkdirSync(path.dirname(MANIFEST), { recursive: true }) fs.writeFileSync(MANIFEST, JSON.stringify(fields, null, 0) + '\n') const byKind = (k) => fields.filter((f) => f.kind === k).length console.log( `siteMarkup.txt: +${inserts.length} new hooks (${fields.length} fields total — ` + `${byKind('text')} text, ${byKind('image')} image, ${byKind('number')} stat)`, )