Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### ✅ Added
- Opens `MarkdownFormatter` so that it can be customised [#978](https://github.com/GetStream/stream-chat-swiftui/pull/978)
### 🐞 Fixed
- Fix openChannel not working when searching or another chat shown [#975](https://github.com/GetStream/stream-chat-swiftui/pull/975)
- Fix crash when using a font that does not support bold or italic trait [#976](https://github.com/GetStream/stream-chat-swiftui/pull/976)
Expand Down
4 changes: 3 additions & 1 deletion Sources/StreamChatSwiftUI/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import StreamChat
/// Class providing implementations of several utilities used in the SDK.
/// The default implementations can be replaced in the init method, or directly via the variables.
public class Utils {
var markdownFormatter = MarkdownFormatter()
public var markdownFormatter: MarkdownFormatter

public var dateFormatter: DateFormatter

Expand Down Expand Up @@ -76,6 +76,7 @@ public class Utils {
internal var pollsDateFormatter = PollsDateFormatter()

public init(
markdownFormatter: MarkdownFormatter = DefaultMarkdownFormatter(),
dateFormatter: DateFormatter = .makeDefault(),
messageRelativeDateFormatter: DateFormatter = MessageRelativeDateFormatter(),
galleryHeaderViewDateFormatter: DateFormatter = GalleryHeaderViewDateFormatter(),
Expand Down Expand Up @@ -104,6 +105,7 @@ public class Utils {
sortReactions: @escaping (MessageReactionType, MessageReactionType) -> Bool = Utils.defaultSortReactions,
shouldSyncChannelControllerOnAppear: @escaping (ChatChannelController) -> Bool = { _ in true }
) {
self.markdownFormatter = markdownFormatter
self.dateFormatter = dateFormatter
self.messageRelativeDateFormatter = messageRelativeDateFormatter
self.galleryHeaderViewDateFormatter = galleryHeaderViewDateFormatter
Expand Down
25 changes: 22 additions & 3 deletions Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@ import Foundation
import StreamChat
import SwiftUI

public protocol MarkdownFormatter {
/// Formats a Markdown string into an `AttributedString`, merging Markdown styles with the provided base attributes and honoring the given layout direction.
/// - Parameters:
/// - string: The Markdown-formatted source string to render.
/// - attributes: Base attributes applied to the entire string; Markdown-specific styling is merged on top of these defaults.
/// - layoutDirection: The text layout direction (left-to-right or right-to-left) used when interpreting and rendering Markdown blocks (for example, lists, block quotes, and headings).
/// - Returns: An `AttributedString` containing the rendered Markdown with the resolved attributes.
@available(iOS 15, *)
func format(
_ string: String,
attributes: AttributeContainer,
layoutDirection: LayoutDirection
) -> AttributedString
}

/// Converts markdown string to AttributedString with styling attributes.
final class MarkdownFormatter {
open class DefaultMarkdownFormatter: MarkdownFormatter {
@Injected(\.colors) private var colors
@Injected(\.fonts) private var fonts

private let markdownParser = MarkdownParser()
private let markdownParser: MarkdownParser

public init() {
markdownParser = MarkdownParser()
}

@available(iOS 15, *)
func format(
open func format(
_ string: String,
attributes: AttributeContainer,
layoutDirection: LayoutDirection
Expand Down
Loading