The Highlight class provides a way to convert a raw String of code into a syntax-highlighted AttributedString. It supports automatic language detection, 50+ languages, and 30+ built-in themes.
Basic Usage
let someCode = """
print("Hello World")
"""
let highlight = Highlight()
let attributedText = try await highlight.attributedText(someCode)
Customizing Language and Themes
- Set Language: Use the
language: parameter to disable automatic detection and force a specific language. - Set Theme: Use the
colors: parameter. You can use .dark(.themeName) or provide custom CSS via .custom(css: "...") following the highlight.js Theme Guide.
Fetching Detailed Results
Use .request() to get a HighlightResult object containing metadata like relevance score, detected language, and background color.
// Basic conversion
let highlight = Highlight()
let attributedText = try await highlight.attributedText(someCode)
// Forced language
let attributedText = try await highlight.attributedText(someCode, language: "swift")
// Custom theme
let attributedText = try await highlight.attributedText(someCode, colors: .dark(.github))
// Custom CSS
let attributedText = try await highlight.attributedText(someCode, colors: .custom(css: someCSS))
// Detailed metadata
let result: HighlightResult = try await highlight.request(someCode)