MCPcopy Create free account
hub / github.com/Alphanimble/htmlstream / splitHtmlIntoBlocks

Function splitHtmlIntoBlocks

src/rehtml/split-blocks.ts:14–63  ·  view source on GitHub ↗
(html: string)

Source from the content-addressed store, hash-verified

12 * Stable blocks never re-render once committed — critical for streaming perf.
13 */
14export function splitHtmlIntoBlocks(html: string): SplitBlocksResult {
15 if (!html) {
16 return { stable: [], live: "" };
17 }
18
19 const stable: string[] = [];
20 let last = 0;
21 let blockDepth = 0;
22 let i = 0;
23
24 while (i < html.length) {
25 if (html[i] === "<") {
26 const slice = html.slice(i);
27 const tagMatch = slice.match(TAG_PATTERN);
28 if (!tagMatch) {
29 i++;
30 continue;
31 }
32
33 const tagName = tagMatch[1]!.toLowerCase();
34 const isClose = slice[1] === "/";
35 const isSelfClose = Boolean(tagMatch[3]) || VOID_TAGS.has(tagName);
36
37 if (BLOCK_TAGS.has(tagName)) {
38 if (isClose) {
39 blockDepth = Math.max(0, blockDepth - 1);
40 if (blockDepth === 0) {
41 const chunk = html.slice(last, i + tagMatch[0].length);
42 if (chunk.trim()) {
43 stable.push(chunk);
44 }
45 last = i + tagMatch[0].length;
46 }
47 } else if (!isSelfClose) {
48 blockDepth++;
49 }
50 }
51
52 i += tagMatch[0].length;
53 continue;
54 }
55
56 i++;
57 }
58
59 return {
60 stable,
61 live: html.slice(last),
62 };
63}

Callers 2

rehtmlFunction · 0.90
rehtml.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected