Skip to content

Commit f77076b

Browse files
Open markdown formatter (#978)
* Open markdown formatter (#974) * Refactor markdownFormatter to use DefaultMarkdownFormatter and update MarkdownFormatter protocol for improved formatting capabilities * Update Utils to allow custom markdownFormatter initialization * feat(Formatter): make format method open for better accessibility * Update CHANGELOG.md --------- Co-authored-by: Hossam Youssof <eng.hossam.youssof@gmail.com>
1 parent d30fdf3 commit f77076b

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
44
# Upcoming
55

66
### ✅ Added
7-
- Make the commandsHandler and the mention methods public to allow customization of the mention command handler [#979](https://github.com/GetStream/stream-chat-swiftui/pull/979)
7+
- Opens the `commandsHandler` and makes the mention methods public [#979](https://github.com/GetStream/stream-chat-swiftui/pull/979)
8+
- Opens `MarkdownFormatter` so that it can be customised [#978](https://github.com/GetStream/stream-chat-swiftui/pull/978)
89

910
### 🐞 Fixed
1011
- Fix openChannel not working when searching or another chat shown [#975](https://github.com/GetStream/stream-chat-swiftui/pull/975)

Sources/StreamChatSwiftUI/Utils.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import StreamChat
88
/// Class providing implementations of several utilities used in the SDK.
99
/// The default implementations can be replaced in the init method, or directly via the variables.
1010
public class Utils {
11-
var markdownFormatter = MarkdownFormatter()
11+
public var markdownFormatter: MarkdownFormatter
1212

1313
public var dateFormatter: DateFormatter
1414

@@ -76,6 +76,7 @@ public class Utils {
7676
internal var pollsDateFormatter = PollsDateFormatter()
7777

7878
public init(
79+
markdownFormatter: MarkdownFormatter = DefaultMarkdownFormatter(),
7980
dateFormatter: DateFormatter = .makeDefault(),
8081
messageRelativeDateFormatter: DateFormatter = MessageRelativeDateFormatter(),
8182
galleryHeaderViewDateFormatter: DateFormatter = GalleryHeaderViewDateFormatter(),
@@ -104,6 +105,7 @@ public class Utils {
104105
sortReactions: @escaping (MessageReactionType, MessageReactionType) -> Bool = Utils.defaultSortReactions,
105106
shouldSyncChannelControllerOnAppear: @escaping (ChatChannelController) -> Bool = { _ in true }
106107
) {
108+
self.markdownFormatter = markdownFormatter
107109
self.dateFormatter = dateFormatter
108110
self.messageRelativeDateFormatter = messageRelativeDateFormatter
109111
self.galleryHeaderViewDateFormatter = galleryHeaderViewDateFormatter

Sources/StreamChatSwiftUI/Utils/MarkdownFormatter.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,34 @@ import Foundation
66
import StreamChat
77
import SwiftUI
88

9+
public protocol MarkdownFormatter {
10+
/// Formats a Markdown string into an `AttributedString`, merging Markdown styles with the provided base attributes and honoring the given layout direction.
11+
/// - Parameters:
12+
/// - string: The Markdown-formatted source string to render.
13+
/// - attributes: Base attributes applied to the entire string; Markdown-specific styling is merged on top of these defaults.
14+
/// - 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).
15+
/// - Returns: An `AttributedString` containing the rendered Markdown with the resolved attributes.
16+
@available(iOS 15, *)
17+
func format(
18+
_ string: String,
19+
attributes: AttributeContainer,
20+
layoutDirection: LayoutDirection
21+
) -> AttributedString
22+
}
23+
924
/// Converts markdown string to AttributedString with styling attributes.
10-
final class MarkdownFormatter {
25+
open class DefaultMarkdownFormatter: MarkdownFormatter {
1126
@Injected(\.colors) private var colors
1227
@Injected(\.fonts) private var fonts
1328

14-
private let markdownParser = MarkdownParser()
29+
private let markdownParser: MarkdownParser
30+
31+
public init() {
32+
markdownParser = MarkdownParser()
33+
}
1534

1635
@available(iOS 15, *)
17-
func format(
36+
open func format(
1837
_ string: String,
1938
attributes: AttributeContainer,
2039
layoutDirection: LayoutDirection

0 commit comments

Comments
 (0)