MCPcopy Index your code
hub / github.com/simstudioai/sim / detectTableBoundaries

Method detectTableBoundaries

apps/sim/lib/chunkers/docs-chunker.ts:255–296  ·  view source on GitHub ↗

Detects table boundaries to avoid splitting tables across chunks.

(content: string)

Source from the content-addressed store, hash-verified

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)

Callers 1

splitContentMethod · 0.95

Calls 2

getCharacterPositionMethod · 0.95
pushMethod · 0.45

Tested by

no test coverage detected