Swiftpack.co - smittytone/HighlighterSwift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by smittytone.
smittytone/HighlighterSwift 1.1.0
macOS, iOS and tvOS code syntax highlighter
⭐️ 1
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/smittytone/HighlighterSwift.git", from: "1.1.0")

HighlighterSwift 1.1.0

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

Far theme example

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

Improvements and Changes

Highlightr makes use of Highlight.js 9.13.4, but the most recent release of the JavaScript library in June 2021, when HighlighterSwift was developed, was version 11.0.1. The use of Highlight.js 9.x is no longer supported or recommended by the Hightlight.js team. Version 1.0.x of HighlighterSwift made use of the stable Highlight.js 10.7.3. Version 1.1.0 moves to Highlight.js 11.5.0.

HighlighterSwift adds support for alpha values in CSS colours, eg. #808080AA, not present in Highlightr.

Unlike Highlightr, HighlighterSwift parses Highlight.js themes for separate declarations of the same style. For example, Hybrid contains the following CSS:

.hljs{display:block;overflow-x:auto;padding:.5em;background:#1d1f21}.hljs span::selection,.hljs::selection{background:#373b41}.hljs{color:#c5c8c6}

The hljs.color attribute is added to hljs.display, hljs.overflow-x, hljs.padding and hljs.background, it doesn’t replace them.

HighlighterSwift was designed from the ground up as a Swift Package. Support for legacy package managers is not included. Highlightr supprts CocoaPods and Carthage.

HighlighterSwift is more deeply commented and the code is presented in a more consistent style.

A number of functions have been given extra parameters, primarily to add font selection when setting themes and initiating Theme objects. Redundant code has been removed. Some parameters have been renamed.

Unit tests have been added, and more will come, I hope.

Far theme example

Why not update Highlightr?

HighlighterSwift was created to meet the needs of a specific project, which was originally conceived with a modified version Hightlightr in mind. Some of the changes listed above are breaking, and so I feel it’s not appropriate to just inflict them on the Hightlightr source, especially when there are many outstanding pull requests yet to be addressed. But I’m not opposed to pulling in my changes if the community requests that.

HighlighterSwift is released under the same licence as Highlightr, allowing developers to select either, both or a mix of the two.

Platform Support

HighlighterSwift supports macOS 10.14 and up, and iOS 12 and up. iOS support is untested, however.


To add HighlighterSwift to your project, use Xcode to add it as a Swift Package at this repo’s URL. The library 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 (Highlight.js is also starting to do this); the two Kimbies have been renamed for consistency; colours have been formalised as hex values.


Instantiate a *Highlighter- object. Its *init()- function returns an optional, which will be nil if the Highlight.min.js 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:


You can apply your chosen font at this time too rather than fall back on 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: 12.0)!

Finally, get an optional NSAttributedString containing the formatted code:

if let displayString: NSAttributedString = highlighter.highlight(codeString, as: "swift") {

Far theme example

The second parameter 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.

Release Notes

  • 1.1.0 26 April 2022
    • Update to Highlight.js 11.5.0.
    • Include all Highlight.js languages.
  • 1.0.1 23 July 2021
    • Correct list of available themes in package.swift.
  • 1.0.0 15 July 2021
    • Initial public release.


HighlighterSwift, like Highlightr before it, is released under the terms of the MIT Licence. Hightlight.js is released under the BSD 3-Clause Licence.

HighlighterSwift is © 2022, Tony Smith. Portions are © 2016, Juan Pablo Illanes. Other portions are © 2006-2022, Ivan Sagalaev.


Stars: 1
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

3 weeks ago

Please see the release notes.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics