Down
Swift中快速的Markdown(CommonMark)解析,基于 cmark v0.29.0 构建。
您的应用在使用它吗?告诉我们吧!
如果您在寻找 iwasrobbed/Down
,您找到了!Rob Phillips,此存储库的创始人,已将其转让给我,因为我将作为主要的维护者。感谢Rob为Down带来的所有发展,并将其托付给我来照顾。
所有现有的iwasrobbed/Down
引用都应重定向到此存储库。但是,建议您更新这些url,使其指向此存储库。
维护者
安装
注意:Swift的支持总结在下面的表中。
Swift版本 | 标签 |
---|---|
Swift 5.1 | >= 0.9.0 |
Swift 5.0 | >= 0.8.1 |
Swift 4 | >= 0.4.x |
Swift 3 | 0.3.x |
现在在master
分支上,并且任何版本号>= 0.8.1的标签(Swift 4是>= 0.4.x,Swift 3是0.3.x)
CocoaPods快速安装
使用pod 'Down'
Carthage安装
使用github "johnxnguyen/Down"
由于Carthage在平台指定方面的限制,您需要使用Carthage定义平台。
例如:
carthage update --platform iOS
Swift Package Manager安装
使用要将Down添加到您的项目中,选择文件 → Swift 包 → 添加包依赖项
并输入Down的GitHub URL。请参阅将包依赖项添加到您的应用程序以获取详细说明。
或手动安装
- 克隆此仓库
- 将Down项目拖到您的项目文件中,在嵌入的框架部分添加框架
- 构建并运行您的应用程序
- ?
- 盈利
强大的性能
cmark可以在一瞬间(十年老笔记本电脑上的127毫秒,而眨眼时间为100-400毫秒)渲染《战争与和平》的Markdown版本。在我们的基准测试中,cmark比原始的Markdown.pl快10,000倍,并且与最快可用的Markdown处理器相当。
该库已通过american fuzzy lop进行了广泛的模糊测试。测试套件包括可能导致其他Markdown解析器崩溃的病理情况(例如,深度达数千层的嵌套括号文本或区块引用)。
输出格式
- 网页视图(请参阅DownView类)
- HTML
- XML
- LaTeX
- groff手册
- CommonMark Markdown
- NSAttributedString
- AST(抽象语法树)
视图渲染
DownView
类提供了一种非常简单的方法来解析UTF-8编码的字符串并以Markdown格式转换为可以添加到任何视图的网页视图
let downView = try? DownView(frame: self.view.bounds, markdownString: "**Oh Hai**") {
// Optional callback for loading finished
}
// Now add to view or constrain w/ Autolayout
// Or you could optionally update the contents at some point:
try? downView?.update(markdownString: "## [Google](https://google.com)") {
// Optional callback for loading finished
}
渲染此README的元示例
解析API
如果您只需要默认设置来进行解析和转换,那么Down
结构体拥有您所需的一切。
let down = Down(markdownString: "## [Down](https://github.com/johnxnguyen/Down)")
// Convert to HTML
let html = try? down.toHTML()
// "<h2><a href=\"https://github.com/johnxnguyen/Down\">Down</a></h2>\n"
// Convert to XML
let xml = try? down.toXML()
// "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <heading level=\"2\">\n <link destination=\"https://github.com/johnxnguyen/Down\" title=\"\">\n <text>Down</text>\n </link>\n </heading>\n</document>\n"
// Convert to groff man
let man = try? down.toGroff()
// ".SS\nDown (https://github.com/johnxnguyen/Down)\n"
// Convert to LaTeX
let latex = try? down.toLaTeX()
// "\\subsection{\\href{https://github.com/johnxnguyen/Down}{Down}}\n"
// Convert to CommonMark Markdown
let commonMark = try? down.toCommonMark()
// "## [Down](https://github.com/johnxnguyen/Down)\n"
// Convert to an attributed string
let attributedString = try? down.toAttributedString()
// NSAttributedString representation of the rendered HTML;
// by default, uses a stylesheet that matches NSAttributedString's default font,
// but you can override this by passing in your own, using the 'stylesheet:' parameter.
// Convert to abstract syntax tree
let ast = try? down.toAST()
// Returns pointer to AST that you can manipulate
渲染粒度
如果您想为支持输出的类型提供更多粒度,您可以创建自己的结构体,使其至少符合一个可渲染协议。
- DownHTMLRenderable
- DownXMLRenderable
- DownLaTeXRenderable
- DownGroffRenderable
- DownCommonMarkRenderable
- DownASTRenderable
- DownAttributedStringRenderable
示例
public struct MarkdownToHTML: DownHTMLRenderable {
/**
A string containing CommonMark Markdown
*/
public var markdownString: String
/**
Initializes the container with a CommonMark Markdown string which can then be rendered as HTML using `toHTML()`
- parameter markdownString: A string containing CommonMark Markdown
- returns: An instance of Self
*/
@warn_unused_result
public init(markdownString: String) {
self.markdownString = markdownString
}
}
DownView
配置
DownView
可以使用自定义包进行配置,使用您自己的HTML / CSS或做些类似支持动态类型或自定义字体等事情。它是完全可配置的。
此选项可以在DownView的实例化函数中找到。
防止缩放
DownView
的默认实现允许对渲染内容进行缩放。如果您想禁用此功能,则需要在 index.html
中的 viewport
被分配 user-scalable=no
的自定义包中实例化 DownView
。更多信息请参阅 这里。
选项
每个协议都有会影响渲染或解析的选项。
/**
Default options
*/
public static let `default` = DownOptions(rawValue: 0)
// MARK: - Rendering Options
/**
Include a `data-sourcepos` attribute on all block elements
*/
public static let sourcePos = DownOptions(rawValue: 1 << 1)
/**
Render `softbreak` elements as hard line breaks.
*/
public static let hardBreaks = DownOptions(rawValue: 1 << 2)
/**
Suppress raw HTML and unsafe links (`javascript:`, `vbscript:`,
`file:`, and `data:`, except for `image/png`, `image/gif`,
`image/jpeg`, or `image/webp` mime types). Raw HTML is replaced
by a placeholder HTML comment. Unsafe links are replaced by
empty strings. Note that this option is provided for backwards
compatibility, but safe mode is now the default.
*/
public static let safe = DownOptions(rawValue: 1 << 3)
/**
Allow raw HTML and unsafe links. Note that safe mode is now
the default, and the unsafe option must be used if rendering
of raw HTML and unsafe links is desired.
*/
public static let unsafe = DownOptions(rawValue: 1 << 17)
// MARK: - Parsing Options
/**
Normalize tree by consolidating adjacent text nodes.
*/
public static let normalize = DownOptions(rawValue: 1 << 4)
/**
Validate UTF-8 in the input before parsing, replacing illegal
sequences with the replacement character U+FFFD.
*/
public static let validateUTF8 = DownOptions(rawValue: 1 << 5)
/**
Convert straight quotes to curly, --- to em dashes, -- to en dashes.
*/
public static let smart = DownOptions(rawValue: 1 << 6)
/**
Combine smart typography with HTML rendering.
*/
public static let smartUnsaFe = DownOptions(rawValue: (1 << 17) + (1 << 6))
支持的
Swift;iOS 9+、tvOS 9+、macOS 10.11+
Markdown 规范
Down 构建在 CommonMark 规范之上。
朋友的一点点帮助
请随意进行分支操作并提交错误修复或改进的拉取请求,确保维护一致的编码风格,添加必要的测试和注释。
致谢
这个库是围绕 cmark 的包装,它基于 CommonMark Markdown 规范构建。