diff --git a/Sources/StreamChatSwiftUI/Utils.swift b/Sources/StreamChatSwiftUI/Utils.swift index f39985f1..3d41ba3d 100644 --- a/Sources/StreamChatSwiftUI/Utils.swift +++ b/Sources/StreamChatSwiftUI/Utils.swift @@ -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 @@ -75,6 +75,7 @@ public class Utils { internal var pollsDateFormatter = PollsDateFormatter() public init( + markdownFormatter: MarkdownFormatter = DefaultMarkdownFormatter(), dateFormatter: DateFormatter = .makeDefault(), messageRelativeDateFormatter: DateFormatter = MessageRelativeDateFormatter(), videoPreviewLoader: VideoPreviewLoader = DefaultVideoPreviewLoader(), @@ -102,6 +103,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.videoPreviewLoader = videoPreviewLoader diff --git a/Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift b/Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift index 97faebdc..31a16db6 100644 --- a/Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift +++ b/Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift @@ -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