fix: flatten decorations to prevent stack overflow#1182
fix: flatten decorations to prevent stack overflow#1182Lakshith7748 wants to merge 2 commits intoshikijs:mainfrom
Conversation
✅ Deploy Preview for shiki-matsu ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for shiki-next ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1182 +/- ##
==========================================
- Coverage 95.08% 94.48% -0.60%
==========================================
Files 91 91
Lines 7866 7941 +75
Branches 1662 1672 +10
==========================================
+ Hits 7479 7503 +24
- Misses 381 432 +51
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I think we should provide an option for wrap/flatten style. And I am not sure if we need to introduce the options to disable the interaction check. Maybe for flatten mode we could just skip it |
antfu
left a comment
There was a problem hiding this comment.
The current approach is a breaking change, and we would not take it. Refer to the comment above.
- Add decorationsStyle option ('wrap' | 'flatten') to DecorationOptions
- 'wrap' (default): maintains current behavior with nested wrappers and intersection validation
- 'flatten': skips intersection check, allowing deeply nested decorations without stack overflow
- Addresses maintainer feedback to avoid breaking changes
- Add test case for flatten mode with nested decorations
Thanks for the feedback! I've updated the PR to add a decorationsStyle option instead of making breaking changes.
|
Description
This PR fixes a RangeError: Maximum call stack size exceeded when using many overlapping decorations.
The issue was caused by transformer-decorations creating deeply nested
span
elements for overlapping decorations. When the nesting depth exceeded the stack limit (e.g., > 2500 levels), hast-util-to-html would crash due to recursion.
This fix modifies transformer-decorations to flatten the decoration structure. Instead of wrapping the existing content in a new
span
for every decoration (which creates nesting), the transformer now applies the decoration to each child element individually when possible.
Linked Issues
Fixes #592
Additional context
Modified packages/core/src/transformer-decorations.ts to apply decorations to individual children instead of creating a wrapper.
Preserved the behavior of creating a wrapper for empty decorations (insertion points).
Updated snapshots in packages/core/test/decorations.test.ts to reflect the flattened structure.
Verified with a reproduction test case involving 5000 overlapping decorations.