Skip to content

mortenjust/CodeHighlighter

Repository files navigation

CodeHighlighter

Forked Highlightr.

A SwiftUI view that automatically highlights code syntax using Highlightr, which is using highlight.js as it core, and supports 185 languages and comes with 89 styles.

Highlightr renders your syntax colored code as a string, no web views involved. This means your SwiftUI layout behaves like you'd expect it to, automatically reporting its frame, and no embedded scroll views.

Since we're using AttributedString, this package requires macOS 12 or iOS 15.

This package adds

  • SwiftUI view for syntax coloring
  • Safe selection of themes via enum
  • Dark mode detection

Installation

Add this repo's URL to your project's package dependencies: https://github.com/mortenjust/CodeHighlighter

Usage

CodeTextView("let a = b(\"Here we are\");")

CodeTextView("console.log(\"hello highlight\");",
             language: "javascript",
             lightTheme: .solarizedLight,
             darkTheme: .solarizedDark)

Contributions welcome

  • A CodeTextEditor, a text editor that lets you edit text with realtime highlighting. Highlightr offers CodeAttributedString, which is a subclass of NSTextStorage, so this could be implemented with a hosted UI/NSTextView.

License

MIT

About

A SwiftUI view for code syntax highlighting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages