(text: string, fileName?: string)
| 267 | * @returns Array of text chunks with preserved structure |
| 268 | */ |
| 269 | export function chunkReadme(text: string, fileName?: string): string[] { |
| 270 | // Check if this appears to be a README format |
| 271 | const hasMultipleHeadings = (text.match(/^#+\s+.+/gm) || []).length > 1; |
| 272 | const hasCodeBlocks = text.includes("```"); |
| 273 | const isReadmeLike = |
| 274 | hasMultipleHeadings && |
| 275 | (hasCodeBlocks || text.includes("* ") || text.includes("- ")); |
| 276 | |
| 277 | // If not README-like, use the regular chunking |
| 278 | if (!isReadmeLike) { |
| 279 | return chunkText(text); |
| 280 | } |
| 281 | |
| 282 | // Check if this is a special case file (like llms.txt) that needs list-item level chunking |
| 283 | const isSpecialListFile = fileName?.toLowerCase().includes("llms.txt"); |
| 284 | |
| 285 | // Track headers and their content |
| 286 | interface HeaderSection { |
| 287 | level: number; |
| 288 | title: string; |
| 289 | content: string; |
| 290 | lineIndex: number; |
| 291 | } |
| 292 | |
| 293 | const sections: HeaderSection[] = []; |
| 294 | let currentSection: HeaderSection | null = null; |
| 295 | let mainHeaderContent = ""; |
| 296 | let mainTitle = ""; |
| 297 | |
| 298 | // Helper function to detect badge lines (markdown image links with badge URLs) |
| 299 | function isBadgeLine(line: string): boolean { |
| 300 | // Detect badge-specific patterns (shield.io, badge URLs, image links in a row) |
| 301 | return ( |
| 302 | /!\[.*\]\(.*badge.*\)/.test(line) || |
| 303 | /!\[.*\]\(.*shield\.io.*\)/.test(line) || |
| 304 | (/\[!\[.*\]\(.*\)\]\(.*\)/.test(line) && |
| 305 | (line.includes("badge") || line.includes("shield"))) || |
| 306 | /img\.shields\.io/.test(line) || |
| 307 | (line.includes("<img") && |
| 308 | (line.includes("badge") || line.includes("shield"))) |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | // First pass: Extract headers and their content |
| 313 | const lines = text.split("\n"); |
| 314 | let inBadgeSection = false; |
| 315 | let badgeSectionEndLine = 0; |
| 316 | let skipToLine = -1; |
| 317 | |
| 318 | // Detect the initial badge/logo section which often appears at the start of READMEs |
| 319 | for (let i = 0; i < Math.min(20, lines.length); i++) { |
| 320 | if ( |
| 321 | (lines[i].includes('<p align="center">') || |
| 322 | lines[i].includes('align="center"') || |
| 323 | lines[i].includes('<div align="center">')) && |
| 324 | i + 5 < lines.length |
| 325 | ) { |
| 326 | // Check if next few lines contain images, badges, or links |
no test coverage detected