为什么选择 LLMMarkdown?
市场上的 Markdown 解析库不在少数,但 LLMMarkdown 凭借以下特性脱颖而出:
- 原生跨平台支持:同时支持 iOS 13.0 + 和 macOS 10.15+,一套代码覆盖多平台
- 高性能解析:基于 AST(抽象语法树)的架构设计,确保高效处理大型文档
- 深度可定制:从字体、颜色到段落样式,全方位支持自定义主题
- Swift 原生实现:完全用 Swift 编写,提供类型安全和现代 Swift 特性支持
- SwiftUI 友好:轻松集成到 SwiftUI 应用中,同时兼容 UIKit 和 AppKit
核心功能亮点
1. 全面的 Markdown 元素支持
LLMMarkdown 支持所有常用的 Markdown 元素,包括:
- 文本格式化:粗体、斜体及嵌套强调
- 标题:H1 到 H6,每个级别都可自定义样式
- 列表:有序列表和无序列表,支持正确编号
- 链接:可点击的超链接,支持自定义样式
- 代码:内联代码块,带有语法高亮支持
- 段落和换行:保留正确的间距和格式
2. 灵活的主题系统
LLMMarkdown 提供了强大的主题系统,让你的 Markdown 内容完美融入应用设计风格。
框架内置了默认主题和 GitHub 风格主题,同时支持完全自定义:
// 创建自定义主题
struct MyCustomTheme: MarkdownTheme {
var colorScheme: ColorScheme {
ColorScheme(
primary: .label,
secondary: .secondaryLabel,
background: .systemBackground,
surface: .secondarySystemBackground,
accent: .systemBlue,
link: .systemBlue,
code: .systemRed,
codeBackground: .systemGray6
)
}
var typography: Typography {
Typography(
baseFont: .systemFont(ofSize: 16),
baseFontSize: 16,
lineHeight: 1.5
)
}
}
// 使用自定义主题
let customParser = LLMMarkdown(theme: MyCustomTheme())
3. 简单易用的 API
LLMMarkdown 的 API 设计遵循 "简单而强大" 的原则,只需几行代码即可实现 Markdown 解析:
import LLMMarkdown
let markdown = """
# Hello World
This is **bold** text and this is *italic*.
Here's some `inline code` and a [link](https://example.com).
"""
// 转换为富文本
let attributedString = LLMMarkdown.attributedString(from: markdown)
// 在UI组件中使用
textView.attributedText = attributedString
4. 无缝集成 SwiftUI
对于 SwiftUI 项目,LLMMarkdown 提供了便捷的集成方式:
import SwiftUI
import LLMMarkdown
struct ContentView: View {
let markdown = "# Hello **SwiftUI**!"
var body: some View {
MarkdownTextView(markdown: markdown)
.padding()
}
}
struct MarkdownTextView: UIViewRepresentable {
let markdown: String
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.isEditable = false
textView.isScrollEnabled = true
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.attributedText = LLMMarkdown.attributedString(from: markdown)
}
}
效果
地址
欢迎 star fork:github.com/linghugoogl…