Skip to content

Commit a7c2e7b

Browse files
authored
Fix #297 - Basic markdown support in MLXChatExample (#303)
* Update MessageView.swift Small update to MessageView.swift to enable default handling of markdown content in "user" and "assistant" content. * Update readme to include details of markdown support.
1 parent 1db9d3a commit a7c2e7b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Applications/MLXChatExample/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ The codebase is thoroughly documented with:
7373
- Clear inline comments explaining complex logic
7474
- DocC documentation format
7575

76+
### Markdown Support
77+
78+
This sample app renders markdown content using SwiftUI's native `Text` view by passing the content as a `LocalizedStringKey`:
79+
80+
```swift
81+
Text(LocalizedStringKey(message.content))
82+
```
83+
84+
#### Limitations and Alternatives
85+
86+
The default SwiftUI markdown rendering only supports standard markdown syntax. It does not support advanced features like tables and task lists that are available in GitHub Flavored Markdown (GFM).
87+
88+
For more comprehensive markdown support:
89+
90+
- **GitHub Flavored Markdown**: Consider using the [swift-markdown-ui](https://github.com/gonzalezreal/swift-markdown-ui) library. However, be aware that this library currently has an [unresolved issue with text selection](https://github.com/gonzalezreal/swift-markdown-ui/issues/264), which is why it wasn't used in this example.
91+
92+
- **Enhanced Text Selection**: If you're satisfied with standard markdown but want better text selection capabilities on iOS (instead of only being able to select and copy entire content block), consider combining:
93+
- [SelectableText](https://github.com/kevinhermawan/SelectableText) for improved selection functionality
94+
- [MarkdownToAttributedString](https://github.com/madebywindmill/MarkdownToAttributedString) for markdown formatting
95+
96+
> More discussion on this can be found on [issue #297](https://github.com/ml-explore/mlx-swift-examples/issues/297)
97+
7698
## Getting Started
7799

78100
1. Clone the repository

Applications/MLXChatExample/Views/MessageView.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ struct MessageView: View {
4747
.clipShape(.rect(cornerRadius: 12))
4848
}
4949

50-
// Message content with tinted background
51-
Text(message.content)
50+
// Message content with tinted background.
51+
// LocalizedStringKey used to trigger default handling of markdown content.
52+
Text(LocalizedStringKey(message.content))
5253
.padding(.vertical, 8)
5354
.padding(.horizontal, 12)
5455
.background(.tint, in: .rect(cornerRadius: 16))
@@ -58,8 +59,9 @@ struct MessageView: View {
5859

5960
case .assistant:
6061
// Assistant messages are left-aligned without background
62+
// LocalizedStringKey used to trigger default handling of markdown content.
6163
HStack {
62-
Text(message.content)
64+
Text(LocalizedStringKey(message.content))
6365
.textSelection(.enabled)
6466

6567
Spacer()

0 commit comments

Comments
 (0)