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('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"'); const document = ({ title, description, body, noindex = false }) => ` ${escape(title)} ${body} `; /** Sidebar: the page tree and a search box that works without scripts. */ function sidebar(categories, currentSlug, term = '') { const groups = categories .map( (category) => ` ${category.pages .map( (page) => `${escape(page.title)}`, ) .join('\n ')}`, ) .join('\n'); return ` `; } /** Table of contents, built from the headings the renderer collected. */ const contents = (headings) => headings.length < 2 ? '' : ` `; const footerLinks = () => { const links = [ env.links.website && `Site`, env.links.docs && `Documentation`, `Conditions d'utilisation`, `Politique de confidentialité`, ].filter(Boolean); return ``; }; const shell = (categories, currentSlug, main, term) => `
${sidebar(categories, currentSlug, term)}
${main} ${footerLinks()}
`; /** 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 = `

${escape(page.category)}

${escape(page.title)}

${page.summary ? `

${escape(page.summary)}

` : ''}
${html}
${contents(headings)}
`; 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 ? '

Aucun résultat.

' : ``; const main = results ? `

Recherche

${results.length} résultat(s) pour « ${escape(term)} ».

${list(results)}
` : `

Documentation FWS

Tout ce qu'il faut savoir sur le bot et l'organisation.

${categories .map((category) => `

${escape(category.name)}

${list(category.pages)}`) .join('\n ')}
`; 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 = `

Page introuvable

Cette page n'existe pas, ou n'est pas encore publiée.

Revenir à la documentation

`; return document({ title: 'Page introuvable · Documentation FWS', description: 'Page introuvable.', body: shell(categories, null, main), noindex: true, }); }