|
| 1 | +import type { z } from "astro:content"; |
| 2 | +import { generateMarkdownPath } from "./generators"; |
| 3 | +import type { llmstxtSchema } from "./schema"; |
| 4 | + |
| 5 | +export * from "./generators"; |
| 6 | +export * from "./routes"; |
| 7 | +export * from "./topics"; |
| 8 | +export interface DocEntry { |
| 9 | + id: string; |
| 10 | + data: { |
| 11 | + title: string; |
| 12 | + description?: string; |
| 13 | + llmstxt?: z.infer<typeof llmstxtSchema>; |
| 14 | + }; |
| 15 | + body?: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface LlmsTxtAsJson { |
| 19 | + title: string; |
| 20 | + description: string; |
| 21 | + topics: Record< |
| 22 | + string, |
| 23 | + { |
| 24 | + title: string; |
| 25 | + description?: string; |
| 26 | + url: string; |
| 27 | + }[] |
| 28 | + >; |
| 29 | +} |
| 30 | + |
| 31 | +export interface ProcessDocsProps { |
| 32 | + /** |
| 33 | + * Title for the LLMs text file. |
| 34 | + */ |
| 35 | + title: string; |
| 36 | + /** |
| 37 | + * Description for the LLMs text file. |
| 38 | + */ |
| 39 | + description: string; |
| 40 | + /** |
| 41 | + * The site URL, used to generate full URLs for documentation entries. |
| 42 | + */ |
| 43 | + site?: URL; |
| 44 | + /** |
| 45 | + * The collection of documentation entries to process. |
| 46 | + * Each entry must include a valid LLMs text schema. |
| 47 | + * See `import("astro:content").getCollection` |
| 48 | + */ |
| 49 | + docs: DocEntry[]; |
| 50 | + /** |
| 51 | + * Name of the llmstxt section and the pathPrefix to match against doc IDs. |
| 52 | + * If a doc matches multiple pathPrefixes, it will be assigned to the first matching section. |
| 53 | + */ |
| 54 | + llmsSections: { name: string; pathPrefix: string }[]; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Processes astro content collection docs and metadata for easy `llms.txt` generation. |
| 59 | + */ |
| 60 | +export async function processDocsForLlmsTxt({ |
| 61 | + title, |
| 62 | + description, |
| 63 | + site, |
| 64 | + docs, |
| 65 | + llmsSections, |
| 66 | +}: ProcessDocsProps) { |
| 67 | + const sections = organizeDocsIntoSections(docs, llmsSections); |
| 68 | + const result: LlmsTxtAsJson = { title, description, topics: {} }; |
| 69 | + |
| 70 | + const siteHref = site?.href ?? ""; |
| 71 | + for (const [sectionName, sectionDocs] of Object.entries(sections)) { |
| 72 | + if (sectionDocs.length === 0) continue; |
| 73 | + |
| 74 | + const topic = sectionName; |
| 75 | + const topics = sectionDocs.map((doc) => { |
| 76 | + const title = doc.data.title; |
| 77 | + const desc = doc.data.description ?? ""; |
| 78 | + const path = generateMarkdownPath(doc.id); |
| 79 | + const url = mergeSiteWithPath(siteHref, path); |
| 80 | + return { title, description: desc, url }; |
| 81 | + }); |
| 82 | + |
| 83 | + result.topics[topic] = topics; |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +function organizeDocsIntoSections( |
| 90 | + docs: DocEntry[], |
| 91 | + llmsSections: ProcessDocsProps["llmsSections"], |
| 92 | +) { |
| 93 | + docs.sort((a, b) => (a.id > b.id ? 1 : -1)); |
| 94 | + const seenDocs = new Set<DocEntry>(); |
| 95 | + const sections: Record<string, DocEntry[]> = {}; |
| 96 | + |
| 97 | + for (const { name, pathPrefix } of llmsSections) { |
| 98 | + sections[name] = docs.filter((doc) => { |
| 99 | + if (seenDocs.has(doc)) return false; |
| 100 | + if (doc.id.startsWith(pathPrefix)) { |
| 101 | + seenDocs.add(doc); |
| 102 | + return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + return sections; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Merges a site URL with path parts. |
| 113 | + * Used when needing to create full URLs when working with astro content collections. |
| 114 | + * @param siteHref The base URL of the site. |
| 115 | + * @param pathParts The path parts to merge with the site URL. |
| 116 | + * @returns The merged URL. |
| 117 | + */ |
| 118 | +export function mergeSiteWithPath(siteHref: string, ...pathParts: string[]): string { |
| 119 | + let result = siteHref; |
| 120 | + |
| 121 | + for (const part of pathParts) { |
| 122 | + if (!part) continue; // Skip empty parts |
| 123 | + |
| 124 | + const resultTrailingSlash = result.endsWith("/"); |
| 125 | + const partLeadingSlash = part.startsWith("/"); |
| 126 | + |
| 127 | + if (resultTrailingSlash && partLeadingSlash) { |
| 128 | + result = `${result}${part.slice(1)}`; |
| 129 | + } else if (!resultTrailingSlash && !partLeadingSlash) { |
| 130 | + result = `${result}/${part}`; |
| 131 | + } else { |
| 132 | + result = `${result}${part}`; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return result; |
| 137 | +} |
0 commit comments