Repository navigation
Conversation
This was referenced Nov 17, 2026
This was referenced Nov 17, 2026
rumpl
force-pushed
the
rumpl/markdown-trusted-sgr
branch
from
November 17, 2026 22:47
9f9c89d to
45be8ce
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Active issues in SGR handling break fenced-code wrapping and tab alignment, lose styling across wraps and inline runs, and truncate styled URL hyperlink targets.
4 open issues
What changed in this PR
Adds opt-in trusted SGR styling to the Markdown renderers, preparing them for display-only transformers while continuing to strip other terminal controls.
Changes:
- Adds
AllowSGRto fast and incremental rendering, preserving renderer options across width changes. - Introduces bounded per-attribute style replay across wraps and makes inline parsing and width calculations escape-aware.
- Adds regression tests for sanitization, style replay, wrapping, link labels, and incremental rendering.
| File | Description |
|---|---|
pkg/tui/components/markdown/sgr.go |
Tracks SGR attributes and replays active styles at line breaks. |
pkg/tui/components/markdown/sanitize.go |
Optionally preserves SGR while stripping other terminal controls. |
pkg/tui/components/markdown/incremental.go |
Adds trusted styling and preserves options across width changes. |
pkg/tui/components/markdown/fast_renderer.go |
Integrates trusted styling and updates inline parsing, measurement, and wrapping. |
pkg/tui/components/markdown/ansi_test.go |
Tests embedded styling, sanitization, wrapping, and renderer consistency. |
Review effort: Balanced
| } | ||
|
|
||
| input = sanitizeForTerminal(input) | ||
| input = sanitizeForTerminal(input, r.allowSGR) |
Comment on lines
+1833
to
+1836
| // Transformer styling stays inside the run it decorates. | ||
| if text[i] == '\x1b' { | ||
| i, _ = scanAnsiSequence(text, i) | ||
| continue |
Comment on lines
+1833
to
+1836
| // Transformer styling stays inside the run it decorates. | ||
| if text[i] == '\x1b' { | ||
| i, _ = scanAnsiSequence(text, i) | ||
| continue |
| out.WriteByte('\n') | ||
| return | ||
| } | ||
| out.WriteString("\x1b[m\n\x1b[") |
Add AllowSGR to the fast and incremental renderers, for trusted input such as display transformer output: SGR sequences are kept, every other escape is still stripped. Inline parsing, link labels and width calculations skip embedded sequences. Wrapping now tracks active styling as one value per SGR attribute, including compound and colon-form codes, and replays it as a single sequence at each break. This keeps replay bounded however many codes a span holds, and fixes wraps that lost styling: the no-space fast path did not replay styles, a word's own codes were replayed before it, and style-only runs took a column and could wrap onto an empty row. Signed-off-by: Djordje Lukic <[email protected]>
rumpl
force-pushed
the
rumpl/markdown-trusted-sgr
branch
from
November 17, 2026 22:58
45be8ce to
8d26c16
Compare
| // Fast path: check if text contains any markdown characters or URLs | ||
| // If not, apply the restore style directly and return | ||
| firstMarker := strings.IndexAny(text, inlineMarkdownChars) | ||
| firstMarker := indexInlineMarker(text) |
This branch has not been 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.

Summary
Adds
AllowSGRto the fast and incremental Markdown renderers, for trusted input such as display transformer output (#4952). SGR sequences are kept; every other escape is still stripped. Inline parsing, link labels and width calculations skip embedded sequences.Wrapping now tracks active styling as one value per SGR attribute, including compound (
1;38;2;r;g;b) and colon-form (38:2::r:g:b) codes, and replays it as one sequence at each break. Replay stays bounded however many codes a span holds. This also fixes wraps that lost styling, all reproducible onmainwith renderer-generated styles:Second of a stack, on top of #4948:
AllowSGRhas no caller until #4952.Validation
task build; race tests forpkg/tui/...andpkg/leantui/...golangci-lintreports no new issues