Skip to content

DruideInformatiqueInc/Marklight

 
 

Repository files navigation

Marklight

Markdown syntax highlighter for iOS and macOS.

SwiftPM Swift macOS 11+ iOS 13+

Description

Marklight is a drop in component to easily add realtime Markdown syntax highlight on any user editable text view in iOS and macOS applications. Marklight doesn't include HTML generation from Markdown, but you can use one of the many other components available like Markingbird.

Regular expressions are taken from Markingbird, a Markdown parser and html generator.

Marklight GifScreenshot 0

Features

  • Applicable to any UITextView.
  • NSTextStorage subclass ready to use.
  • Struct optimized for performances.
  • Swift 5.x compatible.
  • Dynamic text style supported.
  • Choose markdown syntax color.
  • Choose font and color for code blocks.
  • Choose font and color for quotes.
  • Choose dynamic type font text style.
  • Quote indentation.
  • Documented.
  • macOS compatibility.
  • Parsing tests.
  • Performance tests.

Requirements

  • iOS 13.0+
  • Xcode 14.0+
  • macOS 11+

Installation

Swift Package Manager

let package = Package(
    name: "MyServer",
    dependencies: [
        .package(url: "https://github.com/DruideInformatiqueInc/Marklight.git", .upToNextMajor(from: "1.5.0"))
    ]
)

Usage

In this repository you can find a sample project with few lines of code in the ViewController class for a jumpstart.

Sample code is written in Swift but Objective-C should be supported too, if you find an incompatibility please open an issue.

Integration

The easiest way to crete a user editable UITextView with markdown syntax highlight is to use the provided MarklightTextStorage class as NSTextStorage and add the UITextView's textLayout to the MarklightTextStorage text storage.

Import Marklight modules into your Swift class

import Marklight

In your UIViewController subclass keep a strong instance of the this MarklightTextStorage class.

let textStorage = MarklightTextStorage()

Customize the appearance as desired:

  • Dynamic text style.
  • Markdown syntax color.
  • Code's font and color.
  • Quotes' font and color.

As per Apple's documentation it should be enough to assign the UITextView's NSLayoutManager to the NSTextStorage subclass, in our case MarklightTextStorage.

 textStorage.addLayoutManager(textView.layoutManager)

However I'm experiencing some crashes if I want to preload some text instead of letting the user start from scratch with a new text. A workaround is proposed below.

For simplicity we assume you have a String to be highlighted inside an editable UITextView loaded from a storyboard.

let string = "# My awesome markdown string"

Convert string to an NSAttributedString

let attributedString = NSAttributedString(string: string)

Set the loaded string to the UITextView

textView.attributedText = attributedString

Append the loaded string to the NSTextStorage

textStorage.appendAttributedString(attributedString)

Enjoy.

Acknowledgements


Marklight is heavily based on Markingbird, so many thanks to Kristopher Johnson and every previous contribution on which Markingbird is based upon.

License

Marklight is released under the MIT license. See LICENSE for details.

About

Markdown syntax highlighter for iOS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%