Add support for multilingual legal pages with English aliases, dynamic link replacement in headers, and redirections for alias routes. Update tests, configs, and frontend assets accordingly.

This commit is contained in:
Overlord committed 2026-10-13 16:41:08 +02:00
1 parent 5f3ff481b4
commit 5305687f98
16 files changed
+151 -39

No files matched your search

+23 -1
View File
@@ -6,7 +6,7 @@ import { renderMarkdown, renderText } from './render.ts';
import { buildSite, reservedNames, type Site } from './site.ts';
import { buildIndex, type WalkOptions } from './walk.ts';
const LEGAL = new Map([['impressum', 'impressum.txt']]);
const LEGAL = [{ route: 'impressum', aliases: ['imprint'], file: 'impressum.txt' }];
function options(overrides: Partial<WalkOptions> = {}): WalkOptions {
return { maxFileBytes: 1024, maxTotalBytes: 4096, maxEntries: 100, reserved: reservedNames(LEGAL), ...overrides };
@@ -149,6 +149,28 @@ describe('buildSite', () => {
expect(text(site, '/impressum')).toContain('Max Mustermann');
});
it('redirects English aliases to the German legal pages and labels links for both', () => {
expect(site.pages.get('/imprint')).toMatchObject({ status: 301, headers: { Location: '/impressum' } });
expect(text(site, '/ls')).toContain('<a href="/impressum" data-en="imprint">impressum</a>');
expect(text(site, '/sitemap.xml')).not.toContain('/imprint');
});
it('renders markdown legal pages and leaves out pages that are not configured', async () => {
const root = mkdtempSync(join(tmpdir(), 'legal-'));
write(root, { 'datenschutz.md': '# Datenschutz\n\nKeine Cookies.', 'impressum.md': 'unused' });
const legal = [{ route: 'datenschutz', aliases: ['privacy'], file: 'datenschutz.md' }];
const only = buildSite(await buildIndex(root, options({ reserved: reservedNames(legal) })), { siteUrl: 'https://a.example', legal });
expect(text(only, '/datenschutz')).toContain('<h1>Datenschutz</h1>');
expect(only.pages.has('/impressum')).toBe(false);
expect(only.pages.has('/imprint')).toBe(false);
expect(text(only, '/ls')).not.toContain('href="/impressum"');
});
it('reserves the aliases in the content', () => {
expect(reservedNames(LEGAL).has('imprint')).toBe(true);
});
it('lists every page in the sitemap and points robots.txt at it', () => {
const sitemap = text(site, '/sitemap.xml')!;
+8 -2
View File
@@ -1,7 +1,8 @@
import { Marked, type Tokens } from 'marked';
import type { Entry } from './walk.ts';
export type Link = { label: string; href: string };
/** A link; english is the label for visitors whose browser does not prefer German. */
export type Link = { label: string; href: string; english?: string };
/** A woff2 file of JetBrains Mono from the frontend build, whose file names carry a hash. */
export type Font = { url: string; weight: number };
@@ -133,7 +134,11 @@ function fontFaces(fonts: Font[]): string
export function renderPage(options: PageOptions): string
{
const crumbs = options.crumbs.map((crumb) => `<a href="${crumb.href}">${escapeHtml(crumb.label)}</a>`).join(' / ');
const nav = options.nav.map((link) => `<a href="${link.href}">${escapeHtml(link.label)}</a>`).join('');
const nav = options.nav.map((link) =>
{
const english = link.english ? ` data-en="${escapeHtml(link.english)}"` : '';
return `<a href="${link.href}"${english}>${escapeHtml(link.label)}</a>`;
}).join('');
return `<!doctype html>
<html lang="en">
@@ -149,6 +154,7 @@ export function renderPage(options: PageOptions): string
<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">
<script src="/labels.js" defer></script>
<style>${fontFaces(options.fonts ?? [])}${STYLE}</style>
</head>
<body>
+23 -10
View File
@@ -5,6 +5,15 @@ export type SitePage = {
body: Buffer;
type: string;
headers: Record<string, string>;
/** Set for redirects; pages without it are served with 200. */
status?: number;
};
/** A legal page under its German route, with English aliases that redirect to it. */
export type LegalPage = {
route: string;
aliases: string[];
file: string;
};
export type Site = {
@@ -15,19 +24,18 @@ export type Site = {
export type SiteOptions = {
/** Absolute URL of the site, used by the sitemap. */
siteUrl: string;
/** Route name to content file, for example impressum to impressum.txt. */
legal: Map<string, string>;
legal: LegalPage[];
fonts?: Font[];
};
const RESERVED = [
'ws', 'raw', 'ls', 'healthz', 'assets', 'index.html', 'sitemap.xml', 'robots.txt',
'theme.css', 'favicon.ico', 'icon-192.png', 'apple-touch-icon.png',
'theme.css', 'labels.js', '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> {
return new Set([...RESERVED, ...legal.keys()]);
/** Route names the content cannot use at its top level, including the legal pages and their aliases. */
export function reservedNames(legal: LegalPage[]): Set<string> {
return new Set([...RESERVED, ...legal.flatMap((page) => [page.route, ...page.aliases])]);
}
const RAW_TYPES: [RegExp, string][] = [
@@ -82,7 +90,7 @@ export function buildSite(index: ContentIndex, options: SiteOptions): Site
const pages = new Map<string, SitePage>();
const nav: Link[] = [
{ label: 'ls', href: '/ls' },
...[...options.legal.keys()].map((route) => ({ label: route, href: `/${route}` })),
...options.legal.map((page) => ({ label: page.route, href: `/${page.route}`, english: page.aliases[0] })),
];
const html = (title: string, description: string, entryCrumbs: Link[], body: string): SitePage => ({
@@ -107,20 +115,25 @@ export function buildSite(index: ContentIndex, options: SiteOptions): Site
if (entry.content) pages.set(`/raw/${entry.path}`, rawPage(entry as Entry & { content: Buffer }));
}
for (const [routeName, file] of options.legal)
for (const { route, aliases, file } of options.legal)
{
const entry = index.entries.get(file);
const body = entry?.kind === 'file'
? renderFile(entry)
: `<p class="muted">${escapeHtml(file)} is missing from the content directory.</p>`;
pages.set(`/${routeName}`, html(`${routeName} · security-command.org`, `${routeName} of security-command.org.`, [], body));
pages.set(`/${route}`, html(`${route} · security-command.org`, `${route} of security-command.org.`, [], body));
for (const alias of aliases)
{
pages.set(`/${alias}`, { body: Buffer.alloc(0), type: 'text/plain; charset=utf-8', headers: { Location: `/${route}` }, status: 301 });
}
}
const base = options.siteUrl.replace(/\/$/, '');
const urls: [string, Date | undefined][] = [
['/', undefined],
...[...index.entries.values()].map((entry): [string, Date] => [entry.path ? `/${urlPath(entry.path)}` : '/ls', entry.modified]),
...[...options.legal].map(([route, file]): [string, Date | undefined] => [`/${route}`, index.entries.get(file)?.modified]),
...options.legal.map(({ route, file }): [string, Date | undefined] => [`/${route}`, index.entries.get(file)?.modified]),
];
const sitemap = [