-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Refactor markdown rendering for separate compilation #5804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -197,9 +197,18 @@ export default function createStreamingRenderer( | |||||||||||||||
| wrapper.append(applyTransform(activeFragment, options.transformFragment)); | ||||||||||||||||
| } else { | ||||||||||||||||
| // New block boundary in tail: commit newly-finished blocks, replace active. | ||||||||||||||||
| // We must compile the committed and active portions as separate substrings | ||||||||||||||||
| // (not filtered events) because reference links like [1] require their | ||||||||||||||||
| // definitions to be present in the same compilation unit. | ||||||||||||||||
| const newActiveOffsetInTail = tailBlocks[tailBlocks.length - 1].startOffset; | ||||||||||||||||
| const committedTailEvents = tailEvents.filter(([, token]) => token.start.offset < newActiveOffsetInTail); | ||||||||||||||||
| const committedTailHTML = compile(micromarkOptions)(committedTailEvents); | ||||||||||||||||
| const committedTailMarkdown = processedMarkdown.slice( | ||||||||||||||||
| activeBlockStartOffset, | ||||||||||||||||
| activeBlockStartOffset + newActiveOffsetInTail | ||||||||||||||||
| ); | ||||||||||||||||
| const activeTailMarkdown = processedMarkdown.slice(activeBlockStartOffset + newActiveOffsetInTail); | ||||||||||||||||
|
|
||||||||||||||||
| const committedTailHTML = compile(micromarkOptions)(parseEvents(committedTailMarkdown)); | ||||||||||||||||
| const activeTailHTML = compile(micromarkOptions)(parseEvents(activeTailMarkdown)); | ||||||||||||||||
|
Comment on lines
+208
to
+211
|
||||||||||||||||
|
|
||||||||||||||||
| activeBlockStartOffset += newActiveOffsetInTail; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -209,8 +218,7 @@ export default function createStreamingRenderer( | |||||||||||||||
| committedFragment.append(...Array.from(committedDoc.body.childNodes)); | ||||||||||||||||
| betterLinkDocumentMod(committedFragment, createDecorate(emptyDefinitions, externalLinkAlt)); | ||||||||||||||||
|
|
||||||||||||||||
| const remainingHTML = tailHTML.slice(committedTailHTML.length); | ||||||||||||||||
| const activeDoc = domParser.parseFromString(remainingHTML.trim(), 'text/html'); | ||||||||||||||||
| const activeDoc = domParser.parseFromString(activeTailHTML.trim(), 'text/html'); | ||||||||||||||||
| const activeFragment = activeDoc.createDocumentFragment(); | ||||||||||||||||
|
|
||||||||||||||||
| activeFragment.append(...Array.from(activeDoc.body.childNodes)); | ||||||||||||||||
|
|
@@ -251,14 +259,14 @@ export default function createStreamingRenderer( | |||||||||||||||
|
|
||||||||||||||||
| activeBlockStartOffset = lastBlock.startOffset; | ||||||||||||||||
|
|
||||||||||||||||
| // Compile the active (last block) portion first — it is always a single | ||||||||||||||||
| // block and therefore cheap. Then derive the committed HTML by slicing | ||||||||||||||||
| // the already-compiled full output so that inter-block whitespace | ||||||||||||||||
| // (produced by lineEnding events the compiler only flushes mid-stream) | ||||||||||||||||
| // is preserved in the committed fragment. | ||||||||||||||||
| const activeEvents = fullEvents.filter(([, token]) => token.start.offset >= lastBlock.startOffset); | ||||||||||||||||
| const activeHTML = compile(micromarkOptions)(activeEvents); | ||||||||||||||||
| const committedHTML = rawHTML.slice(0, rawHTML.length - activeHTML.length); | ||||||||||||||||
| // Compile the committed and active portions as separate substrings | ||||||||||||||||
| // (not filtered events) because reference links like [1] require their | ||||||||||||||||
| // definitions to be present in the same compilation unit. | ||||||||||||||||
| const committedMarkdown = processedMarkdown.slice(0, lastBlock.startOffset); | ||||||||||||||||
| const activeMarkdown = processedMarkdown.slice(lastBlock.startOffset); | ||||||||||||||||
|
Comment on lines
+262
to
+266
|
||||||||||||||||
|
|
||||||||||||||||
| const committedHTML = compile(micromarkOptions)(parseEvents(committedMarkdown)); | ||||||||||||||||
| const activeHTML = compile(micromarkOptions)(parseEvents(activeMarkdown)); | ||||||||||||||||
|
Comment on lines
+265
to
+269
|
||||||||||||||||
| const committedHTML = compile(micromarkOptions)(parseEvents(committedMarkdown)); | |
| const activeHTML = compile(micromarkOptions)(parseEvents(activeMarkdown)); | |
| const compiler = compile(micromarkOptions); | |
| const committedHTML = compiler(parseEvents(committedMarkdown)); | |
| const activeHTML = compiler(parseEvents(activeMarkdown)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code now duplicates the same conceptual operation (split markdown at an offset → compile two parts) in multiple branches (tail split vs. last-block split), which increases the risk of subtle inconsistencies (especially once reference-definition handling is corrected). Consider factoring this into a small helper that (a) computes the two substrings and (b) enforces a single consistent strategy for reference definitions/whitespace preservation, so future fixes don’t need to be applied in two places.