XUtils

MarkdownKit

A simple and customizable Markdown Parser.


Header 1

Header 2

Header 3

Header 4

Header 5
Header 6

Quote

  • List
  • List
  • List

code or code Links


## Customization

```swift
let markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 18))
markdownParser.enabledElements = .disabledAutomaticLink
markdownParser.bold.color = UIColor.red
markdownParser.italic.font = UIFont.italicSystemFont(ofSize: 300)
markdownParser.header.fontIncrease = 4

Extensibility

To add new Markdown elements all you have to do is implement the MarkdownElement protocol (or descendants) and add it to the MarkdownParser.

import MarkdownKit

class MarkdownSubreddit: MarkdownLink {

  private static let regex = "(^|\\s|\\W)(/?r/(\\w+)/?)"

  override var regex: String {
    return MarkdownSubreddit.regex
  }

  override func match(match: NSTextCheckingResult,
                             attributedString: NSMutableAttributedString) {
    let subredditName = attributedString.attributedSubstringFromRange(match.rangeAtIndex(3)).string
    let linkURLString = "http://reddit.com/r/\(subredditName)"
    formatText(attributedString, range: match.range, link: linkURLString)
    addAttributes(attributedString, range: match.range, link: linkURLString)
  }

}
let markdownParser = MarkdownParser(customElements: [MarkdownSubreddit()])
let markdown = "**/r/iosprogramming** can be *markdown* as well!"
label.attributedText = markdownParser.parse(markdown)

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Acknowledgements

This library is heavily inspired in TSMarkdownParser and also SwiftyMarkdown.

Special thanks to Michael Brown for helping out with the UTF-16 Escaping/Unescaping.


Articles

  • coming soon...