-
Notifications
You must be signed in to change notification settings - Fork 320
Render message text as markdown in the Compose UI kit #6681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gpunto
merged 30 commits into
develop
from
gianmarcodavid/and-1475-add-markdown-support-to-the-compose-ui-kit
Sep 7, 2026
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
78fa627
Add opt-in markdown rendering for Compose message text
gpunto a066949
Mark every line of a rendered block quote
gpunto ddbb469
Keep quote markers correct across hard breaks, paragraphs and nesting
gpunto 29ea4e3
Resolve reference links, autolinks, character references and image al…
gpunto bd7448b
Fix block handling in list items, quoted code, tables and link destin…
gpunto 33bd669
Keep a list marker on the same line as the block that follows it
gpunto d927098
Correct heading scale, empty renders, literal code and block separation
gpunto d871b92
Absorb the line feed after a break written as a tag
gpunto d87a66b
Name the hard break check so its two spellings live in one place
gpunto f6c1775
Restrict link schemes, contain render failures and fix construct edge…
gpunto c8d1642
Keep markdown styling under links and stop dropping content from sour…
gpunto de41c4e
Expose markdown through a formatter factory instead of a public class
gpunto e6e46a2
Keep markdown styling internal until something needs to override it
gpunto 1bb4e2e
Collapse the repeated link and heading handling
gpunto 63d6c27
Trim the comments and docs down to what the code does not already say
gpunto 66e235e
Stop claiming a markdown flavour the renderer does not implement
gpunto 448696b
Drop the markdown factory parameters nothing has asked for
gpunto 48ed4ca
Put the sample's markdown rendering behind a feature flag
gpunto 53c291d
Take block separation from the source and stop link styling from flat…
gpunto 9c27ae1
Drop the link failure log and bound the character reference search
gpunto c43c5a3
Scroll the sample's feature flag list now that it outgrows the screen
gpunto 4d0f1f7
Address review feedback on the Compose markdown renderer
gpunto 9842fc9
Hang the wrapped lines of markdown lists and quotes
gpunto 348ad17
Revert "Hang the wrapped lines of markdown lists and quotes"
gpunto 858dbbe
Reapply "Hang the wrapped lines of markdown lists and quotes"
gpunto 6efaa9c
Keep a wrapped list line at the indent of its own level
gpunto 8e57e99
Draw the block quote rail instead of writing it as a marker
gpunto 6051c87
Mirror the block quote rail in a right to left layout
gpunto c8ad671
Cover only right to left content in the mirrored rail snapshot
gpunto a1d37b4
Remember the layout result held by the markdown snapshot tests
gpunto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
97 changes: 97 additions & 0 deletions
97
...ndroid-compose/src/main/java/io/getstream/chat/android/compose/ui/util/BlockQuoteRails.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * Copyright (c) 2014-2026 Stream.io Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Stream License; | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/GetStream/stream-chat-android/blob/main/LICENSE | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.getstream.chat.android.compose.ui.util | ||
|
|
||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.drawBehind | ||
| import androidx.compose.ui.geometry.Offset | ||
| import androidx.compose.ui.geometry.Size | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.text.AnnotatedString | ||
| import androidx.compose.ui.text.TextLayoutResult | ||
| import androidx.compose.ui.text.style.ResolvedTextDirection | ||
| import androidx.compose.ui.unit.Density | ||
| import androidx.compose.ui.unit.TextUnit | ||
| import androidx.compose.ui.unit.TextUnitType | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| /** | ||
| * Draws the rail beside every rendered line of a block quote, in [color], reading the quotes from | ||
| * the [AnnotationTagBlockQuote] ranges of [annotations] and their extent from [layout]. | ||
| * | ||
| * Drawn rather than written as a marker character, because the lines a quote occupies are only | ||
| * known once the text has been laid out. A character can be placed on a line break the renderer | ||
| * made, never on one the layout chose, and it leaves a gap between lines besides. | ||
| */ | ||
| internal fun Modifier.blockQuoteRails( | ||
| annotations: List<AnnotatedString.Range<String>>, | ||
| layout: () -> TextLayoutResult?, | ||
| color: Color, | ||
| indentPerDepth: TextUnit, | ||
| ): Modifier { | ||
| val quotes = annotations.filter { it.tag == AnnotationTagBlockQuote } | ||
| if (quotes.isEmpty()) return this | ||
| return drawBehind { | ||
| val laidOut = layout() ?: return@drawBehind | ||
| val step = indentPerDepth.toPx(this, laidOut) | ||
| val width = RailWidth.toPx() | ||
| quotes.forEach { quote -> | ||
| val depth = quote.item.toIntOrNull() ?: return@forEach | ||
| // Centred in the space the last level of indent opened up. | ||
| val offset = step * (depth - 1) + (step - width) / 2 | ||
| val lines = laidOut.lineRange(quote) ?: return@forEach | ||
| // Mirrored for a quote running right to left, since the indent it sits in is | ||
| // start-relative. Taken from the paragraph rather than the layout direction, because | ||
| // one message can carry a quote of each direction. | ||
| val left = when (laidOut.getParagraphDirection(quote.start)) { | ||
| ResolvedTextDirection.Rtl -> size.width - offset - width | ||
| else -> offset | ||
| } | ||
| for (line in lines) { | ||
| val top = laidOut.getLineTop(line) | ||
| drawRect( | ||
| color = color, | ||
| topLeft = Offset(left, top), | ||
| size = Size(width, laidOut.getLineBottom(line) - top), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Every line the quote occupies, blank ones included, so the rail runs unbroken through the gap | ||
| * between two paragraphs of the same quote. | ||
| */ | ||
| private fun TextLayoutResult.lineRange(quote: AnnotatedString.Range<String>): IntRange? { | ||
| val last = (quote.end - 1).coerceAtLeast(quote.start) | ||
| if (quote.start >= layoutInput.text.length) return null | ||
| return getLineForOffset(quote.start)..getLineForOffset(last.coerceAtMost(layoutInput.text.length - 1)) | ||
| } | ||
|
|
||
| /** Resolves against the laid-out font size, since the indent is expressed relative to the text. */ | ||
| private fun TextUnit.toPx(density: Density, layout: TextLayoutResult): Float { | ||
| val fontSize = layout.layoutInput.style.fontSize | ||
| return when { | ||
| type == TextUnitType.Sp -> with(density) { toPx() } | ||
| type == TextUnitType.Em && fontSize.type == TextUnitType.Sp -> | ||
| value * with(density) { fontSize.toPx() } | ||
| else -> 0f | ||
| } | ||
| } | ||
|
|
||
| private val RailWidth = 2.dp | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.