Files
FWS-DiscordBot/src/web/services/wiki-render.js
T
nocodeandClaude Opus 5 8869c74b7b fix(wiki): render the public pages on the server
The documentation was assembled in the browser, so a client that runs no
script saw an empty shell — including whoever fetches the terms and privacy
links before approving an application, which is exactly what those pages are
for.

The wiki is now delivered as finished HTML, carries no script at all, and
declares itself indexable, unlike the panel. Search works through a plain
query string, an unknown page answers 404 rather than a blank page, and both
legal pages are linked from the footer of every page.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 23:31:27 +02:00

183 lines
6.3 KiB
JavaScript

import { env } from '../../config/env.js';
import { render as renderMarkdown } from '../public/core/markdown.js';
import { pagePath } from './wiki-seed.js';
/**
* Server-rendered wiki (M17).
*
* The public documentation is delivered as finished HTML rather than assembled
* in the browser. A terms page that needs JavaScript to show its text is a page
* that reads as blank to anything that does not run scripts — a platform
* checking the link before approving an application, a crawler, a reader with
* scripts disabled — and those are exactly the readers it exists for.
*/
const escape = (text) =>
String(text ?? '')
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;');
const document = ({ title, description, body, noindex = false }) => `<!doctype html>
<html lang="fr" data-theme="dark">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="dark light" />
<meta name="description" content="${escape(description)}" />
<meta name="robots" content="${noindex ? 'noindex, nofollow' : 'index, follow'}" />
<title>${escape(title)}</title>
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<link rel="stylesheet" href="/styles.css" />
<link rel="stylesheet" href="/wiki.css" />
</head>
<body class="wiki-body">
${body}
</body>
</html>
`;
/** Sidebar: the page tree and a search box that works without scripts. */
function sidebar(categories, currentSlug, term = '') {
const groups = categories
.map(
(category) => `
<div class="nav-group">${escape(category.name)}</div>
${category.pages
.map(
(page) =>
`<a href="${escape(page.path ?? pagePath(page.slug))}"${
page.slug === currentSlug ? ' class="active" aria-current="page"' : ''
}>${escape(page.title)}</a>`,
)
.join('\n ')}`,
)
.join('\n');
return ` <aside class="wiki-side">
<a class="wiki-brand" href="/wiki">
<img src="/favicon.svg" alt="" width="36" height="36" />
<div><strong>FWS</strong><span>Documentation</span></div>
</a>
<form class="wiki-search" method="get" action="/wiki">
<input class="input" type="search" name="q" value="${escape(term)}" placeholder="Rechercher…" aria-label="Rechercher dans la documentation" />
</form>
<nav class="wiki-nav">
${groups}
</nav>
</aside>`;
}
/** Table of contents, built from the headings the renderer collected. */
const contents = (headings) =>
headings.length < 2
? ''
: ` <nav class="wiki-toc">
<div class="nav-group">Sur cette page</div>
${headings
.map(
(heading) =>
`<a href="#${escape(heading.id)}" data-level="${heading.level}">${escape(heading.text)}</a>`,
)
.join('\n ')}
</nav>`;
const footerLinks = () => {
const links = [
env.links.website && `<a href="${escape(env.links.website)}">Site</a>`,
env.links.docs && `<a href="${escape(env.links.docs)}">Documentation</a>`,
`<a href="/terms-of-service">Conditions d'utilisation</a>`,
`<a href="/privacy-policy">Politique de confidentialité</a>`,
].filter(Boolean);
return `<footer class="wiki-footer">${links.join(' · ')}</footer>`;
};
const shell = (categories, currentSlug, main, term) => ` <div class="wiki-shell">
${sidebar(categories, currentSlug, term)}
<main class="wiki-main">
${main}
${footerLinks()}
</main>
</div>`;
/** A page, with its body already turned into HTML. */
export function renderPage(page, categories) {
const { html, headings } = renderMarkdown(page.content);
const updated = new Date(`${page.updated_at}Z`);
const main = ` <div class="wiki-article">
<article>
<p class="muted">${escape(page.category)}</p>
<h1>${escape(page.title)}</h1>
${page.summary ? `<p class="wiki-summary">${escape(page.summary)}</p>` : ''}
<div class="wiki-content">
${html}
</div>
<footer class="muted wiki-updated">
Dernière modification :
<time datetime="${escape(updated.toISOString())}">${updated.toLocaleDateString('fr-FR', {
dateStyle: 'long',
})}</time>
</footer>
</article>
${contents(headings)}
</div>`;
return document({
title: `${page.title} · Documentation FWS`,
description: page.summary || `${page.title} — documentation de l'organisation FWS.`,
body: shell(categories, page.slug, main),
});
}
/** Landing page, and the search results when a term is given. */
export function renderIndex(categories, { term = '', results = null } = {}) {
const list = (pages) =>
pages.length === 0
? '<p class="empty">Aucun résultat.</p>'
: `<ul class="wiki-index">${pages
.map(
(page) =>
`<li><a href="${escape(page.path ?? pagePath(page.slug))}">${escape(page.title)}</a>${
page.summary ? ` — <span class="muted">${escape(page.summary)}</span>` : ''
}</li>`,
)
.join('')}</ul>`;
const main = results
? ` <article>
<h1>Recherche</h1>
<p class="muted">${results.length} résultat(s) pour « ${escape(term)} ».</p>
${list(results)}
</article>`
: ` <article>
<h1>Documentation FWS</h1>
<p class="wiki-summary">Tout ce qu'il faut savoir sur le bot et l'organisation.</p>
${categories
.map((category) => `<h2>${escape(category.name)}</h2>${list(category.pages)}`)
.join('\n ')}
</article>`;
return document({
title: 'Documentation FWS',
description: "Documentation publique de l'organisation open source FWS.",
body: shell(categories, null, main, term),
});
}
export function renderNotFound(categories) {
const main = ` <article>
<h1>Page introuvable</h1>
<p class="muted">Cette page n'existe pas, ou n'est pas encore publiée.</p>
<p><a href="/wiki">Revenir à la documentation</a></p>
</article>`;
return document({
title: 'Page introuvable · Documentation FWS',
description: 'Page introuvable.',
body: shell(categories, null, main),
noindex: true,
});
}