79 lines · 2.7 KBRaw
| 1 | import { Marked } from "marked"; |
| 2 | |
| 3 | export interface DocLink { |
| 4 | readonly slug: string; |
| 5 | readonly title: string; |
| 6 | } |
| 7 | |
| 8 | export interface DocSection { |
| 9 | readonly title: string; |
| 10 | readonly pages: readonly DocLink[]; |
| 11 | } |
| 12 | |
| 13 | export interface DocHeading { |
| 14 | readonly id: string; |
| 15 | readonly text: string; |
| 16 | } |
| 17 | |
| 18 | export interface DocPage { |
| 19 | readonly slug: string; |
| 20 | readonly title: string; |
| 21 | readonly summary: string; |
| 22 | readonly html: string; |
| 23 | readonly headings: readonly DocHeading[]; |
| 24 | } |
| 25 | |
| 26 | const SECTIONS: readonly { title: string; slugs: readonly string[] }[] = [ |
| 27 | { title: "Get started", slugs: ["overview", "quickstart", "concepts"] }, |
| 28 | { title: "Guides", slugs: ["git", "connect-an-agent", "work-with-agents", "hosted-agents", "pull-requests", "tasks", "ci", "organizations"] }, |
| 29 | { title: "Reference", slugs: ["decision-records", "ci-config", "protocol", "cli", "permissions-and-limits", "glossary"] }, |
| 30 | ]; |
| 31 | |
| 32 | const SOURCES = import.meta.glob<string>("../docs/*.md", { query: "?raw", import: "default", eager: true }); |
| 33 | |
| 34 | function slugify(text: string): string { |
| 35 | return text.toLowerCase().replace(/<[^>]+>|`/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, ""); |
| 36 | } |
| 37 | |
| 38 | function parse(slug: string, source: string): DocPage { |
| 39 | const [, title = slug, rest = ""] = /^# (.+)\n+([\s\S]*)$/.exec(source) ?? []; |
| 40 | const summary = rest.split("\n\n")[0]?.replace(/\n/g, " ").replace(/[`*]|\[([^\]]+)\]\([^)]+\)/g, "$1") ?? ""; |
| 41 | const headings: DocHeading[] = []; |
| 42 | const marked = new Marked({ |
| 43 | gfm: true, |
| 44 | renderer: { |
| 45 | heading({ tokens, depth, text }) { |
| 46 | const id = slugify(text); |
| 47 | if (depth === 2) headings.push({ id, text: text.replace(/`/g, "") }); |
| 48 | return `<h${depth} id="${id}"><a href="#${id}">${this.parser.parseInline(tokens)}</a></h${depth}>\n`; |
| 49 | }, |
| 50 | }, |
| 51 | }); |
| 52 | const html = marked.parse(rest, { async: false }); |
| 53 | return { slug, title, summary, html, headings }; |
| 54 | } |
| 55 | |
| 56 | const PAGES = new Map( |
| 57 | Object.entries(SOURCES).map(([path, source]) => { |
| 58 | const slug = path.slice(path.lastIndexOf("/") + 1, -".md".length); |
| 59 | return [slug, parse(slug, source)] as const; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | export const DOC_SECTIONS: readonly DocSection[] = SECTIONS.map(({ title, slugs }) => ({ |
| 64 | title, |
| 65 | pages: slugs.map((slug) => { |
| 66 | const page = PAGES.get(slug); |
| 67 | if (!page) throw new Error(`The docs page ${slug}.md is missing.`); |
| 68 | return { slug, title: page.title }; |
| 69 | }), |
| 70 | })); |
| 71 | |
| 72 | const ORDER = DOC_SECTIONS.flatMap((section) => section.pages); |
| 73 | |
| 74 | export function docPage(slug: string): { page: DocPage; previous: DocLink | null; next: DocLink | null } | null { |
| 75 | const page = PAGES.get(slug); |
| 76 | const index = ORDER.findIndex((link) => link.slug === slug); |
| 77 | if (!page || index < 0) return null; |
| 78 | return { page, previous: ORDER[index - 1] ?? null, next: ORDER[index + 1] ?? null }; |
| 79 | } |