| 1 | import { Readability } from "@mozilla/readability"; |
| 2 | import { JSDOM } from "jsdom"; |
| 3 | |
| 4 | import { Chunk } from "../../"; |
| 5 | import { cleanFragment, cleanHeader, markdownChunker } from "../chunk/markdown"; |
| 6 | |
| 7 | import { type PageData } from "./crawlers/DocsCrawler"; |
| 8 | |
| 9 | export type ArticleComponent = { |
| 10 | title: string; |
| 11 | body: string; |
| 12 | }; |
| 13 | |
| 14 | export type Article = { |
| 15 | url: string; |
| 16 | subpath: string; |
| 17 | title: string; |
| 18 | article_components: ArticleComponent[]; |
| 19 | }; |
| 20 | |
| 21 | export type ArticleWithChunks = { |
| 22 | article: Article; |
| 23 | chunks: Chunk[]; |
| 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 | } |
| 84 | |
| 85 | // Normal line handling |
| 86 | if (content.length + line.length + 1 <= max_chunk_size) { |
| 87 | content += `${line}\n`; |
| 88 | endLine = i; |
| 89 | } else { |
| 90 | if (content.trim().length > 0) { |
| 91 | createChunk(content, startLine, endLine); |
| 92 | } |
| 93 | content = `${line}\n`; |
| 94 | startLine = i; |
| 95 | endLine = i; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Push the last chunk |
| 100 | if (content.trim().length > 0) { |
| 101 | createChunk(content, startLine, endLine); |
| 102 | } |
| 103 | |
| 104 | return chunks.filter((c) => c.content.trim().length > 20); |
| 105 | } |
| 106 | |
| 107 | function chunkArticle(article: Article, maxChunkSize: number): Chunk[] { |
| 108 | const chunks: Chunk[] = []; |
| 109 | |
| 110 | for (const component of article.article_components) { |
| 111 | const articleChunks = breakdownArticleComponent( |
| 112 | article.url, |
| 113 | component, |
| 114 | article.subpath, |
| 115 | maxChunkSize, |
| 116 | ); |
| 117 | chunks.push(...articleChunks); |
| 118 | } |
| 119 | |
| 120 | return chunks; |
| 121 | } |
| 122 | |
| 123 | export async function htmlPageToArticleWithChunks( |
| 124 | page: PageData, |
| 125 | maxChunkSize: number, |
| 126 | ): Promise<ArticleWithChunks | undefined> { |
| 127 | try { |
| 128 | const html = page.content; |
| 129 | const subpath = page.path; |
| 130 | const url = page.url; |
| 131 | |
| 132 | const dom = new JSDOM(html); |
| 133 | const reader = new Readability(dom.window.document); |
| 134 | const readability = reader.parse(); |
| 135 | |
| 136 | if (!readability) { |
| 137 | console.error("Docs indexing: Error getting readability for URL", url); |
| 138 | |
| 139 | return undefined; |
| 140 | } |
| 141 | |
| 142 | const title = readability.title || subpath; |
| 143 | |
| 144 | const titles = Array.from(dom.window.document.querySelectorAll("h2")); |
| 145 | |
| 146 | const article_components = |
| 147 | titles.length > 0 |
| 148 | ? titles.map((titleElement) => { |
| 149 | const title = titleElement.textContent || ""; |
| 150 | let body = ""; |
| 151 | let nextSibling = titleElement.nextElementSibling; |
| 152 | |
| 153 | while (nextSibling && nextSibling.tagName !== "H2") { |
| 154 | body += nextSibling.textContent || ""; |
| 155 | nextSibling = nextSibling.nextElementSibling; |
| 156 | } |
| 157 | |
| 158 | return { title, body }; |
| 159 | }) |
| 160 | : [ |
| 161 | { |
| 162 | title: title, |
| 163 | body: readability.textContent, |
| 164 | }, |
| 165 | ]; |
| 166 | |
| 167 | const article = { |
| 168 | url, |
| 169 | subpath, |
| 170 | title: title, |
| 171 | article_components, |
| 172 | }; |
| 173 | return { |
| 174 | article, |
| 175 | chunks: chunkArticle(article, maxChunkSize), |
| 176 | }; |
| 177 | } catch (err) { |
| 178 | console.error("Error converting URL to article components", err); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | export async function markdownPageToArticleWithChunks( |
| 183 | page: PageData, |
| 184 | maxChunkSize: number, |
| 185 | ): Promise<ArticleWithChunks | undefined> { |
| 186 | try { |
| 187 | let index = 0; |
| 188 | const chunks: Chunk[] = []; |
| 189 | const chunker = markdownChunker(page.content, maxChunkSize, 1); |
| 190 | for await (const chunk of chunker) { |
| 191 | const fullUrl = new URL(page.url); |
| 192 | fullUrl.hash = `#${cleanFragment(chunk.otherMetadata?.title)}`; |
| 193 | |
| 194 | chunks.push({ |
| 195 | ...chunk, |
| 196 | index, |
| 197 | filepath: fullUrl.toString(), |
| 198 | digest: fullUrl.toString(), |
| 199 | }); |
| 200 | index++; |
| 201 | } |
| 202 | return { |
| 203 | article: { |
| 204 | url: page.url, |
| 205 | subpath: page.path, |
| 206 | article_components: [], // TODO: markdown chunker just skips this for now since not used outside, only for html chunker |
| 207 | title: chunks[0]?.otherMetadata?.title || page.path, |
| 208 | }, |
| 209 | chunks, |
| 210 | }; |
| 211 | } catch (err) { |
| 212 | console.error(`Docs indexing: failed to chunk markdown from ${page.url}`); |
| 213 | } |
| 214 | } |