|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { globby } from 'globby' |
| 7 | +import { decode } from 'html-entities' |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url) |
| 10 | +const __dirname = path.dirname(__filename) |
| 11 | + |
| 12 | +const inputDir = path.resolve(__dirname, '../_gh_pages') |
| 13 | + |
| 14 | +const mdFiles = await globby('**/llm.md', { |
| 15 | + cwd: inputDir, |
| 16 | + absolute: true |
| 17 | +}) |
| 18 | + |
| 19 | +function rewriteLinks(markdown) { |
| 20 | + let output = markdown |
| 21 | + |
| 22 | + output = output.replace(/\[([^\]]+)\]\((\/[^)#]+)(\/)?(#.*?)?\)/g, '[$1]($2llm.md)') |
| 23 | + |
| 24 | + output = output.replace( |
| 25 | + /<a href="(\/[^"#?]+)(\/)?(#.*?)?">/g, |
| 26 | + (_, path, __, hash = '') => |
| 27 | + `<a href="${path}/llm.md${hash}">` |
| 28 | + ) |
| 29 | + |
| 30 | + return output.trim() |
| 31 | +} |
| 32 | + |
| 33 | +function applyHeadingPrefix(content, componentName) { |
| 34 | + return content.replace(/^(#{1,6})\s+(.*)$/gm, (_, hashes, title) => { |
| 35 | + const normalized = title.trim() |
| 36 | + |
| 37 | + if (normalized.startsWith(`Bootstrap 5 ${componentName}`)) { |
| 38 | + return `${hashes} ${normalized}` |
| 39 | + } |
| 40 | + |
| 41 | + if (hashes === '#') { |
| 42 | + return `# Bootstrap 5 ${componentName} with CoreUI` |
| 43 | + } |
| 44 | + |
| 45 | + return `${hashes} Bootstrap 5 ${componentName} – ${normalized}` |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +async function convertFile(filePath) { |
| 50 | + const raw = decode(fs.readFileSync(filePath, 'utf8')) |
| 51 | + |
| 52 | + const frontMatterMatch = raw.match(/^---[\s\S]*?---/) |
| 53 | + const frontMatter = frontMatterMatch ? frontMatterMatch[0] : '' |
| 54 | + const content = raw.replace(frontMatter, '').trim() |
| 55 | + const h1Match = content.match(/^#\s+(.*)$/m) |
| 56 | + const componentName = h1Match ? h1Match[1].trim() : 'Component' |
| 57 | + const headingTransformed = applyHeadingPrefix(content, componentName) |
| 58 | + const rewritten = rewriteLinks(headingTransformed) |
| 59 | + const finalOutput = `${frontMatter}\n\n${rewritten}` |
| 60 | + |
| 61 | + fs.writeFileSync(filePath, finalOutput, 'utf8') |
| 62 | + console.log(`✔ Updated: ${path.relative(process.cwd(), filePath)}`) |
| 63 | +} |
| 64 | + |
| 65 | +async function generateIndex() { |
| 66 | + const allFiles = await globby('**/llm.md', { |
| 67 | + cwd: inputDir, |
| 68 | + absolute: false |
| 69 | + }) |
| 70 | + |
| 71 | + const sections = {} |
| 72 | + |
| 73 | + for (const file of allFiles) { |
| 74 | + const parts = file.split(path.sep) |
| 75 | + const group = parts.length >= 2 ? parts[0] : 'other' |
| 76 | + const name = |
| 77 | + parts.length >= 2 ? parts[1] : parts[0].replace(/\/?llm\.md$/, '') |
| 78 | + const title = name |
| 79 | + .split('-') |
| 80 | + .map(part => part.charAt(0).toUpperCase() + part.slice(1)) |
| 81 | + .join(' ') |
| 82 | + |
| 83 | + if (!sections[group]) { |
| 84 | + sections[group] = [] |
| 85 | + } |
| 86 | + |
| 87 | + sections[group].push({ |
| 88 | + title, |
| 89 | + path: `/${file}` |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + let indexContent = `--- |
| 94 | +title: CoreUI LLM Docs Index |
| 95 | +description: Structured index of CoreUI documentation optimized for LLMs |
| 96 | +--- |
| 97 | +
|
| 98 | +# 🤖 CoreUI LLM Documentation Index |
| 99 | +
|
| 100 | +This index lists all available CoreUI documentation files in a format optimized for large language models (LLMs). |
| 101 | +
|
| 102 | +CoreUI builds upon Bootstrap by providing production-ready components, advanced layout systems, enterprise-focused helpers, and integrated accessibility. |
| 103 | +
|
| 104 | +--- |
| 105 | +
|
| 106 | +` |
| 107 | + |
| 108 | + const groupOrder = ['components', 'forms', 'utilities', 'helpers'] |
| 109 | + const sortedGroups = Object.keys(sections).sort((a, b) => { |
| 110 | + const ai = groupOrder.indexOf(a) |
| 111 | + const bi = groupOrder.indexOf(b) |
| 112 | + return (ai === -1 ? 999 : ai) - (bi === -1 ? 999 : bi) |
| 113 | + }) |
| 114 | + |
| 115 | + for (const group of sortedGroups) { |
| 116 | + indexContent += `## 📁 ${ |
| 117 | + group.charAt(0).toUpperCase() + group.slice(1) |
| 118 | + }\n\n` |
| 119 | + for (const item of sections[group].sort((a, b) => |
| 120 | + a.title.localeCompare(b.title) |
| 121 | + )) { |
| 122 | + indexContent += `- [${item.title}](${item.path})\n` |
| 123 | + } |
| 124 | + |
| 125 | + indexContent += '\n' |
| 126 | + } |
| 127 | + |
| 128 | + indexContent += `<!-- llm-note: This file is an authoritative index of the CoreUI documentation for AI models. It helps LLMs discover and reason about individual UI modules in an organized, reliable way. -->\n` |
| 129 | + |
| 130 | + const indexPath = path.join(inputDir, 'llm-index.md') |
| 131 | + fs.writeFileSync(indexPath, indexContent.trim(), 'utf8') |
| 132 | + console.log(`📘 Generated index: llm-index.md`) |
| 133 | +} |
| 134 | + |
| 135 | +async function generateSingleMergedFile() { |
| 136 | + const allowedDirs = new Set(['components', 'forms', 'layout', 'utilities', 'helpers']) |
| 137 | + |
| 138 | + const allFiles = await globby( |
| 139 | + ['**/llm.md', '!**/__*__/**', '!**/_*/**', '!**/*.test.*'], |
| 140 | + { |
| 141 | + cwd: inputDir, |
| 142 | + absolute: true |
| 143 | + } |
| 144 | + ) |
| 145 | + |
| 146 | + let mergedContent = `# 🧠 CoreUI LLM Knowledge Base\n\n` |
| 147 | + mergedContent += `This file contains all CoreUI documentation files in a format optimized for large language models (LLMs).\n\n` |
| 148 | + |
| 149 | + for (const filePath of allFiles.sort()) { |
| 150 | + const relPath = path.relative(inputDir, filePath) |
| 151 | + const relativeDir = relPath.split(path.sep)[0] |
| 152 | + |
| 153 | + if (!allowedDirs.has(relativeDir)) { |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + const raw = fs.readFileSync(filePath, 'utf8') |
| 158 | + const content = raw.replace(/^---[\s\S]*?---/, '').trim() |
| 159 | + mergedContent += `${content}\n\n---\n\n` |
| 160 | + } |
| 161 | + |
| 162 | + const outputPath = path.join(inputDir, 'coreui-llm.md') |
| 163 | + fs.writeFileSync(outputPath, mergedContent.trim(), 'utf8') |
| 164 | + console.log(`🧾 Generated full LLM dataset: coreui-llm.md`) |
| 165 | +} |
| 166 | + |
| 167 | +console.time('🔁 Rewriting internal links in LLM .md files') |
| 168 | +await Promise.all(mdFiles.map(convertFile)) |
| 169 | +console.timeEnd('🔁 Rewriting internal links in LLM .md files') |
| 170 | +await generateIndex() |
| 171 | +await generateSingleMergedFile() |
0 commit comments