Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -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(),
Expand Down Expand Up @@ -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
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