Detects table boundaries to avoid splitting tables across chunks.
(content: string)
| 253 | |
| 254 | /** Detects table boundaries to avoid splitting tables across chunks. */ |
| 255 | private detectTableBoundaries(content: string): { start: number; end: number }[] { |
| 256 | const tables: { start: number; end: number }[] = [] |
| 257 | const lines = content.split('\n') |
| 258 | |
| 259 | let inTable = false |
| 260 | let inCodeBlock = false |
| 261 | let tableStart = -1 |
| 262 | |
| 263 | for (let i = 0; i < lines.length; i++) { |
| 264 | const line = lines[i].trim() |
| 265 | |
| 266 | if (line.startsWith('```')) { |
| 267 | inCodeBlock = !inCodeBlock |
| 268 | continue |
| 269 | } |
| 270 | |
| 271 | if (inCodeBlock) continue |
| 272 | |
| 273 | if (line.includes('|') && line.split('|').length >= 3 && !inTable) { |
| 274 | const nextLine = lines[i + 1]?.trim() |
| 275 | if (nextLine?.includes('|') && nextLine.includes('-')) { |
| 276 | inTable = true |
| 277 | tableStart = i |
| 278 | } |
| 279 | } else if (inTable && (!line.includes('|') || line === '' || line.startsWith('#'))) { |
| 280 | tables.push({ |
| 281 | start: this.getCharacterPosition(lines, tableStart), |
| 282 | end: this.getCharacterPosition(lines, i - 1) + (lines[i - 1]?.length ?? 0), |
| 283 | }) |
| 284 | inTable = false |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (inTable && tableStart >= 0) { |
| 289 | tables.push({ |
| 290 | start: this.getCharacterPosition(lines, tableStart), |
| 291 | end: content.length, |
| 292 | }) |
| 293 | } |
| 294 | |
| 295 | return tables |
| 296 | } |
| 297 | |
| 298 | private getCharacterPosition(lines: string[], lineIndex: number): number { |
| 299 | return lines.slice(0, lineIndex).reduce((acc, line) => acc + line.length + 1, 0) |
no test coverage detected