HighlighterSwift

A Swift wrapper for the popular Highlight.js syntax highlighter

Latest Release: 1.0.0

   New  

 

This library provides a Swift wrapper for the popular Highlight.js code highlighting utility.

It is a more up-to-date version of Juan Pablo Illanes’ Highlightr library and relies heavily upon code from that project, which is now no longer maintained.

Various PreviewCode themes

How to use HighlighterSwift

Installation

To add HighlighterSwift to your project, use Xcode’s File > Swift Packages menu command. When requested, enter this repo’s git URL, https://github.com/smittytone/HighlighterSwift.git.

The library already contains the Highlight.js code and themes.

Note This project was begun to support another, so some themes have been modified slightly to meet the needs of that other project. For example, background images have been removed from the Brown Paper, Greyscale, Schoolbook and Pojoacque themes; the two Kimbie themes have been renamed for consistency; and colours have been formalised as hex values.

Usage

Make sure Highlighter is listed under Frameworks, Libraries, and Embedded Content under your target’s General settings.

Include import Hightlighter at the top of your source code.

Instantiate a Highlighter object. Its init() function returns an optional, which will be nil if the Highlight.min.js file could not be found or is non-functional, or the Default theme CSS file is missing:

if let highlighter: Highlighter = Highlighter.init() {
    ...
}

You can set a specific theme using the setTheme() function:

highlighter.setTheme("atom-one-light")

You can also apply your chosen font at this time too, or use the default: 14pt Courier.

highlighter.setTheme("atom-one-light",
                     withFont: "Menlo-Regular",
                     ofSize: 16.0)

You can set or change your preferred font later by using setCodeFont(), which takes an NSFont or UIFont instance configured for the font and text size you want, and is called on the Highlighter instance’s theme property:

let font: NSFont = NSFont.init(name: "Menlo-Regular",
                               size: 16.0)!
highlighter.theme.setCodeFont(font)

Finally, get an optional NSAttributedString containing the formatted code:

if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift") {
    myTextView.textStorage!.addAttributedString(displayString)
}

The second parameter of highlight() is the name of language you’re rendering. If you leave out this parameter, or pass nil, Highlighter will use Highlight.js’ language detection feature.

You can get a list of supported languages by the name they are known to Highlight.js by calling supportedLanguages() — it returns an array of strings.

The function availableThemes() returns a list of the installed themes.

Acknowledgements

PreviewCode makes use of code from the following sources:


Source Code

You can view HighlighterSwift’s source code at GitHub.


Release Notes

  • 1.0.0 17 June 2021
    • Initial public release.

Related Software


The Seal of RassilonThe Seal of Rassilon
Site and software copyright © 2021, Tony Smith