| 1 | const fs = require("fs"); |
| 2 | const path = require("path"); |
| 3 | |
| 4 | function getAllHtmlFiles(dir, fileList = []) { |
| 5 | const files = fs.readdirSync(dir); |
| 6 | |
| 7 | files.forEach((file) => { |
| 8 | const filePath = path.join(dir, file); |
| 9 | const stat = fs.statSync(filePath); |
| 10 | |
| 11 | if (stat.isDirectory()) { |
| 12 | getAllHtmlFiles(filePath, fileList); |
| 13 | } else if (file === "index.html") { |
| 14 | fileList.push(filePath); |
| 15 | } |
| 16 | }); |
| 17 | |
| 18 | return fileList; |
| 19 | } |
| 20 | |
| 21 | module.exports = function customLLMsTxtPlugin(context, options) { |
| 22 | return { |
| 23 | name: "custom-llms-txt", |
| 24 | async postBuild({ siteConfig, outDir }) { |
| 25 | // Group docs by their sidebar category |
| 26 | const sections = { |
| 27 | "Getting Started": [], |
| 28 | Features: [], |
| 29 | Guides: [], |
| 30 | Customization: [], |
| 31 | Customize: [], |
| 32 | Hub: [], |
| 33 | }; |
| 34 | |
| 35 | // Get all HTML files |
| 36 | const htmlFiles = getAllHtmlFiles(outDir); |
| 37 | |
| 38 | for (const htmlPath of htmlFiles) { |
| 39 | try { |
| 40 | const htmlContent = fs.readFileSync(htmlPath, "utf8"); |
| 41 | |
| 42 | // Extract title from HTML (handle both formats) |
| 43 | const titleMatch = htmlContent.match(/<title[^>]*>([^<]+)<\/title>/); |
| 44 | const title = titleMatch |
| 45 | ? titleMatch[1].replace(" | Continue", "").trim() |
| 46 | : "Untitled"; |
| 47 | |
| 48 | // Skip if title is just "Continue" (likely homepage) |
| 49 | if (title === "Continue") continue; |
| 50 | |
| 51 | // Extract description from meta tag |
| 52 | const descMatch = htmlContent.match( |
| 53 | /<meta\s+name="description"\s+content="([^"]+)"/, |
| 54 | ); |
| 55 | const description = descMatch ? descMatch[1] : ""; |
| 56 | |
| 57 | // Get relative path from build directory |
| 58 | const relativePath = path.relative(outDir, htmlPath); |
| 59 | // Convert to URL path |
| 60 | const cleanPath = relativePath |
| 61 | .replace(/index\.html$/, "") |
| 62 | .replace(/\/$/, ""); |
| 63 | const url = `https://docs.continue.dev/${cleanPath}`; |
| 64 | |
| 65 | const docInfo = { title, url, description, path: cleanPath }; |
| 66 | |
| 67 | // Determine section based on path |
| 68 | if (cleanPath.includes("getting-started")) { |
| 69 | sections["Getting Started"].push(docInfo); |
| 70 | } else if (cleanPath.includes("features")) { |
| 71 | sections["Features"].push(docInfo); |
| 72 | } else if (cleanPath.includes("guides")) { |
| 73 | sections["Guides"].push(docInfo); |
| 74 | } else if (cleanPath.includes("customization")) { |
| 75 | sections["Customization"].push(docInfo); |
| 76 | } else if (cleanPath.includes("customize")) { |
| 77 | sections["Customize"].push(docInfo); |
| 78 | } else if (cleanPath.includes("hub")) { |
| 79 | sections["Hub"].push(docInfo); |
| 80 | } else if (cleanPath === "" || cleanPath === "/") { |
| 81 | // Root page |
| 82 | sections["Getting Started"].unshift(docInfo); |
| 83 | } |
| 84 | } catch (error) { |
| 85 | console.warn(`Failed to process ${htmlPath}:`, error.message); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Generate the structured llms.txt content |
| 90 | let content = "# Continue Documentation\n\n"; |
| 91 | content += |
| 92 | "Documentation for Continue - the open-source AI code assistant for developers\n\n"; |
| 93 | |
| 94 | // Add each section |
| 95 | Object.entries(sections).forEach(([sectionName, docs]) => { |
| 96 | if (docs.length > 0) { |
| 97 | content += `## ${sectionName}\n\n`; |
| 98 | |
| 99 | // Sort docs within each section |
| 100 | docs.sort((a, b) => { |
| 101 | // Sort by path depth first (shorter paths first) |
| 102 | const depthA = (a.path.match(/\//g) || []).length; |
| 103 | const depthB = (b.path.match(/\//g) || []).length; |
| 104 | if (depthA !== depthB) return depthA - depthB; |
| 105 | |
| 106 | // Then alphabetically |
| 107 | return a.path.localeCompare(b.path); |
| 108 | }); |
| 109 | |
| 110 | docs.forEach((doc) => { |
| 111 | content += `- [${doc.title}](${doc.url})`; |
| 112 | if (doc.description) { |
| 113 | content += `: ${doc.description}`; |
| 114 | } |
| 115 | content += "\n"; |
| 116 | }); |
| 117 | content += "\n"; |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | // Write to file |
| 122 | const outputPath = path.join(outDir, "llms.txt"); |
| 123 | fs.writeFileSync(outputPath, content, "utf8"); |
| 124 | |
| 125 | console.log( |
| 126 | `Generated structured llms.txt with ${Object.values(sections).flat().length} pages`, |
| 127 | ); |
| 128 | }, |
| 129 | }; |
| 130 | }; |