|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Render a static HTML fragment from contracts.json for pre-rendering. |
| 4 | + * |
| 5 | + * The contracts page is JS-interactive (checkboxes, download), but crawlers |
| 6 | + * and LLMs need to see the content without executing JavaScript. |
| 7 | + * This script generates a plain HTML summary of all contracts that |
| 8 | + * prerender-routes.js injects into the static shell. |
| 9 | + * |
| 10 | + * Output: website/public/docs/contracts.html |
| 11 | + * |
| 12 | + * Usage: node scripts/render-contracts.js |
| 13 | + */ |
| 14 | + |
| 15 | +const fs = require('fs') |
| 16 | +const path = require('path') |
| 17 | + |
| 18 | +const ROOT = path.join(__dirname, '..') |
| 19 | +const CONTRACTS_JSON = path.join(ROOT, 'website/public/data/contracts.json') |
| 20 | +const OUTPUT = path.join(ROOT, 'website/public/docs/contracts.html') |
| 21 | + |
| 22 | +function escapeHtml(str) { |
| 23 | + return String(str) |
| 24 | + .replace(/&/g, '&') |
| 25 | + .replace(/</g, '<') |
| 26 | + .replace(/>/g, '>') |
| 27 | + .replace(/"/g, '"') |
| 28 | +} |
| 29 | + |
| 30 | +function renderTemplate(template) { |
| 31 | + return template |
| 32 | + .split('\n') |
| 33 | + .map((line) => { |
| 34 | + if (line.startsWith('- ')) { |
| 35 | + return `<li>${escapeHtml(line.slice(2))}</li>` |
| 36 | + } |
| 37 | + if (line.trim() === '') return '' |
| 38 | + return `<p>${escapeHtml(line)}</p>` |
| 39 | + }) |
| 40 | + .join('\n') |
| 41 | +} |
| 42 | + |
| 43 | +function renderContract(contract) { |
| 44 | + const anchors = contract.anchors |
| 45 | + .map( |
| 46 | + (id) => |
| 47 | + `<a href="#/anchor/${escapeHtml(id)}" class="inline-block rounded-full bg-blue-100 dark:bg-blue-900/30 px-2 py-0.5 text-xs text-blue-700 dark:text-blue-300">${escapeHtml(id)}</a>` |
| 48 | + ) |
| 49 | + .join(' ') |
| 50 | + |
| 51 | + return ` |
| 52 | + <div class="rounded-lg border border-[var(--color-border)] bg-[var(--color-bg)] p-5 mb-4"> |
| 53 | + <h3 class="text-lg font-semibold mb-1">${escapeHtml(contract.title)}</h3> |
| 54 | + <p class="text-sm text-[var(--color-text-secondary)] mb-3">${escapeHtml(contract.description)}</p> |
| 55 | + <div class="rounded-md bg-[var(--color-bg-secondary)] p-3 mb-3 text-sm leading-relaxed"> |
| 56 | + <ul class="list-disc list-inside space-y-1"> |
| 57 | + ${renderTemplate(contract.template)} |
| 58 | + </ul> |
| 59 | + </div> |
| 60 | + <div class="flex flex-wrap gap-1.5">${anchors}</div> |
| 61 | + </div>` |
| 62 | +} |
| 63 | + |
| 64 | +function main() { |
| 65 | + if (!fs.existsSync(CONTRACTS_JSON)) { |
| 66 | + console.error(`ERROR: ${CONTRACTS_JSON} not found`) |
| 67 | + process.exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + const contracts = JSON.parse(fs.readFileSync(CONTRACTS_JSON, 'utf-8')) |
| 71 | + |
| 72 | + const html = ` |
| 73 | +<h1>Semantic Contracts</h1> |
| 74 | +<p class="text-[var(--color-text-secondary)] mb-6"> |
| 75 | + Semantic Anchors reference public knowledge that LLMs already understand. |
| 76 | + But your team's conventions, templates, and definitions need Semantic Contracts. |
| 77 | + A contract defines what a term means in your project — either by composing |
| 78 | + established anchors or by providing custom definitions that only exist within your team. |
| 79 | +</p> |
| 80 | +<div class="grid gap-4 md:grid-cols-2"> |
| 81 | + ${contracts.map(renderContract).join('\n')} |
| 82 | +</div>` |
| 83 | + |
| 84 | + fs.mkdirSync(path.dirname(OUTPUT), { recursive: true }) |
| 85 | + fs.writeFileSync(OUTPUT, html, 'utf-8') |
| 86 | + console.log(`Rendered: ${path.relative(ROOT, OUTPUT)}`) |
| 87 | +} |
| 88 | + |
| 89 | +main() |
0 commit comments