README / scripts /render-card.mjs
compute1's picture
Upload scripts/render-card.mjs with huggingface_hub
1f56632 verified
Raw History Blame Contribute Delete
3.13 kB
import { readFileSync, readdirSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import App from '../.ssr/App.js'
// ---------------------------------------------------------------------------
// The org card on hf.co is an inline, sanitized copy of the final HTML:
// - <script> tags are removed (nothing executes)
// - <style> is scoped under a generated class: `:root` matches nothing and
// @media blocks are dropped entirely
// - quotes are stripped from font-family names
// So the output must be fully self-contained: inlined CSS, embedded fonts,
// data-URI logo, no scripts, no external references.
// ---------------------------------------------------------------------------
// 1) Brand fonts, embedded as base64 @font-face (variable files cover all
// used weights). Single-token family names survive quote-stripping.
const fonts = [
{ family: 'OrchSans', file: 'Inter-400.woff2', weight: '100 900' },
{ family: 'OrchGrotesk', file: 'SpaceGrotesk-600.woff2', weight: '300 700' },
{ family: 'OrchMono', file: 'JetBrainsMono-400.woff2', weight: '100 800' },
]
const fontCss = fonts
.map((f) => {
const b64 = readFileSync(join('src/fonts', f.file)).toString('base64')
return `@font-face { font-family: ${f.family}; font-style: normal; font-weight: ${f.weight}; font-display: swap; src: url(data:font/woff2;base64,${b64}) format('woff2'); }`
})
.join('\n')
// 2) Extract the built CSS and expand every var(--x) into its literal value,
// then drop the :root block (which would not survive scoping anyway).
const builtCss = readdirSync('dist/assets')
.filter((f) => f.endsWith('.css'))
.map((f) => readFileSync(join('dist/assets', f), 'utf8'))
.join('\n')
const vars = {}
const rootMatch = builtCss.match(/:root\s*{([^}]+)}/)
if (rootMatch) {
for (const decl of rootMatch[1].split(';')) {
const m = decl.match(/--([\w-]+)\s*:\s*(.+)$/)
if (m) vars['--' + m[1]] = m[2].trim()
}
}
let css = builtCss.replace(/:root\s*{[^}]+}/, '')
css = css.replace(/var\(\s*(--[\w-]+)\s*\)/g, (all, name) => vars[name] ?? all)
if (Object.keys(vars).length && /var\(--/.test(css)) {
throw new Error('unexpanded CSS variable remains: ' + css.match(/var\(--[^)]*\)/)[0])
}
const finalCss = fontCss + '\n' + css
// 3) Logo as a data URI (external asset paths do not resolve once inlined).
const logoB64 = readFileSync('src/assets/logo-horizontal.svg', 'utf8').trim()
const body = renderToStaticMarkup(React.createElement(App)).replace(
/@LOGO_DATA_URI@/g,
'data:image/svg+xml;base64,' + Buffer.from(logoB64, 'utf8').toString('base64'),
)
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Orchestrel AI</title>
<style>${finalCss}</style>
</head>
<body>
${body}
</body>
</html>
`
writeFileSync('dist/index.html', html)
console.log(
'rendered dist/index.html (' +
(html.length / 1024).toFixed(0) +
' KB, self-contained, no scripts)',
)