Add tests for backend asset handling and frontend boot sequence, remove outdated MOTD script, and add icons.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ad5b5f44f
commit
7937879ace
34 files changed
+613
-116
No files matched your search
@@ -138,6 +138,13 @@ describe('buildSite', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('declares the given font files on every page', async () => {
|
||||
const fonts = [{ url: '/assets/jetbrains-mono-latin-400-normal-x.woff2', weight: 400 }];
|
||||
const styled = buildSite(await buildIndex(mkdtempSync(join(tmpdir(), 'site-')), options()), { siteUrl: 'https://a.example', legal: LEGAL, fonts });
|
||||
|
||||
expect(text(styled, '/ls')).toContain('font-weight: 400; font-display: swap; src: url("/assets/jetbrains-mono-latin-400-normal-x.woff2")');
|
||||
});
|
||||
|
||||
it('renders legal pages from their content files', () => {
|
||||
expect(text(site, '/impressum')).toContain('Max Mustermann');
|
||||
});
|
||||
|
||||
@@ -3,6 +3,9 @@ import type { Entry } from './walk.ts';
|
||||
|
||||
export type Link = { label: string; href: string };
|
||||
|
||||
/** A woff2 file of JetBrains Mono from the frontend build, whose file names carry a hash. */
|
||||
export type Font = { url: string; weight: number };
|
||||
|
||||
const SAFE_SCHEME = /^(https?|mailto):/i;
|
||||
const ANY_SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
||||
|
||||
@@ -94,9 +97,8 @@ export function isText(content: Buffer): boolean
|
||||
}
|
||||
|
||||
const STYLE = `
|
||||
:root { color-scheme: dark; --bg: #0b0d10; --text: #d6dde6; --muted: #7d8794; --accent: #7ee787; --border: #1f262e; }
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.6 "JetBrains Mono", ui-monospace, monospace; }
|
||||
body { margin: 0; background: var(--background); color: var(--text); font: 15px/1.6 "JetBrains Mono", ui-monospace, monospace; font-variant-ligatures: no-contextual; }
|
||||
header, main, footer { max-width: 60rem; margin: 0 auto; padding: 0.75rem 1rem; }
|
||||
header { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; justify-content: space-between; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
|
||||
header .site { color: var(--accent); }
|
||||
@@ -109,7 +111,7 @@ table { border-collapse: collapse; width: 100%; }
|
||||
td { padding: 0.15rem 1.5rem 0.15rem 0; white-space: nowrap; }
|
||||
td:first-child { width: 100%; white-space: normal; }
|
||||
img { max-width: 100%; }
|
||||
.markdown code, .markdown pre { background: #12161b; }
|
||||
.markdown code, .markdown pre { background: var(--surface); }
|
||||
`;
|
||||
|
||||
export type PageOptions = {
|
||||
@@ -118,8 +120,16 @@ export type PageOptions = {
|
||||
crumbs: Link[];
|
||||
body: string;
|
||||
nav: Link[];
|
||||
fonts?: Font[];
|
||||
};
|
||||
|
||||
function fontFaces(fonts: Font[]): string
|
||||
{
|
||||
return fonts
|
||||
.map((font) => `@font-face { font-family: "JetBrains Mono"; font-weight: ${font.weight}; font-display: swap; src: url("${encodeURI(font.url)}") format("woff2"); }`)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
export function renderPage(options: PageOptions): string
|
||||
{
|
||||
const crumbs = options.crumbs.map((crumb) => `<a href="${crumb.href}">${escapeHtml(crumb.label)}</a>`).join(' / ');
|
||||
@@ -135,7 +145,11 @@ export function renderPage(options: PageOptions): string
|
||||
<meta name="description" content="${escapeHtml(options.description)}">
|
||||
<meta property="og:title" content="${escapeHtml(options.title)}">
|
||||
<meta property="og:description" content="${escapeHtml(options.description)}">
|
||||
<style>${STYLE}</style>
|
||||
<link rel="icon" href="/favicon.ico" sizes="48x48">
|
||||
<link rel="icon" href="/icon-192.png" sizes="192x192" type="image/png">
|
||||
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
|
||||
<link rel="stylesheet" href="/theme.css">
|
||||
<style>${fontFaces(options.fonts ?? [])}${STYLE}</style>
|
||||
</head>
|
||||
<body>
|
||||
<header><a class="site" href="/">security-command.org</a><nav>${nav}</nav></header>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { escapeHtml, isText, renderFile, renderListing, renderPage, urlPath, type Link } from './render.ts';
|
||||
import { escapeHtml, isText, renderFile, renderListing, renderPage, urlPath, type Font, type Link } from './render.ts';
|
||||
import type { ContentIndex, Entry } from './walk.ts';
|
||||
|
||||
export type SitePage = {
|
||||
@@ -17,9 +17,13 @@ export type SiteOptions = {
|
||||
siteUrl: string;
|
||||
/** Route name to content file, for example impressum to impressum.txt. */
|
||||
legal: Map<string, string>;
|
||||
fonts?: Font[];
|
||||
};
|
||||
|
||||
const RESERVED = ['ws', 'raw', 'ls', 'healthz', 'assets', 'index.html', 'sitemap.xml', 'robots.txt'];
|
||||
const RESERVED = [
|
||||
'ws', 'raw', 'ls', 'healthz', 'assets', 'index.html', 'sitemap.xml', 'robots.txt',
|
||||
'theme.css', 'favicon.ico', 'icon-192.png', 'apple-touch-icon.png',
|
||||
];
|
||||
|
||||
/** Route names the content cannot use at its top level, including the legal pages. */
|
||||
export function reservedNames(legal: Map<string, string>): Set<string> {
|
||||
@@ -83,7 +87,7 @@ export function buildSite(index: ContentIndex, options: SiteOptions): Site
|
||||
];
|
||||
|
||||
const html = (title: string, description: string, entryCrumbs: Link[], body: string): SitePage => ({
|
||||
body: Buffer.from(renderPage({ title, description, crumbs: entryCrumbs, body, nav })),
|
||||
body: Buffer.from(renderPage({ title, description, crumbs: entryCrumbs, body, nav, fonts: options.fonts })),
|
||||
type: 'text/html; charset=utf-8',
|
||||
headers: {},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user