|
| 1 | +/** |
| 2 | + * Processes InstUI JSON metadata to generate a file |
| 3 | + * formatted for Large Language Models (LLMs) and AI agent consumption |
| 4 | + * - Contains a catalog of links pointing to ai-accessible markdown files about InstUI components and guides |
| 5 | + * - Contains a summary of each component/guide next to the links |
| 6 | +*/ |
| 7 | + |
| 8 | +import { writeFileSync, readFileSync } from 'fs' |
| 9 | + |
| 10 | +interface SectionData { |
| 11 | + docs?: string[] |
| 12 | + sections?: string[] |
| 13 | + title?: string |
| 14 | +} |
| 15 | + |
| 16 | +interface GenerateLLMSOptions { |
| 17 | + summariesFilePath?: string |
| 18 | + baseUrl?: string |
| 19 | + outputFilePath?: string |
| 20 | +} |
| 21 | + |
| 22 | +function generateAIAccessibleLlmsFile( |
| 23 | + sourceFilePath: string, |
| 24 | + options: GenerateLLMSOptions = {} |
| 25 | +): string { |
| 26 | + const { summariesFilePath, baseUrl = '', outputFilePath } = options |
| 27 | + |
| 28 | + const fileContent = readFileSync(sourceFilePath, 'utf-8') |
| 29 | + const jsonData = JSON.parse(fileContent) |
| 30 | + const { sections, library } = jsonData |
| 31 | + |
| 32 | + const version = library?.version |
| 33 | + |
| 34 | + let summaries: Record<string, string> = {} |
| 35 | + // Generate summaries object from the summaries file for later lookup |
| 36 | + if (summariesFilePath) { |
| 37 | + try { |
| 38 | + const summariesContent = readFileSync(summariesFilePath, 'utf-8') |
| 39 | + const data = JSON.parse(summariesContent) |
| 40 | + |
| 41 | + data.forEach((item: { title: string; summary?: string }) => { |
| 42 | + summaries[item.title] = item.summary || '' |
| 43 | + }) |
| 44 | + } catch (error) { |
| 45 | + console.warn('Could not load summaries file:', error) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let LlmsMarkdownContent = `# Instructure UI (InstUI) - React Component Library\n\n- version ${version} \n\n` |
| 50 | + LlmsMarkdownContent += `- Instructure UI (InstUI) is a comprehensive React component library.\n\n` |
| 51 | + |
| 52 | + // Add main Documentation section |
| 53 | + LlmsMarkdownContent += `## Documentation\n\n` |
| 54 | + |
| 55 | + // Add User Guides section |
| 56 | + LlmsMarkdownContent += `### User Guides\n` |
| 57 | + |
| 58 | + Object.entries(sections as Record<string, SectionData>).forEach( |
| 59 | + ([sectionKey, sectionData]) => { |
| 60 | + const sectionTitle = sectionData.title || sectionKey |
| 61 | + |
| 62 | + // Only process these specific sections |
| 63 | + if (!['Getting Started', 'Guides', 'Patterns'].includes(sectionTitle)) { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + LlmsMarkdownContent += `#### ${sectionTitle}\n\n` |
| 68 | + |
| 69 | + if (sectionData.docs && sectionData.docs.length > 0) { |
| 70 | + const uniqueDocs = [...new Set(sectionData.docs)] |
| 71 | + uniqueDocs.forEach((doc) => { |
| 72 | + // Skip unnecessary documents |
| 73 | + if ( |
| 74 | + doc === 'CODE_OF_CONDUCT' || |
| 75 | + doc === 'LICENSE' || |
| 76 | + doc.includes('upgrade-guide') |
| 77 | + ) { |
| 78 | + return |
| 79 | + } |
| 80 | + const displayName = doc |
| 81 | + .replace(/([A-Z])/g, ' $1') |
| 82 | + .replace(/-/g, ' ') |
| 83 | + .trim() |
| 84 | + .toLowerCase() |
| 85 | + .replace(/^\w/, (char) => char.toUpperCase()) |
| 86 | + |
| 87 | + const summary = summaries[doc] |
| 88 | + LlmsMarkdownContent += `- [${displayName}](${baseUrl}${doc}.md)${ |
| 89 | + summary ? `: ${summary}` : '' |
| 90 | + }\n` |
| 91 | + }) |
| 92 | + } |
| 93 | + LlmsMarkdownContent += '\n' |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + // Add Components section under Documentation |
| 98 | + LlmsMarkdownContent += `### Components\n\n` |
| 99 | + |
| 100 | + const componentsSection = sections['components'] as SectionData | undefined |
| 101 | + if (componentsSection) { |
| 102 | + const allComponents = [...new Set(componentsSection.docs || [])] |
| 103 | + allComponents.forEach((component) => { |
| 104 | + const summary = summaries[component] |
| 105 | + LlmsMarkdownContent += `- [${component}](${baseUrl}${component}.md)${ |
| 106 | + summary ? `: ${summary}` : '' |
| 107 | + }\n` |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + // Process component subsections like Components/AI Components and Components/Utilities, skip deprecated |
| 112 | + if (componentsSection?.sections && componentsSection.sections.length > 0) { |
| 113 | + const subsections = componentsSection.sections |
| 114 | + subsections.forEach((subsectionPath: string) => { |
| 115 | + if (subsectionPath.toLowerCase().includes('deprecated')) return |
| 116 | + |
| 117 | + const subsection = sections[subsectionPath] as SectionData | undefined |
| 118 | + if (subsection && subsection.docs && subsection.docs.length > 0) { |
| 119 | + const subsectionTitle = subsection.title |
| 120 | + LlmsMarkdownContent += `\n#### ${subsectionTitle}\n\n` |
| 121 | + // Avoid duplicates |
| 122 | + const uniqueSubDocs = [...new Set(subsection.docs)] |
| 123 | + uniqueSubDocs.forEach((doc) => { |
| 124 | + const summary = summaries[doc] |
| 125 | + LlmsMarkdownContent += `- [${doc}](${baseUrl}${doc}.md)${ |
| 126 | + summary ? `: ${summary}` : '' |
| 127 | + }\n` |
| 128 | + }) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | + |
| 133 | + if (outputFilePath) { |
| 134 | + writeFileSync(outputFilePath, LlmsMarkdownContent) |
| 135 | + } |
| 136 | + |
| 137 | + return LlmsMarkdownContent |
| 138 | +} |
| 139 | + |
| 140 | +export { generateAIAccessibleLlmsFile } |
0 commit comments