MarkdownKit Documentation

repository·master·Indexed 21 days ago

https://github.com/bmoliveira/markdownkit

A customizable and extensible Markdown parser for iOS and macOS that converts Markdown text into NSAttributedString. It supports standard elements like emphasis, headers, quotes, lists, code, and links, and allows for custom elements via the MarkdownElement protocol.

Tokens
1.2K
Snippets
6
Records
7
Agent score
25%

What's inside MarkdownKit

  1. Extend MarkdownKit with custom elements

    master

    To add new Markdown elements, implement the MarkdownElement protocol (or its descendants like MarkdownLink) and provide a regular expression. You then pass these custom elements into the MarkdownParser initializer via the customElements parameter.

    Example of creating a custom subreddit link parser:

    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)
      }
    }
    
    // Usage
    let markdownParser = MarkdownParser(customElements: [MarkdownSubreddit()])
    let markdown = "**/r/iosprogramming** can be *markdown* as well!"
    label.attributedText = markdownParser.parse(markdown)
  2. Customize MarkdownKit attributes and elements

    master

    You can customize the appearance of Markdown elements (like bold, italic, and headers) and control which elements are enabled.

    Key customization properties include:

    • font: Sets the base font for the parser.
    • enabledElements: A set used to enable or disable specific Markdown features (e.g., .disabledAutomaticLink).
    • bold.color: Sets the color for bold text.
    • italic.font: Sets the font for italic text.
    • header.fontIncrease: An integer determining how much the font size increases for headers.
    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
  3. Parse Markdown into NSAttributedString

    master

    To transform Markdown text into an NSAttributedString, create an instance of MarkdownParser and call the parse(_:) method. The resulting attributed string can be assigned directly to a UILabel or similar text component.

    let markdownParser = MarkdownParser()
    let markdown = "I support a *lot* of custom Markdown **Elements**, even `code`!"
    label.attributedText = markdownParser.parse(markdown)
  4. Supported Markdown elements

    master

    MarkdownKit supports the following standard elements via Regular Expressions:

    • Emphasis: *italic* or _italics_, **bold** or __bold__, ~~strikethrough~~
    • Headers: # H1 through ###### H6
    • Quotes: > Quote
    • Lists: * List, - List, + List
    • Code: `code` or ```code```
    • Links: [Links](url)