Skip to content
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

Styled markdown with AttributedString #757

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

laevandus
Copy link
Contributor

@laevandus laevandus commented Feb 17, 2025

🔗 Issue Link

Resolves: IOS-41

🎯 Goal

Use AttributedString's markdown parsing support with additional styling.

🛠 Implementation

  • AttributedString supported markdown parsing give much more feature rich parsing compared to before (e.g. even headers were not supported before, only basic support for links and inline presentation attributes like bold, italic, strikethrough)
  • Use StreamChat provided MarkdownParser for styling the final AttributedString
  • First parse markdown in LinkDetectionTextView and then apply mentions and plain link detection

🧪 Testing

  • Try various markdown combinations (parsing is similar to GitHub)
  • Try various combinations of markdown + mentions (e.g. @username, @userid) + plain links

🎨 Changes

Before After
b1 a1
b2 a2
b3 a3

☑️ Checklist

  • I have signed the Stream CLA (required)
  • Changelog is updated with client-facing changes
  • New code is covered by unit tests
  • Affected documentation updated (docusaurus, tutorial, CMS (task created)

@laevandus laevandus requested a review from a team as a code owner February 17, 2025 13:02
Comment on lines +3854 to +3855
branch = "feature/attributed-string-styled-markdown";
kind = branch;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change it before merge

Copy link

github-actions bot commented Feb 17, 2025

1 Warning
⚠️ Big PR

Generated by 🚫 Danger

Comment on lines 275 to 280
Group {
if let displayedText {
Text(displayedText)
} else if markdownEnabled {
Text(text)
} else {
Text(message.adjustedText)
}
Copy link
Contributor Author

@laevandus laevandus Feb 17, 2025

Choose a reason for hiding this comment

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

This file's diff is pretty unreadable.

Before

  • Manual link detection for mentions and plain links was separate from markdown handling
  • Markdown handling came purely from Text's markdown support

After

  • First parse and style markdown (if allowed)
  • Add links for mentions (if allowed)
  • Detect plain links in text (if allowed)
  • Apply styling attributes for links

Some things could be improved further: since now we use AttributedString always, then it could be created in init (onAppear feels unnecessary, but removing it does break tint color). Needs some investigation (probably better to be a separate PR).

Comment on lines +327 to +332
var linkAttributeContainer = AttributeContainer()
if let uiColor = linkAttributes[.foregroundColor] as? UIColor {
linkAttributeContainer = linkAttributeContainer.foregroundColor(Color(uiColor: uiColor))
linkAttributes.removeValue(forKey: .foregroundColor)
}
linkAttributeContainer.merge(AttributeContainer(linkAttributes))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Slightly messy because I need to make sure SwiftUIAttributes.ForegroundColor is used if UIColor is passed in from messageLinkDisplayResolver. Only way I found it make it working is init attributes without UIColor and then adding Color to the container using the .foregroundColor() functions which takes in a Color instance.

@laevandus laevandus added the ✅ Feature An issue or PR related to a feature label Feb 17, 2025
@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Feb 17, 2025

SDK Size

title develop branch diff status
StreamChatSwiftUI 7.6 MB 7.6 MB +9 KB 🟢

Copy link
Member

@nuno-vieira nuno-vieira left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ Feature An issue or PR related to a feature 🧪 QAing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants