(text, chunkSize)
| 259 | } |
| 260 | |
| 261 | function splitByChunks(text, chunkSize) { |
| 262 | chunkSize -= 20 |
| 263 | const chunks = [] |
| 264 | for (let i = 0; i < text.length; i += chunkSize) { |
| 265 | chunks.push(text.slice(i, i + chunkSize)) |
| 266 | } |
| 267 | |
| 268 | const regex = /<\s*\w+(?:\s+\w+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+)))*\s*$/ |
| 269 | |
| 270 | // append tag to chunk if it was split out |
| 271 | for (const index in chunks) { |
| 272 | const nextIndex = parseInt(index, 10) + 1 |
| 273 | if (!chunks[nextIndex]) break |
| 274 | |
| 275 | const currentChunk = chunks[index] |
| 276 | const nextChunk = chunks[nextIndex] |
| 277 | |
| 278 | const lastTag = currentChunk.match(regex) |
| 279 | if (lastTag) { |
| 280 | chunks[nextIndex] = lastTag[0] + nextChunk |
| 281 | } |
| 282 | |
| 283 | const path = buildPath(parse(currentChunk)) |
| 284 | if (path) { |
| 285 | chunks[nextIndex] = path + chunks[nextIndex] |
| 286 | } |
| 287 | |
| 288 | if (chunks[nextIndex].includes('<html')) continue |
| 289 | chunks[nextIndex] = `<html><body>${chunks[nextIndex]}</body></html>` |
| 290 | } |
| 291 | |
| 292 | return chunks.map(chunk => chunk.trim()) |
| 293 | } |
| 294 | |
| 295 | function simplifyHtmlElement(html, maxLength = 300) { |
| 296 | try { |
no test coverage detected