Repository navigation
refactor(markdown): SourceRange as an object, not a tuple - #5366
Merged
Merged
Conversation
`BlockNode.range` becomes `{start, end}` instead of `[start, end]`. Same two
numbers, same meaning; `source.slice(range.start, range.end)` replaces
`source.slice(...range)`.
A tuple is closed: the only way to say where a block is in line numbers as
well as offsets would be a second field with a second name, and `BlockNode`
would carry two spellings of one concept forever. An object takes optional
`startLine`/`endLine` additively, whenever a consumer needs them. Nothing here
adds them — this is the shape only.
`sourceRanges` is unreleased (core is published at 0.4.7 and this landed in
#5290 today), so no installed consumer can be broken by the change.
Also adds the cache assertion parser.perf.test.ts was missing: streaming a
document rebuilds a median of 2 blocks per chunk at 50, 200 and 500
paragraphs, and asking for source ranges costs exactly zero extra rebuilds.
Integers, not milliseconds — a position-tracking implementation that dropped
the settled cache each chunk would pass any time budget loose enough not to
flake, and fails this immediately.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
cixzhang
enabled auto-merge (squash)
August 23, 2026 21:07
Contributor
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsMarkdown (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
This was referenced Aug 24, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
#5290 landed
parseMarkdown(source, {sourceRanges: true})→BlockNode.rangethis morning, spelled as a tuple:Character offsets are the right primary unit — they are what a chat transcript needs to slice a selection back out as markdown, and the incremental parser stamps them without giving up its settled cache. But a tuple is a closed shape. The next consumer that needs a line (a diff or blame view is line-addressed, and a block nested in a blockquote has lines but no contiguous
[start, end)) cannot be served by adding a field — it gets a second property with a second name, andBlockNodecarries two spellings of one concept forever.Core is published at 0.4.7 and
mainis unreleased, so today the shape is still free to change. After the release cut it is not.Solution
Same two numbers, same half-open meaning. Three call sites:
stampSourceRanges, which builds the range for each block;mergeSettledBlocks, which joins the two halves of an incrementally-merged list into one; and the tests.mergeSettledBlocksgets the clearest of it.[prevLast.range[0], deltaFirst.range[1]] as SourceRangeneeded the assertion because an array literal widens, and the reader has to know that index 0 is a start and index 1 is an end to see that it is right.{start: prevLast.range.start, end: deltaFirst.range.end}is structurally checked and says what it is.Nothing here adds line numbers or nested-block coverage. Both are real and both want a consumer first; this is the shape only, so that when one arrives it is
{start, end, startLine?, endLine?}and not a break.API
The
readonlyis carried over from the tuple, so the immutability guarantee is the same one #5290 shipped.What the change costs. It costs the call site its nicest spelling:
source.slice(...block.range)was a genuinely good line andsource.slice(block.range.start, block.range.end)is not. That is the honest price and it is worth paying once.SourceRangeis exported public surface, and an object is strictly more surface than a tuple: two named members that can never be renamed, plus the implicit promise that the type is open to more members. That is the point of paying it — the alternative is either freezing the tuple and addingpositionalongside it later, or a breaking change to a released type. The class this serves is not speculative: line-addressed consumers were the whole subject of #5154, and nested blocks can only ever be addressed by line. What it costs to be wrong is small and bounded, because it is the same two numbers either way — the field names are the only thing being frozen that was not frozen before.Theme targets
No new theme targets.
Breaking
SourceRangechanged shape, and anyconst [start, end] = block.rangestops compiling. But there is no consumer who can be broken:@astryxdesign/coreis published at 0.4.7,sourceRangesmerged into unreleasedmaina few hours ago, and nothing in the repo destructures a range outside the tests this PR updates. That is precisely why it is being done now rather than argued about later.sourceRanges.Every difference between the two shapes surfaces at compile time — a destructure, a spread, an index — so there is no silent behavioral drift to hunt for. The two that would be silent if anything consumed them are
JSON.stringifyof a block ([6,10]becomes{"start":6,"end":10}) and atoEqualagainst a literal range; nothing outside the tests this PR updates does either.The title says
refactorand that is checked, not assumed: no runtime behavior changes, and no installed consumer can be broken, so it is not a!.Performance
No runtime cost. One object literal per block instead of one array literal per block, only when
sourceRanges: true, and only for top-level blocks.The real perf content here is a test that was owed.
parser.perf.test.tsasserts wall-clock milliseconds; what the incremental parser actually exists to protect is its settled-block cache, and a cache is countable. A settled block is handed back as the same object, so a returned block that is not one of the previous chunk's is one this chunk built:Two assertions, both integers, neither touching a clock:
Both were checked against the failure they exist to catch. Making the parser drop its settled cache when
sourceRangesis on takes (2) from20975 === 20975to62524 vs 20975; making it re-parse the whole input every chunk takes (1) from2/2/2to31/118/293. A wall-clock budget loose enough not to flake on a loaded runner passes both mutants.Testing
vitest run packages/core/src/Markdown/— 272 passing. Targeted only; CI is the full check.Two findings, deliberately not fixed here
Both are pre-existing and both want their own PR — one change at a time.
parser.perf.test.tshas fiveexpect(elapsed).toBeLessThan(maxMs)assertions. They are the shape that gets deleted after it flakes on a shared runner, and they cannot see a 2× regression hiding under headroom. The new cache assertions are additive; replacing the wall-clock ones is a separate change with its own argument to make.parseMarkdownIncrementalfull-re-parses on every chunk that lands inside an unclosed code fence.findSettledBoundaryreturns-1there and the whole input is re-parsed, so ~30% of chunks in the perf fixture rebuild every block — visible as the spread between the median (2) and the total (20975). Correct, but it is the streaming case an AI transcript hits most, and a fence that streams for 200 chars costs a full re-parse per chunk for its whole length.