Skip to content
forked from johnxnguyen/Down

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.

License

Notifications You must be signed in to change notification settings

withDouble/Down

 
 

Repository files navigation

Down

Build Status MIT licensed CocoaPods Swift 5 macOS iOS tvOS Linux Code Coverage

Blazing fast Markdown (CommonMark) rendering in Swift, built upon cmark v0.29.0.

Is your app using it? Let us know!

If you're looking for iwasrobbed/Down, you found it! Rob Phillips, the originator of this repository, has transferred it to me as I will be the primary maintainer from now on. Thanks to Rob for bringing Down as far as it has come and for entrusting me with its care.

All existing references to iwasrobbed/Down should redirect to this repository. However, It is recommended to update those urls to point to this repository.

Maintainers

Installation

Note: Swift support is summarized in the table below.

Swift Version Tag
Swift 5.1 >= 0.9.0
Swift 5.0 >= 0.8.1
Swift 4 >= 0.4.x
Swift 3 0.3.x

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

Quickly install using CocoaPods:

pod 'Down'

Install using Carthage:

github "johnxnguyen/Down"

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

e.g.

carthage update --platform iOS

Install using Swift Package Manager:

To add Down to your project, select File → Swift Packages → Add Package Dependency and enter the GitHub URL for Down. See Adding Package Dependencies to Your App for detailed instructions.

Or manually install:

  1. Clone this repository
  2. Drag and drop the Down project into your workspace file, adding the framework in the embedded framework section
  3. Build and run your app
  4. ?
  5. Profit

Robust Performance

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, cmark is 10,000 times faster than the original Markdown.pl, and on par with the very fastest available Markdown processors.

The library has been extensively fuzz-tested using american fuzzy lop. 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

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

View Rendering

The 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](https://google.com)") {
    // Optional callback for loading finished
}

Meta example of rendering this README:

Example gif

Parsing API

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

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

Rendering Granularity

If you'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:

  • DownHTMLRenderable
  • DownXMLRenderable
  • DownLaTeXRenderable
  • DownGroffRenderable
  • DownCommonMarkRenderable
  • DownASTRenderable
  • DownAttributedStringRenderable

Example:

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

Configuration of DownView

DownView can be configured with a custom bundle using your own HTML / CSS or to do things like supporting Dynamic Type or custom fonts, etc. It's completely configurable.

This option can be found in DownView's instantiation function.

Prevent zoom

The 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.

Options

Each 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. 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))

Supports

Swift; iOS 9+, tvOS 9+, macOS 10.11+

Markdown Specification

Down is built upon the CommonMark specification.

A little help from my friends

Please 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

This library is a wrapper around cmark, which is built upon the CommonMark Markdown specification.

cmark is Copyright (c) 2014, John MacFarlane. View full license.

About

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 56.0%
  • Swift 22.2%
  • C++ 21.5%
  • HTML 0.2%
  • Ruby 0.1%
  • Shell 0.0%