34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
#!/usr/bin/env node
|
|||
|
|
// Assembles sites/<site>/public/header.html from shared/header.template.html
|
||
|
|
// + sites/<site>/nav.html. Run automatically via each site's predev/prebuild.
|
||
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
import path from 'node:path';
|
||
|
|
|
||
|
|
const site = process.argv[2];
|
||
|
|
if (!site) {
|
||
|
|
console.error('usage: render-header.mjs <site>');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const root = path.resolve(fileURLToPath(import.meta.url), '../..');
|
||
|
|
const templatePath = path.join(root, 'shared/header.template.html');
|
||
|
|
const navPath = path.join(root, 'sites', site, 'nav.html');
|
||
|
|
const outPath = path.join(root, 'sites', site, 'public/header.html');
|
||
|
|
|
||
|
|
const template = readFileSync(templatePath, 'utf8');
|
||
|
|
const nav = readFileSync(navPath, 'utf8').trimEnd();
|
||
|
|
|
||
|
|
const placeholder = '<!--NAV_ITEMS-->';
|
||
|
|
const parts = template.split(placeholder);
|
||
|
|
if (parts.length !== 3) {
|
||
|
|
console.error(`expected exactly 2 occurrences of ${placeholder} in ${templatePath}, found ${parts.length - 1}`);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const indent = (block, spaces) =>
|
||
|
|
block.split('\n').map(line => ' '.repeat(spaces) + line).join('\n');
|
||
|
|
|
||
|
|
const rendered = parts[0] + indent(nav, 8) + parts[1] + indent(nav, 10) + parts[2];
|
||
|
|
writeFileSync(outPath, rendered);
|