返回 CodeWhale
reference-guides.ts
根目录 / web / lib / reference-guides.ts
1 import { AGENT_DOCS } from "./agent-docs";
2 import { REFERENCE_GUIDES } from "./content/reference-guides";
3 import { pickText } from "./i18n/dictionaries";
4 import { buildInstallGuide } from "../scripts/install-guide-lib.mjs";
5
6 export function getReferenceGuide(slug: string) {
7 const spec = REFERENCE_GUIDES.find((entry) => entry.slug === slug);
8 if (!spec) throw new Error(`Unknown reference guide: ${slug}`);
9 const document = AGENT_DOCS.find((entry) => entry.slug === slug);
10 const source = document?.sources.find((entry) => entry.path === spec.repoSource);
11 if (!source) throw new Error(`Missing canonical reference source: ${spec.repoSource}`);
12 const sources = document!.sources;
13 const body = sources.map((entry, index) => `${index ? "## Registered HTTP paths\n\n" : ""}${entry.content.replace(/^# [^\n]+\n/, "")}`).join("\n\n");
14 const guide = buildInstallGuide(body, { sourcePath: spec.repoSource.replace(/^docs\//, ""), tableLabel: "Reference table" });
15 return { spec, source, sources, guide };
16 }
17
18 export function getReferenceSearchSections(locale: string) {
19 return REFERENCE_GUIDES.flatMap((spec) => {
20 const { guide } = getReferenceGuide(spec.slug);
21 return [
22 { id: "overview", title: pickText(spec.title, locale), text: `${pickText(spec.lead, locale)} ${guide.preamble}` },
23 ...guide.searchSections,
24 ].map((section) => ({
25 href: `/docs/${spec.slug}#${section.id}`,
26 pageTitle: pickText(spec.title, locale),
27 title: section.title,
28 text: section.text,
29 lang: "en",
30 }));
31 });
32 }
33
33 lines TYPESCRIPT