( url: string, article: ArticleComponent, subpath: string, max_chunk_size: number, )
| 24 | }; |
| 25 | |
| 26 | function breakdownArticleComponent( |
| 27 | url: string, |
| 28 | article: ArticleComponent, |
| 29 | subpath: string, |
| 30 | max_chunk_size: number, |
| 31 | ): Chunk[] { |
| 32 | const chunks: Chunk[] = []; |
| 33 | const lines = article.body.split("\n"); |
| 34 | let startLine = 0; |
| 35 | let endLine = 0; |
| 36 | let content = ""; |
| 37 | let index = 0; |
| 38 | |
| 39 | const fullUrl = new URL( |
| 40 | `${subpath}#${cleanFragment(article.title)}`, |
| 41 | url, |
| 42 | ).toString(); |
| 43 | |
| 44 | const createChunk = ( |
| 45 | chunkContent: string, |
| 46 | chunkStartLine: number, |
| 47 | chunkEndLine: number, |
| 48 | ) => { |
| 49 | chunks.push({ |
| 50 | content: chunkContent.trim(), |
| 51 | startLine: chunkStartLine, |
| 52 | endLine: chunkEndLine, |
| 53 | otherMetadata: { |
| 54 | title: cleanHeader(article.title), |
| 55 | }, |
| 56 | index: index++, |
| 57 | filepath: fullUrl, |
| 58 | digest: fullUrl, |
| 59 | }); |
| 60 | }; |
| 61 | |
| 62 | for (let i = 0; i < lines.length; i++) { |
| 63 | const line = lines[i]; |
| 64 | |
| 65 | // Handle oversized lines by splitting them |
| 66 | if (line.length > max_chunk_size) { |
| 67 | // First push any accumulated content |
| 68 | if (content.trim().length > 0) { |
| 69 | createChunk(content, startLine, endLine); |
| 70 | content = ""; |
| 71 | } |
| 72 | |
| 73 | // Split the long line into chunks |
| 74 | let remainingLine = line; |
| 75 | let subLineStart = i; |
| 76 | while (remainingLine.length > 0) { |
| 77 | const chunk = remainingLine.slice(0, max_chunk_size); |
| 78 | createChunk(chunk, subLineStart, i); |
| 79 | remainingLine = remainingLine.slice(max_chunk_size); |
| 80 | } |
| 81 | startLine = i + 1; |
| 82 | continue; |
| 83 | } |
no test coverage detected