Swift SwiftUI 必备基础库之 02 Down 快速渲染Markdown

Swift SwiftUI 必备基础库之 02 Down 快速渲染Markdown

什么是Down

Down是Swift中的Markdown,CommonMark快速渲染组件。

Down 强劲的性能

cmark可以在眨眼之间呈现出Markdown版本的《战争与和平》(在一十年之久的笔记本电脑上为127毫秒,而眨眼为100-400毫秒)。在我们的基准测试中,cmark比原始Markdown.pl快10,000倍,与可用最快的Markdown处理器相当。

支持输出格式

  • Web View (see DownView class)
  • HTML
  • XML
  • LaTeX
  • groff man
  • CommonMark Markdown
  • NSAttributedString
  • AST (abstract syntax tree)

View查看渲染

DownView 类提供了一种非常简单的方法,可以使用标记解码解析 UTF-8 编码字符串,并将其转换为可添加到任何视图的 Web 视图:

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
}

image.png

解析API

如果您只想要开箱即用的解析和转换设置,则向下结构具有所需的一切。

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
    }
}

安装

https://github.com/johnxnguyen/Down.git

加入我们一起学习SwiftUI

QQ:3365059189
SwiftUI技术交流QQ群:518696470
教程网站:www.openswiftui.com

发表回复