Trin-for-trin guide til at bygge den perfekte build.ts til et statisk HTML-site drevet af @webhouse/cms.
Hvad er build.ts?
build.ts er en custom statisk site-generator der læser dit CMS-indhold (JSON-filer) og outputter ren HTML. Intet framework, intet runtime-JavaScript — bare HTML + CSS der virker overalt.
npx tsx build.ts # Generér dist/Lektion 1: Indlæs indhold
Fundamentet — læs JSON-filer fra content/:
import { readFileSync, readdirSync, existsSync } from 'fs';
import { join } from 'path';
const CONTENT = join(process.cwd(), 'content');
export function getCollection(name: string) {
const dir = join(CONTENT, name);
if (!existsSync(dir)) return [];
return readdirSync(dir)
.filter(f => f.endsWith('.json'))
.map(f => JSON.parse(readFileSync(join(dir, f), 'utf-8')))
.filter(d => d.status === 'published');
}
export function getDocument(collection: string, slug: string) {
const file = join(CONTENT, collection, `${slug}.json`);
if (!existsSync(file)) return null;
return JSON.parse(readFileSync(file, 'utf-8'));
}Lektion 2: Rendér markdown
Konvertér richtext-indhold til HTML:
import { marked } from 'marked';
function renderMarkdown(content: string): string {
return marked.parse(content, { async: false }) as string;
}Lektion 3: HTML-skabelon
Wrap indhold i et komplet HTML-dokument:
function htmlPage(title: string, body: string, css: string): string {
return `<!DOCTYPE html>
<html lang="da">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>${css}</style>
</head>
<body>${body}</body>
</html>`;
}Lektion 4: Byg en side
Kombinér indholdsindlæsning, markdown-rendering og HTML-skabelon.
Lektion 5: Rendér blokke
Hvis dine sider bruger blokke (hero, features, CTA), rendér hver bloktype:
function BlockRenderer({ block }: { block: any }) {
switch (block._block) {
case 'hero':
return (
<section>
<h1>{block.tagline}</h1>
<p>{block.description}</p>
</section>
);
case 'features':
return (
<section>
<h2>{block.title}</h2>
<div className="grid grid-cols-3 gap-4">
{block.items?.map((item: any, i: number) => (
<div key={i}>
<h3>{item.title}</h3>
<p>{item.description}</p>
</div>
))}
</div>
</section>
);
default:
return null;
}
}Lektion 6: SEO-metadata
Udtrk SEO-felter og generér meta-tags fra _seo-feltet.
Lektion 7: Skriv output
Generér filer til dist/ med writeFileSync og mkdirSync.
Lektion 8: Sitemap
Generér sitemap.xml til søgemaskiner.
Lektion 9: Resolve snippets
Tilføj snippet-support til dit build — se Delte Snippets.
Lektion 10: Kopiér statiske assets
Kopiér uploads og public-filer til dist/.
Næste skridt
- Skabeloner — start fra en fungerende boilerplate
- Next.js-mønstre — hvis du vil have React
- Delte Snippets — genbrugelige kodeblokke