HighlightSwift

1.0.9

Code syntax highlighting in Swift and SwiftUI
appstefan/HighlightSwift

What's New

2024-06-13T20:50:13Z

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 built-in color themes, or use your own CSS
  • 🧰 Built with highlight.js and JavaScriptCore
  • 🖥️ Supported on iOS, iPadOS, macOS, and tvOS

CodeText

Display syntax highlighted code just like a standard Text view

  • 🎨 HighlightSwift
  • 🔠 Supports most Text modifiers like .font()
  • 🌗 Color theme syncs automatically with Dark Mode
  • 🟩 Optional card style adds the theme background color
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 request function returns a HighlightResult struct. This result struct includes details such as the detected language along with the attributed text:

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\")")

The default style is .plain with no background or padding. Use the customizable .card style to show the theme background color:

CodeText("print(\"Hello World\")")
    .codeTextStyle(.card(cornerRadius: 5))

Apply standard Text modifiers like .font():

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

Add the .codeTextColors(_:) modifier to set the color theme. The built-in color themes update automatically with Dark Mode to the corresponding dark variant.

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

For more control, use any custom CSS theme 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 set a specific language and disable automatic detection:

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

Add the .onHighlight(_:) modifier to get the detected language, background color and other details:

var body: some View {
    CodeText("print(\"Hello World\")")
        .onHighlight { result in
            //  ...
        }
}

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.10.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Nov 20 2024 22:31:34 GMT-1000 (Hawaii-Aleutian Standard Time)