| 11 | |
| 12 | // Build an index of pages for the given library from the CDN's llms.txt. |
| 13 | export async function buildPageIndex(library: Library): Promise<PageInfo[]> { |
| 14 | if (pageIndexLoaded.has(library)) { |
| 15 | return Array.from(pageCache.values()).filter(p => p.key.startsWith(`${library}/`)); |
| 16 | } |
| 17 | |
| 18 | const pages: PageInfo[] = []; |
| 19 | |
| 20 | // Read llms.txt to enumerate available pages without downloading them all. |
| 21 | const baseUrl = getLibraryBaseUrl(library); |
| 22 | const llmsUrl = `${baseUrl}/llms.txt`; |
| 23 | const txt = await fetchText(llmsUrl); |
| 24 | const re = /^\s*-\s*\[([^\]]+)\]\(([^)]+)\)(?:\s*:\s*(.*))?\s*$/; |
| 25 | for (const line of txt.split(/\r?\n/)) { |
| 26 | const m = line.match(re); |
| 27 | if (!m) { |
| 28 | continue; |
| 29 | } |
| 30 | const display = (m[1] || '').trim(); |
| 31 | const href = (m[2] || '').trim(); |
| 32 | const description = (m[3] || '').trim() || undefined; |
| 33 | if (!href || !/\.md$/i.test(href)) { |
| 34 | continue; |
| 35 | } |
| 36 | const hrefWithoutExt = href.replace(/\.md$/i, '').replace(/\\/g, '/'); |
| 37 | const key = `${library}/${hrefWithoutExt}`; |
| 38 | const name = display || path.basename(hrefWithoutExt); |
| 39 | const filePath = `${baseUrl}/${hrefWithoutExt}.md`; |
| 40 | const info: PageInfo = {key, name, description, filePath, sections: []}; |
| 41 | pages.push(info); |
| 42 | pageCache.set(info.key, info); |
| 43 | } |
| 44 | |
| 45 | pageIndexLoaded.add(library); |
| 46 | return pages.sort((a, b) => a.key.localeCompare(b.key)); |
| 47 | } |
| 48 | |
| 49 | export async function ensureParsedPage(info: PageInfo): Promise<PageInfo> { |
| 50 | if (info.sections && info.sections.length > 0 && info.description !== undefined) { |