wireapp/Down

Name: Down

Owner: Wire Swiss GmbH

Description: Blazing fast Markdown rendering in Swift, built upon cmark.

Forked from: iwasrobbed/Down

Created: 2018-01-31 08:28:39.0

Updated: 2018-04-11 14:08:48.0

Pushed: 2018-04-11 14:15:19.0

Homepage: null

Size: 2302

Language: C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Down

Build Status MIT licensed CocoaPods Swift 4 macOS iOS tvOS Coverage Status

Blazing fast Markdown rendering in Swift, built upon cmark.

Is your app using it? Let us know!

Maintainers
Installation

Note: Swift 4 support is now on the master branch and any tag >= 0.4.x (Swift 3 is 0.3.x)

Quickly install using CocoaPods:

'Down'

Or Carthage:

ub "iwasrobbed/Down"

Due to limitations in Carthage regarding platform specification, you need to define the platform with Carthage.

e.g.

anually install:

lone this repository
uild the Down project
dd the resulting framework file to your project

rofit

Robust Performance

ark](https://github.com/jgm/cmark) can render a Markdown version of War and Peace in the blink of an eye (127 milliseconds on a ten year old laptop, vs. 100-400 milliseconds for an eye blink). In our [benchmarks](https://github.com/jgm/cmark/blob/master/benchmarks.md), cmark is 10,000 times faster than the original Markdown.pl, and on par with the very fastest available Markdown processors.

e library has been extensively fuzz-tested using [american fuzzy lop](http://lcamtuf.coredump.cx/afl). The test suite includes pathological cases that bring many other Markdown parsers to a crawl (for example, thousands-deep nested bracketed text or block quotes).

Output Formats
b View (see DownView class)
ML
L
TeX
off man
mmonMark Markdown
AttributedString
T (abstract syntax tree)

View Rendering

`DownView` class offers a very simple way to parse a UTF-8 encoded string with Markdown and convert it to a web view that can be added to any view:

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“) {

// Optional callback for loading finished

}

 example of rendering this README:

ample gif](Images/ohhai.gif)

# Prevent zoom
default implementation of the `DownView` allows for zooming in the rendered content. If you want to disable this, then you?ll need to instantiate the `DownView` with a custom bundle where the `viewport` in `index.html` has been assigned `user-scalable=no`. More info can be found [here](https://github.com/iwasrobbed/Down/pull/30).

Parsing API

`Down` struct has everything you need if you just want out-of-the-box setup for parsing and conversion. 

let down = Down(markdownString: “## Down“)

// Convert to HTML let html = try? down.toHTML() // “

<a href="https://github.com/iwasrobbed/Down">Down

\n”

// Convert to XML let xml = try? down.toXML() // “<?xml version="1.0" encoding="UTF-8"?>\n\n<document xmlns="http://commonmark.org/xml/1.0">\n <heading level="2">\n <link destination="https://github.com/iwasrobbed/Down" title="">\n Down\n \n \n\n”

// Convert to groff man let man = try? down.toGroff() // “.SS\nDown (https://github.com/iwasrobbed/Down)\n”

// Convert to LaTeX let latex = try? down.toLaTeX() // “\subsection{\href{https://github.com/iwasrobbed/Down}{Down}}\n”

// Convert to CommonMark Markdown let commonMark = try? down.toCommonMark() // “## Down\n”

// Convert to an attributed string let attributedString = try? down.toAttributedString() // NSAttributedString representation of the rendered HTML

// Convert to abstract syntax tree let ast = try? down.toAST() // Returns pointer to AST that you can manipulate

Rendering Granularity

ou'd like more granularity for the output types you want to support, you can create your own struct conforming to at least one of the renderable protocols:

wnHTMLRenderable
wnXMLRenderable
wnLaTeXRenderable
wnGroffRenderable
wnCommonMarkRenderable
wnASTRenderable
wnAttributedStringRenderable

ple:

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
}

}

Options

 protocol has options that will influence either rendering or parsing:

/* 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. / public static let Safe = DownOptions(rawValue: 1 « 3)

// 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)

Supports
t, ARC & iOS 9+

Markdown Specification

 is built upon the [CommonMark](http://commonmark.org) specification.

A little help from my friends
se feel free to fork and create a pull request for bug fixes or improvements, being sure to maintain the general coding style, adding tests, and adding comments as necessary.

Credit
 library is a wrapper around [cmark](https://github.com/jgm/cmark), which is built upon the [CommonMark](http://commonmark.org) Markdown specification. 

rk](https://github.com/jgm/cmark) is Copyright (c) 2014 - 2017, John MacFarlane. View [full license](https://github.com/jgm/cmark/blob/master/COPYING).

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.