buzzfeed/CocoaMarkdown

Name: CocoaMarkdown

Owner: BuzzFeed

Description: Markdown parsing and rendering for iOS and OS X

Created: 2016-02-16 20:15:47.0

Updated: 2017-07-25 16:09:48.0

Pushed: 2017-09-06 19:34:50.0

Homepage:

Size: 1180

Language: Objective-C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

CocoaMarkdown
Markdown parsing and rendering for iOS and OS X

CocoaMarkdown is a cross-platform framework for parsing and rendering Markdown, built on top of the C reference implementation of CommonMark.

This is currently beta-quality code.

Why?

CocoaMarkdown aims to solve two primary problems better than existing libraries:

  1. More flexibility. CocoaMarkdown allows you to define custom parsing hooks or even traverse the Markdown AST using the low-level API.
  2. Efficient NSAttributedString creation for easy rendering on iOS and OS X. Most existing libraries just generate HTML from the Markdown, which is not a convenient representation to work with in native apps.
Installation

First you will want to add this project as a submodule to your project:

submodule add https://github.com/indragiek/CocoaMarkdown.git

Then, you need to pull down all of its dependencies.

ocoaMarkdown
submodule update --init --recursive

Next, drag the .xcodeproj file from within CocoaMarkdown into your project. After that, click on the General tab of your target. Select the plus button under “Embedded Binaries” and select the CocoaMarkdown.framework.

API
Traversing the Markdown AST

CMNode and CMIterator wrap CommonMark's C types with an object-oriented interface for traversal of the Markdown AST.

document = CMDocument(contentsOfFile: path, options: nil)
ment.rootNode.iterator().enumerateUsingBlock { (node, _, _) in
print("String value: \(node.stringValue)")

Building Custom Renderers

The CMParser class isn't really a parser (it just traverses the AST), but it defines an NSXMLParser-style delegate API that provides handy callbacks for building your own renderers:

tocol CMParserDelegate <NSObject>
ional
oid)parserDidStartDocument:(CMParser *)parser;
oid)parserDidEndDocument:(CMParser *)parser;

oid)parser:(CMParser *)parser foundText:(NSString *)text;
oid)parserFoundHRule:(CMParser *)parser;


CMAttributedStringRenderer is an example of a custom renderer that is built using this API.

Rendering Attributed Strings

CMAttributedStringRenderer is the high level API that will be useful to most apps. It creates an NSAttributedString directly from Markdown, skipping the step of converting it to HTML altogether.

Going from a Markdown document to rendering it on screen is as easy as:

document = CMDocument(contentsOfFile: path, options: nil)
renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
View.attributedText = renderer.render()

Or, using the convenience method on CMDocument:

View.attributedText = CMDocument(contentsOfFile: path, options: nil).attributedStringWithAttributes(CMTextAttributes())

All attributes used to style the text are customizable using the CMTextAttributes class:

attributes = CMTextAttributes()
ibutes.linkAttributes = [
NSForegroundColorAttributeName: UIColor.redColor()

ibutes.emphasisAttributes = [
NSBackgroundColorAttributeName: UIColor.yellowColor()

HTML elements can be supported by implementing CMHTMLElementTransformer. The framework includes several transformers for commonly used tags:

Transformers can be registered with the renderer to use them:

document = CMDocument(contentsOfFile: path, options: nil)
renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
erer.registerHTMLElementTransformer(CMHTMLStrikethroughTransformer())
erer.registerHTMLElementTransformer(CMHTMLSuperscriptTransformer())
View.attributedText = renderer.render()
Rendering HTML

CMHTMLRenderer provides the ability to render HTML from Markdown:

document = CMDocument(contentsOfFile: path, options: nil)
renderer = CMHTMLRenderer(document: document)
HTML = renderer.render()

Or, using the convenience method on CMDocument:

HTML = CMDocument(contentsOfFile: path).HTMLString()
Example Apps

The project includes example apps for iOS and OS X to demonstrate rendering attributed strings.

Contact
License

CocoaMarkdown is licensed under the MIT License. See LICENSE for more information.


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.