Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct MathFirstMarkdownViewRenderer: MarkdownViewRenderer {

var extractor = ParsingRangesExtractor()
extractor.visit(content.parse(options: ParseOptions().union(.parseBlockDirectives)))
for range in extractor.parsableRanges(in: rawText) {
for range in extractor.parsableRanges(in: rawText).reversed() {
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes a crash scenario (math blocks + code blocks). Please add a regression test that renders markdown containing both a fenced code block and a display-math block to ensure MathFirstMarkdownViewRenderer no longer crashes when processing multiple parsable segments.

Copilot uses AI. Check for mistakes.
let segment = rawText[range]
let segmentParser = MathParser(text: segment)
Comment on lines +21 to 23
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with iterating ranges in reverse, this loop still reuses precomputed Range<String.Index> values after mutating rawText (via replaceSubrange). Swift String mutations can invalidate existing indices/ranges, which can still lead to crashes in edge cases. Consider capturing ranges as integer offsets (e.g., UTF-16 offsets) and recreating fresh String.Index values on the current rawText before slicing/replacing, or perform replacements on a copied String segment and then splice the segment back into rawText.

Copilot uses AI. Check for mistakes.
for math in segmentParser.mathRepresentations.reversed() where !math.kind.inline {
Expand Down