Skip to content

feat: paint highlight ranges behind the rendered text (upstream #1218) - #17

Open
gruckion wants to merge 3 commits into
pingdotgg:fork/t3-mobilefrom
gruckion:t3/highlight-ranges
Open

gruckion wants to merge 3 commits into
pingdotgg:fork/t3-mobilefrom
gruckion:t3/highlight-ranges

Conversation

@gruckion

Copy link
Copy Markdown

This brings software-mansion#1218 to fork/t3-mobile, so that T3 Code can tint the paragraph and the word that read aloud speaks on mobile.

It adds three props to EnrichedMarkdownText:

  • highlights: UTF-16 ranges painted behind the rendered text.
  • onRenderedTextChange: sends the rendered text and its blocks.
  • onHighlightLayout: sends the rect of each range, so that the app can scroll to follow it.

A change to highlights does not parse or lay out the markdown again, and it does not change copied text. The details and limits are in software-mansion#1218.

The port to the fork needed no conflict fixes. The upstream branch needed some, because upstream's Android onDraw now draws text backgrounds.

Verification

  • Typecheck, eslint, jest, ktlint and the Android unit tests pass.
  • iOS XCTest runs 69 tests, and 3 of them are new.
  • I ran the Highlight example screen on an iPhone 17 Pro Max simulator (iOS 26.5) and a Pixel 8 emulator (Android 15), in light and dark mode. I also ran it in the T3 Code mobile app, with read aloud.

T3 Code uses a tarball of this branch (1.2.0-t3.18-3431ed09) in its read-aloud highlight PR, until this lands.

Model and harness: Claude Opus 5.5 in Claude Code.

gruckion and others added 3 commits November 20, 2026 13:00
Add a highlights prop to EnrichedMarkdownText: ranges of the rendered text
with a color, border radius and padding, drawn behind the glyphs on iOS
(TextKit 1 layout manager background pass) and Android (TextView onDraw,
before the text). The text, selection, copy and measuring do not change, and
moving a highlight does not render the Markdown again. In GitHub flavor, code
blocks and tables are tinted as a whole.

onRenderedTextChange reports the text the offsets index into and its blocks.
onHighlightLayout reports each highlight's box when it moves, so a screen can
scroll a highlighted word into view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

The change adds native text highlighting based on UTF-16 ranges in rendered Markdown text. iOS and Android report rendered text with block offsets and emit highlight layout rectangles. The TypeScript API exposes the highlight props and callbacks. Tests and documentation cover the API and native coordination. The example app adds a screen that highlights words as playback advances.

Estimated code review effort: 4 (Complex) | ~45 minutes

Merge Risk: Moderate · up to 3431e

Highlights in Android CommonMark code blocks can cover the text itself instead of sitting behind it, which breaks the main feature in that case. Highlight indices can also shift when an entry is ignored, and Android can report stale rendered text after a render failure. These are worth fixing before merge; most other findings are minor edge cases.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: adding paintable highlight ranges behind rendered text in the react-native-enriched-markdown package, which the changeset implements across JS props, Android, and iOS.…
Description check Passed The description states the purpose: porting upstream #1218 to the fork so T3 Code can tint paragraphs and spoken words. It lists the three new props (highlights, onRenderedTextChange, onHighlightLayou…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: adding paintable highlight ranges behind rendered text in the react-native-enriched-markdown package, which the changeset implements across JS props, Android, and iOS. It also notes the upstream PR reference. The title is specific and accurate, though it does not mention the new rendered-text and highlight-layout callbacks.

Full details: Description check

Explanation

The description states the purpose: porting upstream #1218 to the fork so T3 Code can tint paragraphs and spoken words. It lists the three new props (highlights, onRenderedTextChange, onHighlightLayout), which match the changeset. It also covers verification steps. It is related to the changeset in detail.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 7

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Minor · Update highlight metadata when a render failure clears the text. · EnrichedMarkdownText.kt:328

packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownText.kt:328
Minor · Data Integrity & Integration · Quick win

Update highlight metadata when a render failure clears the text.

After a successful render, these failure paths clear text without updating highlightCoordinator. If a listener is then enabled, setEnableRenderedTextChange emits the previous text and blocks although the view is empty. The event contract requires the text that the view shows. (raw.githubusercontent.com)

Route both clears through a helper that also refreshes coordinator state, emits the empty rendered-text snapshot when enabled, and schedules geometry reporting. Keep the existing render-ID and attachment guards.

Also applies to: 346-346

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3b7ded1-9253-420e-9e4a-a66e58457283
Commits

Reviewing files that changed from the base of the PR and between 497fbe6 and 3431ed0.

Files selected for processing: 45

  • apps/react-native-example/src/App.tsx
  • apps/react-native-example/src/navigation/types.ts
  • apps/react-native-example/src/screens/highlight/HighlightScreen.tsx
  • apps/react-native-example/src/screens/home/HomeScreen.tsx
  • docs-md/API_REFERENCE.md
  • docs/docs/react-native/api-reference/enriched-markdown-text.md
  • packages/react-native-enriched-markdown/__tests__/highlights.test.tsx
  • packages/react-native-enriched-markdown/__tests__/ios/ENRMHighlightCoordinatorTests.mm
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdown.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownManager.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownText.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownTextManager.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/accessibility/AccessibleMarkdownTextView.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/events/HighlightLayoutEvent.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/events/RenderedTextChangeEvent.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/highlight/HighlightEvents.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/highlight/TextHighlight.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/highlight/TextHighlightCoordinator.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/highlight/TextHighlightPainter.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/CodeBlockContainerView.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/ContainerNodeView.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/TableContainerView.kt
  • packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/utils/common/MarkdownViewManagerUtils.kt
  • packages/react-native-enriched-markdown/ios/EnrichedMarkdown.mm
  • packages/react-native-enriched-markdown/ios/EnrichedMarkdownText.mm
  • packages/react-native-enriched-markdown/ios/segments/ENRMCodeBlockContainerView.h
  • packages/react-native-enriched-markdown/ios/segments/ENRMCodeBlockContainerView.m
  • packages/react-native-enriched-markdown/ios/segments/TableContainerView.h
  • packages/react-native-enriched-markdown/ios/segments/TableContainerView.m
  • packages/react-native-enriched-markdown/ios/utils/ENRMHighlightCoordinator.h
  • packages/react-native-enriched-markdown/ios/utils/ENRMHighlightCoordinator.mm
  • packages/react-native-enriched-markdown/ios/utils/ENRMTextHighlight.h
  • packages/react-native-enriched-markdown/ios/utils/ENRMTextHighlight.m
  • packages/react-native-enriched-markdown/ios/utils/ENRMTextHighlightProps.h
  • packages/react-native-enriched-markdown/ios/utils/RuntimeKeys.h
  • packages/react-native-enriched-markdown/ios/utils/RuntimeKeys.m
  • packages/react-native-enriched-markdown/ios/utils/TextViewLayoutManager.h
  • packages/react-native-enriched-markdown/ios/utils/TextViewLayoutManager.mm
  • packages/react-native-enriched-markdown/src/EnrichedMarkdownNativeComponent.ts
  • packages/react-native-enriched-markdown/src/EnrichedMarkdownTextNativeComponent.ts
  • packages/react-native-enriched-markdown/src/index.tsx
  • packages/react-native-enriched-markdown/src/native/EnrichedMarkdownText.tsx
  • packages/react-native-enriched-markdown/src/types/MarkdownTextProps.ts
  • packages/react-native-enriched-markdown/src/types/events.ts
  • packages/react-native-enriched-markdown/src/web/nativeProps.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


### `onRenderedTextChange`

Callback fired after each render with the text as the view shows it, which [`highlights`](#highlights) offsets index into. It differs from the Markdown source (list markers, quotes and code fences are not in it) and can differ between iOS and Android, so read it on each platform rather than computing it. Blocks are joined with `\n`; a table's cells are joined with tabs and its rows with newlines.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Describe change-based callback delivery.

On iOS, changing Markdown from **Hello** to Hello renders again but does not emit this callback. The coordinator detects identical rendered text and block metadata, so the view suppresses the event. “After each render” therefore promises a notification that callers do not receive. (raw.githubusercontent.com)

Document change-based delivery here and in the matching API reference and prop comments. If delivery after every native render is required, change the emission guard instead.

override fun onDraw(canvas: Canvas) {
highlightPainter.draw(canvas)
super.onDraw(canvas)
highlightPainter.drawOverBackgrounds(canvas)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Major · Functional Correctness · Heavy lift

Draw highlight fills before the glyphs, not after them.

For a CommonMark fenced code block, CodeBlockSpan implements LineBackgroundSpan, so drawOverBackgrounds fills the highlighted glyph area after super.onDraw has drawn the text. An opaque highlight hides the glyphs. A translucent highlight changes the glyph color instead of only the background. (raw.githubusercontent.com)

Integrate this fill between syntax-background drawing and glyph/selection drawing. Moving it before all backgrounds would make opaque syntax backgrounds hide the highlight again.

if (lineEnd <= lineStart) continue
path.reset()
layout.getSelectionPath(lineStart, lineEnd, path)
path.computeBounds(rect, true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Keep separate bidi selection rectangles when painting.

On one LTR line containing abc אבג def, range [0,5) selects abc א. Android creates separate rectangles for the directional runs. This bounding rectangle also covers the unselected בג between them, so those characters receive the highlight. Preserve the separate rectangles for drawing. Use their union only for bounds(). (raw.githubusercontent.com)

Comment on lines +73 to +74
CGRect box = [layoutManager boundingRectForGlyphRange:part
inTextContainer:container];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Use separate enclosing rectangles for bidirectional ranges.

In LTR text abc אבג def, UTF-16 range [2,5) selects c א. The selected glyphs occupy separate visual pieces. This single bounding rectangle also covers the unselected ב and ג between them. Apple specifies that this API returns one rectangle enclosing all requested glyphs. (developer.apple.com)

Use enumerateEnclosingRectsForGlyphRange:withinSelectedGlyphRange:inTextContainer:usingBlock: to retain separate visual pieces. Apply padding and rounding to each rectangle. The existing bounds consumer can still union them. Single-direction ranges continue to produce one background. Apple documents enclosing rectangles for drawing range highlights. (developer.apple.com)

Comment on lines +15 to +16
if (item.start < 0 || item.end <= item.start) {
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Data Integrity & Integration · Quick win

Preserve highlight indices when ignoring empty ranges.

For text hello, pass [0,0) followed by [0,5), both with plain colors. JS retains both entries. This conversion removes the first entry, so the coordinator reports the second highlight as index: 0. The public contract defines this index as the entry's position in highlights; it must remain 1. (raw.githubusercontent.com)

Preserve the original index through conversion, or retain a non-drawing placeholder. Apply the same correction to Android's parseTextHighlights, which also compacts these entries. (raw.githubusercontent.com)

Comment on lines +193 to +194
NSUInteger first = glyphs.location > 0 ? glyphs.location - 1 : glyphs.location;
NSUInteger last = MIN(NSMaxRange(glyphs), glyphCount - 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Invalidate the full padded area when a highlight changes.

The setter invalidates only the line fragments that span each highlight and its adjacent glyphs. The background pass draws each highlight box expanded by padding. When the padding extends a box past those adjacent lines, the outer part of the old tint lies outside the invalidated region. Removing or moving that highlight can then leave tint on lines that are not redrawn.

Invalidate the line fragments that the padded rectangle covers, not only the adjacent ones.

`[EnrichedMarkdownText] Ignoring highlight ${start}-${end}: its color is not a plain color.`
);
}
continue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Data Integrity & Integration · Quick win

Preserve highlight indices when an entry is ignored.

If the first entry has an unprocessable color and the second entry is valid, this continue moves the valid entry to native index 0. Both native coordinators report that compacted index, and handleHighlightLayout forwards it unchanged. The public contract requires index 1, so a scroll-following handler can select the wrong highlight or miss it. (raw.githubusercontent.com)

Retain an original-index mapping through normalization and translate layout indices before calling the public callback. Account for native range filtering too. Keep the identity mapping when every entry is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant