/** * The documentation site's page model. * * Each page model includes **the locale**. Every * page belongs to one, its URL carries that locale's prefix, and the same page in * another language is a sibling reached by swapping the prefix. Getting that wrong is * not a visible bug — it is a set of hreflang tags pointing at 404s, which nobody * notices until a search console says so months later. */ import { join } from 'node:path'; import { readSources } from 'ranpress'; import type { Frontmatter, MarkdownRenderer, Section, TocEntry } from 'ranpress'; import { setRenderContext } from './render-context.ts'; import { LOCALES, ROOT_LOCALE, SITE } from './config.ts'; import type { LocaleDef } from './config.ts'; import { pageUrl, sourceUrl } from './urls.ts'; import { designReferenceSources } from './design-reference.ts'; import { componentElements, componentFacts } from './component-facts.ts'; import { playground } from './playground.ts'; import { messagesFor } from './langs/index.ts'; import { slugify } from 'ranpress'; export type DocKind = 'page' | 'notfound'; export interface DocPage { kind: DocKind; /** Source path, for messages: `docs/cn/src/ranui/index.md`, or `design/DESIGN.md` for the design reference. */ file: string; /** The locale this page is written in. */ locale: LocaleDef; /** * The path with the locale prefix removed — `src/ranui/index.md`. Two pages in * different languages share this, and that is what makes them translations of one * another rather than unrelated URLs. */ baseRel: string; /** Site-absolute public URL, extensionless: `/cn/ranui/button/`. */ url: string; /** * The URL the source tree implies, `/cn/src/ranui/button/`. Relative markdown links * resolve from here; absolute links should already be public paths. Also the retired * public URL kept reachable by redirects. */ sourceUrl: string; outFile: string; title: string; description: string; html: string; toc: TocEntry[]; /** The page split at its headings, for the search index. */ sections: Section[]; } /** * Cloudflare Pages serves `/404.html` from the root for any unmatched path, so the page * has to land there literally rather than at `404/index.html`. */ const kindFor = (rel: string): DocKind => (rel === '404.md' ? 'notfound' : 'page'); /** `cn/src/ranui/index.md` → the `cn` locale; anything unprefixed is the root locale. */ const localeOf = (rel: string): LocaleDef => { const head = rel.split('/')[0]; return LOCALES.find((locale) => locale.dir && locale.dir === head) ?? ROOT_LOCALE; }; /** * Where a page is written, which decides the URL the host actually serves. * * The trailing slash already carries the distinction: `foo/index.md` produced `/foo/` * and `foo.md` produced `/foo`. The file layout has to follow it — `foo.html` for the * second, not `foo/index.html`. * * Getting this wrong does not break a page. Cloudflare Pages serves `foo/index.html` at * `/foo/` and **308s `/foo` to it**, so every canonical and sitemap URL without a * trailing slash — 904 of them — quietly named a redirect instead of a page. That is the * same failure packages/docs hit once before in mirror image, and it is invisible from a * browser: the page loads, just one hop late and under a different URL than the one it * claims to be canonical for. * * Comparing canonicals between the two engines does not catch it either. The strings are * identical; it is the file layout underneath them that changed. */ const outFileFor = (url: string, kind: DocKind): string => { if (kind === 'notfound') return '404.html'; if (url === '/') return 'index.html'; const clean = url.replace(/^\//, ''); return clean.endsWith('/') ? `${clean}index.html` : `${clean}.html`; }; /** * The site's first prose paragraph is a far better meta description than a site-wide * template. Without this every page shares one string — which is what happened here * across 316 pages before VitePress's config grew a `deriveDescription`. */ const MAX_DESCRIPTION = 155; export interface DocContent { pages: DocPage[]; /** Every URL the build produced, for existence-checking hreflang alternates. */ urls: ReadonlySet; /** `baseRel` → the locales that actually ship that page. */ translations: ReadonlyMap; } export const loadDocs = (root: string, markdown: MarkdownRenderer): DocContent => { const pages: DocPage[] = []; const seen = new Map(); const sources = readSources(root, { // Everything that is not prose: the generator, its data, the client bundle, the // stylesheets, and whatever is copied verbatim into the output. ignore: ['node_modules', 'public', 'dist', 'bin', 'build', 'client', 'styles', 'assets', 'design-i18n'], // Orientation for maintainers, not a page — and VitePress excludes it for the same // reason, which is how its own "a bare {{ breaks the build" note used to break it. ignoreFiles: ['CLAUDE.md'], labelPrefix: 'docs', }); const designReference = designReferenceSources(join(root, '..', '..', 'design')).map((source) => ({ ...source, data: { title: source.title } as Frontmatter, })); const factsFor = componentFacts(root); const playgroundFor = playground(root, componentElements(root)); for (const { rel, label, data, content: source } of [...sources, ...designReference]) { const kind = kindFor(rel); const locale = localeOf(rel); const baseRel = locale.dir ? rel.slice(locale.dir.length + 1) : rel; const url = pageUrl(baseRel, locale.dir); const legacy = sourceUrl(baseRel, locale.dir); // A component page's title is followed by its element names and reference links. // A component page's title is followed by its element names and reference links, and // the page ends with a playground for its first live example. const { ui } = messagesFor(locale); const play = playgroundFor(baseRel, source, { heading: ui.playground, markup: ui.playgroundMarkup }); const facts = factsFor(baseRel, locale, play ? slugify(ui.playground) : undefined); const titled = facts ? source.replace(/^# .+$/m, (title) => `${title}\n\n${facts}\n`) : source; const content = `${titled}${play}`; const clash = seen.get(url); if (clash) throw new Error(`${label}: URL ${url} is already produced by ${clash}`); seen.set(url, label); // The component renderers need the page's language; link resolution needs the page // a relative path is relative to. setRenderContext({ locale, url: legacy }); const { html, toc, excerpt, sections } = markdown.render(content); const fmTitle = typeof data.title === 'string' ? data.title : ''; const fmDescription = typeof data.description === 'string' ? data.description : ''; // A page's own H1 is its title when frontmatter does not say otherwise; falling back // to the filename would put slugs in , which is what a reader sees in a tab. const heading = toc.find((entry) => entry.level === 2)?.text ?? ''; const title = fmTitle || /^#\s+(.+)$/m.exec(content)?.[1]?.trim() || heading || 'ran'; // The home pages are a single component placeholder with no prose to derive from, // so they fall back to the site description rather than shipping an empty one — the // most-linked page on the site is the worst one to leave without a summary. const description = (fmDescription || excerpt || SITE.description).slice(0, MAX_DESCRIPTION); pages.push({ kind, file: label, locale, baseRel, url, sourceUrl: legacy, outFile: outFileFor(url, kind), title, description, html, toc, sections, }); } const translations = new Map<string, LocaleDef[]>(); for (const page of pages) { translations.set(page.baseRel, [...(translations.get(page.baseRel) ?? []), page.locale]); } return { pages, urls: new Set(pages.map((page) => page.url)), translations }; };