@@ -198,24 +198,19 @@ export default function createStreamingRenderer(
198198 } else {
199199 // New block boundary in tail: commit newly-finished blocks, replace active.
200200 const newActiveOffsetInTail = tailBlocks [ tailBlocks . length - 1 ] . startOffset ;
201-
202- activeBlockStartOffset += newActiveOffsetInTail ;
203-
204- // Split at the micromark-event level so that blocks which compile to
205- // multiple sibling DOM nodes (e.g. htmlFlow) are kept whole.
206201 const committedTailEvents = tailEvents . filter ( ( [ , token ] ) => token . start . offset < newActiveOffsetInTail ) ;
207- const activeTailEvents = tailEvents . filter ( ( [ , token ] ) => token . start . offset >= newActiveOffsetInTail ) ;
208-
209202 const committedTailHTML = compile ( micromarkOptions ) ( committedTailEvents ) ;
210- const activeTailHTML = compile ( micromarkOptions ) ( activeTailEvents ) ;
203+
204+ activeBlockStartOffset += newActiveOffsetInTail ;
211205
212206 const committedDoc = domParser . parseFromString ( committedTailHTML , 'text/html' ) ;
213207 const committedFragment = committedDoc . createDocumentFragment ( ) ;
214208
215209 committedFragment . append ( ...Array . from ( committedDoc . body . childNodes ) ) ;
216210 betterLinkDocumentMod ( committedFragment , createDecorate ( emptyDefinitions , externalLinkAlt ) ) ;
217211
218- const activeDoc = domParser . parseFromString ( activeTailHTML , 'text/html' ) ;
212+ const remainingHTML = tailHTML . slice ( committedTailHTML . length ) ;
213+ const activeDoc = domParser . parseFromString ( remainingHTML , 'text/html' ) ;
219214 const activeFragment = activeDoc . createDocumentFragment ( ) ;
220215
221216 activeFragment . append ( ...Array . from ( activeDoc . body . childNodes ) ) ;
@@ -248,35 +243,27 @@ export default function createStreamingRenderer(
248243
249244 // Full reparse path.
250245 const fullEvents = parseEvents ( processedMarkdown ) ;
251- const blocks = findTopLevelBlocks ( fullEvents ) ;
252-
253- if ( blocks . length >= 2 ) {
254- const lastBlock = blocks [ blocks . length - 1 ] ;
255-
256- activeBlockStartOffset = lastBlock . startOffset ;
257-
258- // Split at the micromark-event level so that blocks which compile to
259- // multiple sibling DOM nodes (e.g. htmlFlow) are kept whole.
260- const committedEvents = fullEvents . filter ( ( [ , token ] ) => token . start . offset < lastBlock . startOffset ) ;
261- const activeEvents = fullEvents . filter ( ( [ , token ] ) => token . start . offset >= lastBlock . startOffset ) ;
246+ const rawHTML = compile ( micromarkOptions ) ( fullEvents ) ;
247+ const parsedDocument = domParser . parseFromString ( rawHTML , 'text/html' ) ;
248+ const fragment = parsedDocument . createDocumentFragment ( ) ;
262249
263- const committedHTML = compile ( micromarkOptions ) ( committedEvents ) ;
264- const activeHTML = compile ( micromarkOptions ) ( activeEvents ) ;
250+ fragment . append ( ...Array . from ( parsedDocument . body . childNodes ) ) ;
265251
266- const committedDoc = domParser . parseFromString ( committedHTML , 'text/html' ) ;
267- const committedFragment = committedDoc . createDocumentFragment ( ) ;
252+ const blocks = findTopLevelBlocks ( fullEvents ) ;
268253
269- committedFragment . append ( ...Array . from ( committedDoc . body . childNodes ) ) ;
254+ if ( blocks . length >= 2 ) {
255+ activeBlockStartOffset = blocks [ blocks . length - 1 ] . startOffset ;
270256
271- const activeDoc = domParser . parseFromString ( activeHTML , 'text/html' ) ;
272- const activeFragment = activeDoc . createDocumentFragment ( ) ;
257+ const range = document . createRange ( ) ;
273258
274- activeFragment . append ( ...Array . from ( activeDoc . body . childNodes ) ) ;
259+ range . setStartBefore ( fragment . firstChild ! ) ;
260+ range . setEndBefore ( fragment . lastElementChild ! ) ;
275261
262+ const committedFragment = range . extractContents ( ) ;
276263 const decorate = createDecorate ( emptyDefinitions , externalLinkAlt ) ;
277264
278265 betterLinkDocumentMod ( committedFragment , decorate ) ;
279- betterLinkDocumentMod ( activeFragment , decorate ) ;
266+ betterLinkDocumentMod ( fragment , decorate ) ;
280267
281268 const wrapper = ensureWrapper ( options . container , options . containerClassName ) ;
282269
@@ -285,7 +272,7 @@ export default function createStreamingRenderer(
285272 wrapper . replaceChildren (
286273 applyTransform ( committedFragment , options . transformFragment ) ,
287274 activeSentinel ,
288- applyTransform ( activeFragment , options . transformFragment )
275+ applyTransform ( fragment , options . transformFragment )
289276 ) ;
290277
291278 return ;
@@ -295,12 +282,6 @@ export default function createStreamingRenderer(
295282 activeBlockStartOffset = 0 ;
296283 activeSentinel = null ;
297284
298- const rawHTML = compile ( micromarkOptions ) ( fullEvents ) ;
299- const parsedDocument = domParser . parseFromString ( rawHTML , 'text/html' ) ;
300- const fragment = parsedDocument . createDocumentFragment ( ) ;
301-
302- fragment . append ( ...Array . from ( parsedDocument . body . childNodes ) ) ;
303-
304285 betterLinkDocumentMod ( fragment , createDecorate ( emptyDefinitions , externalLinkAlt ) ) ;
305286
306287 const wrapper = ensureWrapper ( options . container , options . containerClassName ) ;
0 commit comments