|
| 1 | +import { writeFileSync, mkdirSync, readdirSync, readFileSync, existsSync } from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { SiteConfig } from 'vitepress' |
| 4 | +import type { Plugin } from 'vite' |
| 5 | +// @ts-ignore |
| 6 | +import matter from 'gray-matter' |
| 7 | +import { llmsConfig } from '../llms.config' |
| 8 | + |
| 9 | +interface PageInfo { |
| 10 | + title: string |
| 11 | + llms_description: string |
| 12 | + url: string |
| 13 | + srcPath: string |
| 14 | + content: string |
| 15 | + section: 'docs' | 'optional' |
| 16 | +} |
| 17 | + |
| 18 | +function extractH1(content: string): string | null { |
| 19 | + const match = content.match(/^#\s+(.+)$/m) |
| 20 | + return match ? match[1].trim() : null |
| 21 | +} |
| 22 | + |
| 23 | +function readPages(srcDir: string): PageInfo[] { |
| 24 | + const pages: PageInfo[] = [] |
| 25 | + |
| 26 | + const docsDir = path.join(srcDir, 'docs') |
| 27 | + if (existsSync(docsDir)) { |
| 28 | + for (const file of readdirSync(docsDir).filter(f => f.endsWith('.md'))) { |
| 29 | + const filePath = path.join(docsDir, file) |
| 30 | + const raw = readFileSync(filePath, 'utf-8') |
| 31 | + const { data: fm, content } = matter(raw) |
| 32 | + |
| 33 | + if (fm.llms === false || !fm.llms_description) continue |
| 34 | + |
| 35 | + const slug = file.replace(/\.md$/, '') |
| 36 | + const llmsValue = fm.llms ?? true |
| 37 | + pages.push({ |
| 38 | + title: fm.title || extractH1(content) || slug, |
| 39 | + llms_description: fm.llms_description, |
| 40 | + url: `/docs/${slug}`, |
| 41 | + srcPath: `llm/docs/${file}`, |
| 42 | + content: content.trim(), |
| 43 | + section: llmsValue === 'optional' ? 'optional' : 'docs', |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return pages |
| 49 | +} |
| 50 | + |
| 51 | +function getSidebarPaths(config: SiteConfig): string[] { |
| 52 | + const paths: string[] = [] |
| 53 | + |
| 54 | + const sidebar = |
| 55 | + (config.site.locales?.root as any)?.themeConfig?.sidebar ?? |
| 56 | + config.site.themeConfig?.sidebar ?? |
| 57 | + {} |
| 58 | + |
| 59 | + function extract(items: any[]) { |
| 60 | + if (!Array.isArray(items)) return |
| 61 | + for (const item of items) { |
| 62 | + if (item.link) paths.push(item.link) |
| 63 | + if (item.items) extract(item.items) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + for (const section of Object.values(sidebar)) { |
| 68 | + if (Array.isArray(section)) extract(section) |
| 69 | + } |
| 70 | + |
| 71 | + return paths |
| 72 | +} |
| 73 | + |
| 74 | +function sortPages(pages: PageInfo[], sidebarPaths: string[]): PageInfo[] { |
| 75 | + const order = new Map(sidebarPaths.map((p, i) => [p, i])) |
| 76 | + |
| 77 | + return [...pages].sort((a, b) => { |
| 78 | + const ia = order.get(a.url) ?? 9999 |
| 79 | + const ib = order.get(b.url) ?? 9999 |
| 80 | + return ia - ib || a.url.localeCompare(b.url) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +function buildLlmsTxt(pages: PageInfo[]): string { |
| 85 | + const { title, summary, details, baseUrl, docsSection, optionalSection } = llmsConfig |
| 86 | + |
| 87 | + const docs = pages.filter(p => p.section === 'docs') |
| 88 | + const optional = pages.filter(p => p.section === 'optional') |
| 89 | + |
| 90 | + const lines: string[] = [ |
| 91 | + `# ${title}`, |
| 92 | + '', |
| 93 | + `> ${summary}`, |
| 94 | + '', |
| 95 | + ...details.map(d => `- ${d}`), |
| 96 | + '', |
| 97 | + ] |
| 98 | + |
| 99 | + if (docs.length > 0) { |
| 100 | + lines.push(`## ${docsSection}`, '') |
| 101 | + for (const p of docs) { |
| 102 | + lines.push(`- [${p.title}](${baseUrl}/${p.srcPath}): ${p.llms_description}`) |
| 103 | + } |
| 104 | + lines.push('') |
| 105 | + } |
| 106 | + |
| 107 | + if (optional.length > 0) { |
| 108 | + lines.push(`## ${optionalSection}`, '') |
| 109 | + for (const p of optional) { |
| 110 | + lines.push(`- [${p.title}](${baseUrl}/${p.srcPath}): ${p.llms_description}`) |
| 111 | + } |
| 112 | + lines.push('') |
| 113 | + } |
| 114 | + |
| 115 | + return lines.join('\n') |
| 116 | +} |
| 117 | + |
| 118 | +function buildLlmsFullTxt(pages: PageInfo[]): string { |
| 119 | + const { title, summary, details } = llmsConfig |
| 120 | + |
| 121 | + const lines: string[] = [ |
| 122 | + `# ${title}`, |
| 123 | + '', |
| 124 | + `> ${summary}`, |
| 125 | + '', |
| 126 | + ...details.map(d => `- ${d}`), |
| 127 | + ] |
| 128 | + |
| 129 | + for (const page of pages) { |
| 130 | + const startsWithH1 = /^#\s+/.test(page.content) |
| 131 | + lines.push('', '---', '') |
| 132 | + if (!startsWithH1) { |
| 133 | + lines.push(`# ${page.title}`, '') |
| 134 | + } |
| 135 | + lines.push(page.content) |
| 136 | + } |
| 137 | + |
| 138 | + lines.push('') |
| 139 | + return lines.join('\n') |
| 140 | +} |
| 141 | + |
| 142 | +// Build-time generation (called from buildEnd) |
| 143 | +export async function generateLlms(config: SiteConfig) { |
| 144 | + const pages = sortPages(readPages(config.srcDir), getSidebarPaths(config)) |
| 145 | + |
| 146 | + // Per-page .md files |
| 147 | + for (const page of pages) { |
| 148 | + const outPath = path.join(config.outDir, page.srcPath) |
| 149 | + mkdirSync(path.dirname(outPath), { recursive: true }) |
| 150 | + writeFileSync(outPath, page.content + '\n') |
| 151 | + } |
| 152 | + |
| 153 | + writeFileSync(path.join(config.outDir, 'llms.txt'), buildLlmsTxt(pages)) |
| 154 | + writeFileSync(path.join(config.outDir, 'llms-full.txt'), buildLlmsFullTxt(pages)) |
| 155 | + |
| 156 | + console.log(`✓ llms.txt generated (${pages.length} docs)`) |
| 157 | + console.log(`✓ llms-full.txt generated`) |
| 158 | + console.log(`✓ ${pages.length} per-page .md files generated`) |
| 159 | +} |
| 160 | + |
| 161 | +// Vite plugin for dev server |
| 162 | +export function llmsPlugin(): Plugin { |
| 163 | + let docsRoot: string |
| 164 | + |
| 165 | + return { |
| 166 | + name: 'vitepress-llms-dev', |
| 167 | + configResolved(config) { |
| 168 | + docsRoot = config.root |
| 169 | + }, |
| 170 | + configureServer(server) { |
| 171 | + server.middlewares.use((req, res, next) => { |
| 172 | + const url = req.url || '' |
| 173 | + |
| 174 | + if (url === '/llms.txt') { |
| 175 | + const pages = sortPages(readPages(docsRoot), []) |
| 176 | + res.setHeader('Content-Type', 'text/plain; charset=utf-8') |
| 177 | + res.end(buildLlmsTxt(pages)) |
| 178 | + return |
| 179 | + } |
| 180 | + |
| 181 | + if (url === '/llms-full.txt') { |
| 182 | + const pages = sortPages(readPages(docsRoot), []) |
| 183 | + res.setHeader('Content-Type', 'text/plain; charset=utf-8') |
| 184 | + res.end(buildLlmsFullTxt(pages)) |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + // Serve per-page .md files |
| 189 | + const mdMatch = url.match(/^\/llm\/docs\/(.+\.md)$/) |
| 190 | + if (mdMatch) { |
| 191 | + const filePath = path.join(docsRoot, 'docs', mdMatch[1]) |
| 192 | + if (existsSync(filePath)) { |
| 193 | + const raw = readFileSync(filePath, 'utf-8') |
| 194 | + const { content } = matter(raw) |
| 195 | + res.setHeader('Content-Type', 'text/markdown; charset=utf-8') |
| 196 | + res.end(content.trim() + '\n') |
| 197 | + return |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + next() |
| 202 | + }) |
| 203 | + }, |
| 204 | + } |
| 205 | +} |
0 commit comments