Skip to content

Commit cbd29b6

Browse files
authored
Styled markdown with AttributedString (#3590)
* Add AttributedString extension for parsing markdown and enabling styling in the MarkdownFormatter * Add tests for AttributedString markdown styling * Use paragraph style for setting correct text alignment in lists * Remove MarkdownStyles.linkFont because we use UITextView for rendering and UITextView overrides it * Document linkFont removal and attributed string parsing * Basic support for RTL and remove background color from code blocks * Use unicode bullet for lists in markdown * Be more careful with insertion index and skip parsing if string is empty * Use Fonts for default fonts in markdown * Remove SwiftyMarkdown * Turn off markdown styles in the demo app * Do not use markdown regex
1 parent 63ec71e commit cbd29b6

File tree

122 files changed

+1074
-2574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+1074
-2574
lines changed

.swiftlint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ excluded:
44
- Dependencies
55
- Sources/StreamChatUI/Generated
66
- Sources/StreamChatUI/StreamSwiftyGif
7-
- Sources/StreamChatUI/StreamSwiftyMarkdown
87
- Sources/StreamChatUI/StreamNuke
98
- Sources/StreamChat/StreamStarscream
109
- Sources/StreamChatUI/StreamDifferenceKit
@@ -80,7 +79,8 @@ file_name_no_space:
8079
severity: error
8180

8281
identifier_name:
83-
excluded: [r, g, b, a, x, y, z, dx, dy, dz, i, j, k, id, op, or, me, at, to, in]
82+
excluded:
83+
[r, g, b, a, x, y, z, dx, dy, dz, i, j, k, id, op, or, me, at, to, in]
8484
allowed_symbols: ["_"]
8585
validates_start_with_lowercase: "warning"
8686

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6+
## StreamChat
7+
### ✅ Added
8+
- Add `MarkdownParser` for parsing and styling markdown strings [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
9+
- Add `Fonts.title2` for supporting markdown headers [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
10+
11+
### StreamChatUI
612
### 🔄 Changed
13+
- Feature rich markdown rendering with `AttributedString` [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
14+
- Note: Markdown is rendered only on iOS 15 and above. On iOS 14 and below markdown is rendered as plain text
15+
- Rename `MarkdownFormatter.format(_:)` to `MarkdownFormatter.format(_:attributes:)` for allowing to pass in current text attributes
16+
### 💥 Removed
17+
- Remove `MarkdownStyles.linkFont` because link attributes are ignored by `UITextView`. Update `ChatMessageContentView.textView.linkTextAttributes` instead [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
18+
- Remove `DefaultMarkdownFormatter.markdownRegexPattern` because regular expression based validation was removed [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
19+
- Remove `MarkdownFormatter.containsMarkdown(_:)`, optionally validate input strings in `MarkdownFormatter.format(_:attributes:)` instead [#3590](https://github.com/GetStream/stream-chat-swift/pull/3590)
720

821
# [4.73.0](https://github.com/GetStream/stream-chat-swift/releases/tag/4.73.0)
922
_February 27, 2025_

DemoApp/StreamChat/StreamChatWrapper+DemoApp.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ extension StreamChatWrapper {
6767
Components.default.reactionsSorting = ReactionSorting.byFirstReactionAt
6868
Components.default.channelListErrorView = DemoChatChannelListErrorView.self
6969

70-
// Customize MarkdownFormatter
71-
let defaultFormatter = DefaultMarkdownFormatter()
72-
defaultFormatter.styles.bodyFont.color = .systemOrange
73-
defaultFormatter.styles.codeFont.color = .systemPurple
74-
defaultFormatter.styles.h1Font.color = .systemBlue
75-
defaultFormatter.styles.h2Font.color = .systemRed
76-
defaultFormatter.styles.h3Font.color = .systemYellow
77-
defaultFormatter.styles.h4Font.color = .systemGreen
78-
defaultFormatter.styles.h5Font.color = .systemBrown
79-
defaultFormatter.styles.h6Font.color = .systemPink
80-
Appearance.default.formatters.markdownFormatter = defaultFormatter
70+
// Example of how to customize Markdown
71+
/*
72+
let defaultFormatter = DefaultMarkdownFormatter()
73+
defaultFormatter.styles.bodyFont.color = .systemOrange
74+
defaultFormatter.styles.codeFont.color = .systemPurple
75+
defaultFormatter.styles.h1Font.color = .systemBlue
76+
defaultFormatter.styles.h2Font.color = .systemRed
77+
defaultFormatter.styles.h3Font.color = .systemYellow
78+
defaultFormatter.styles.h4Font.color = .systemGreen
79+
defaultFormatter.styles.h5Font.color = .systemBrown
80+
defaultFormatter.styles.h6Font.color = .systemPink
81+
Appearance.default.formatters.markdownFormatter = defaultFormatter
82+
*/
8183
}
8284
}
8385

Makefile

-6
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ update_dependencies:
4646
make update_nuke version=10.3.3
4747
echo "👉 Updating SwiftyGif"
4848
make update_swiftygif version=5.4.2
49-
echo "👉 Updating SwiftyMarkdown"
50-
make update_swiftymarkdown version=master
5149
echo "👉 Updating DifferenceKit"
5250
make update_differencekit version=1.3.0
5351

@@ -59,10 +57,6 @@ update_swiftygif: check_version_parameter
5957
./Scripts/updateDependency.sh $(version) Dependencies/SwiftyGif Sources/StreamChatUI/StreamSwiftyGif SwiftyGif
6058
./Scripts/removePublicDeclarations.sh Sources/StreamChatUI/StreamSwiftyGif
6159

62-
update_swiftymarkdown: check_version_parameter
63-
./Scripts/updateDependency.sh $(version) Dependencies/SwiftyMarkdown Sources/StreamChatUI/StreamSwiftyMarkdown Sources
64-
./Scripts/removePublicDeclarations.sh Sources/StreamChatUI/StreamSwiftyMarkdown
65-
6660
update_differencekit: check_version_parameter
6761
./Scripts/updateDependency.sh $(version) Dependencies/DifferenceKit Sources/StreamChatUI/StreamDifferenceKit Sources
6862
./Scripts/removePublicDeclarations.sh Sources/StreamChatUI/StreamDifferenceKit

Scripts/run-linter.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
echo -e "👉 Running SwiftFormat Linting"
55

66
echo -e "👉 Linting Sources..."
7-
mint run swiftformat --lint --config .swiftformat Sources --exclude **/Generated,Sources/StreamChatUI/StreamNuke,Sources/StreamChatUI/StreamSwiftyGif,Sources/StreamChatUI/StreamSwiftyMarkdown,Sources/StreamChatUI/StreamDifferenceKit
7+
mint run swiftformat --lint --config .swiftformat Sources --exclude **/Generated,Sources/StreamChatUI/StreamNuke,Sources/StreamChatUI/StreamSwiftyGif,Sources/StreamChatUI/StreamDifferenceKit
88

99
echo -e "👉 Linting Tests..."
1010
mint run swiftformat --lint --config .swiftformat Tests

Scripts/updateDependency.sh

-9
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ if [[ $dependency_directory == *"Nuke"* ]]; then
2525
dependency_url="[email protected]:kean/Nuke.git"
2626
elif [[ $dependency_directory == *"SwiftyGif"* ]]; then
2727
dependency_url="[email protected]:kirualex/SwiftyGif.git"
28-
elif [[ $dependency_directory == *"SwiftyMarkdown"* ]]; then
29-
dependency_url="[email protected]:GetStream/SwiftyMarkdown.git"
3028
elif [[ $dependency_directory == *"DifferenceKit"* ]]; then
3129
dependency_url="[email protected]:ra1028/DifferenceKit.git"
3230
else
@@ -70,10 +68,3 @@ if [[ $dependency_directory == *"DifferenceKit"* ]]; then
7068
# We currently use customized UIKit extensions in Utils/DifferenceKit+Stream.swift
7169
rm $output_directory/Extensions/UIKitExtension.swift
7270
fi
73-
74-
if [[ $dependency_directory == *"SwiftyMarkdown"* ]]; then
75-
# We currently use customized version of SwiftyMarkdown
76-
git restore $output_directory/SwiftyMarkdown/PerformanceLog.swift || true
77-
git restore $output_directory/SwiftyMarkdown/SwiftyLineProcessor.swift || true
78-
git restore $output_directory/SwiftyMarkdown/SwiftyTokeniser.swift || true
79-
fi

0 commit comments

Comments
 (0)