EFMarkdown 是一个基于 Swift 的轻量级 Markdown 库,基于 EFCMark,受 markdown 和 Markoff 的启发。
概述
示例1 | 示例2 | 示例3 | 示例4 |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install
。
需求
版本 | 需要 |
---|---|
0.x | XCode 8.0+ Swift 3.0+ iOS 8.0+ |
4.x | XCode 9.0+ Swift 4.0+ iOS 8.0+ |
5.x | XCode 10.2+ Swift 5.0+ iOS 8.0+ |
安装
EFMarkdown 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "EFMarkdown"
使用说明
1. 将Markdown转换为HTML
您可以使用EFMarkdown
轻松地将Markdown字符串转换为HTML字符串。
let markdown = "# Hello"
var html = ""
do {
html = try EFMarkdown().markdownToHTML(markdown, options: EFMarkdownOptions.safe)
print(html) // This will return "<h1>Hello</h1>\n"
} catch let error as NSError {
print ("Error: \(error.domain)")
}
2. 预览Markdown
您可以使用EFMarkdownView
来预览Markdown。
let screenSize = UIScreen.main.bounds
let markView = EFMarkdownView()
markView.frame = CGRect(x: 0, y: 20, width: screenSize.width, height: screenSize.height - 20)
markView.onRendered = {
[weak self] (height) in
if let _ = self {
// Optional: you can know the change of height in this block
print("onRendered height: \(height ?? 0)")
}
}
self.view.addSubview(markView)
markView.load(markdown: testMarkdownFileContent(), options: [.default]) {
[weak self] (_, _) in
if let _ = self {
// Optional: you can change font-size with a value of percent here
markView.setFontSize(percent: 128)
printLog("load finish!")
}
}
3. 选项
您可以将不同的选项传递给底层的cmark
库。默认情况下,传递了safe
选项。
可用的选项有
- 默认
- sourcePos
- hardBreaks
- safe
- noBreaks
- validateUTF8
- smart
- githubPreLang
- liberalHtmlTag
有关可用选项的更多信息,请参阅cmark
。
作者
EyreFree,[email protected]
授权协议
EFMarkdown在WTFPL授权下可用。有关更多信息,请参阅LICENSE文件。