HighlightSwift

1.0.8

Code syntax highlighting in Swift and SwiftUI
appstefan/HighlightSwift

What's New

1.0.8

2024-05-11T18:03:01Z

HighlightSwift 🎨

Syntax Highlighting in Swift and SwiftUI

CodeCardDemo

Contents

Highlight

Convert any String of code into a syntax highlighted AttributedString

  • 🔍 Automatic language detection
  • 📚 Works with 50 common languages
  • 🌈 Choose from 30 classic color themes
  • 🧰 Built with highlight.js and JavaScriptCore
  • 🖥️ Supported on iOS, iPadOS, macOS, and tvOS

CodeText

Drop-in replacement for the SwiftUI Text view

  • 🔠 Supports most Text modifiers like .font()
  • 🌗 Color theme syncs automatically with Dark Mode
CodeText

Highlight

Create an instance of the Highlight class:

@State var highlight = Highlight()

Convert a String of code into a syntax highlighted AttributedString:

let attributedText = try await highlight.attributedText("print(\"Hello World\")")

Providing the language: parameter disables automatic language detection:

let attributedText = try await highlight.attributedText(code, language: "swift")

Set the colors: parameter to choose the highlight color theme.

let attributedText = try await highlight.attributedText(code, colors: .dark(.github))

Or use any custom CSS theme with the .custom option. Refer to the highlight.js Theme Guide for more info.

let attributedText = try await highlight.attributedText(code, colors: .custom(css: someCoolCSS))

The somewhat less convenient request function returns a HighlightResult struct. It includes the attributed text along with the detected language and other details:

let result: HighlightResult = try await highlight.request("print(\"Hello World\")")

//   HighlightResult(
//      attributedText: "...",
//      relevance: 5,
//      language: "swift",
//      languageName: "Swift?",
//      backgroundColor: #1F2024FF,
//      hasIllegal: false,
//      isUndefined: false
//  )

CodeText

Create a CodeText view:

CodeText("print(\"Hello World\")")

It has a themed background color by default for legibility. This can be disabled by setting showBackground to false:

CodeText("print(\"Hello World\")", showBackground: false)

Standard Text modifiers like .font() work as expected:

CodeText("print(\"Hello World\")")
    .font(.system(.callout, weight: .semibold))

The .codeTextColors(_:) modifier sets one of the color themes. The dark color scheme variant of each theme is applied automatically in Dark Mode.

CodeText("print(\"Hello World\")")
    .codeTextColors(.github)

Or use any custom CSS themes with the .custom color option. Refer to the official highlight.js Theme Guide for more info.

CodeText("print(\"Hello World\")")
    .codeTextColors(.custom(dark: .custom(css: someDarkCSS), light: .custom(css: someLightCSS)))

Use the .codeTextLanguage(_:) modifier to disable automatic language detection:

CodeText("print(\"Hello World\")")
    .codeTextLanguage(.swift)

Optionally, read the result binding to get the detected language, background color and other details:

@Binding var result: HighlightResult?

var body: some View {
    CodeText("print(\"Hello World\")", result: $result)
        .onChange(of: result) { _, newResult in
            // Use result
        }
}

Installation

Project

  1. In Xcode, go to File > Add packages...
  2. Enter https://github.com/appstefan/highlightswift in the field and click Add Package

Package

In Package.swift add this repository as a dependency:

dependencies: [
    .package(url: "https://github.com/appstefan/highlightswift.git", from: "1.0.0")
],
targets: [
    .target(
        name: "YourPackageName",
        dependencies: ["HighlightSwift"]
    )
]

Author

Stefan, thrower_ranges.0d@icloud.com

License

HighlightSwift is available under the MIT license. See the LICENSE.md file. Highlight.js is available under the BSD license. See the LICENSE.md file.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon May 27 2024 00:20:39 GMT-0900 (Hawaii-Aleutian Daylight Time)