Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
appstefan committed Jun 25, 2024
1 parent 49a85e0 commit 784ca3c
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Swift class to convert a `String` of code into a syntax highlighted `AttributedS
* 🔍 Automatic language detection
* 📚 Support for 50+ common languages
* 🌈 Choose from 30 built-in color themes or use custom CSS
* 🧰 Built with [highlight.js](https://github.com/highlightjs/highlight.js) and `JavaScriptCore`
* 🧰 Built with [highlight.js](https://github.com/highlightjs/highlight.js) & `JavaScriptCore`
* ☑️ Complete concurrency checking enabled
* 🖥️ Works on iOS, iPadOS, macOS, and tvOS

Expand All @@ -29,8 +29,9 @@ SwiftUI view to display a `String` of code with syntax highlighting
* 🌗 Color theme syncs automatically with Dark Mode
* 📜 Theme background color included with `.card` style
* 🔠 Works with `Text` modifiers like `.bold()` or `.font()`
* ⚙️ Includes modifiers to set the color theme, style and language
* 📫 Callback modifiers to get the highlight results
* ⚙️ Includes modifiers to set the color theme, style & language
* 📫 Callback modifiers to get the highlight results, language & score
* 🍃 Memory efficient using an internal `Highlight` environment entry

<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/appstefan/HighlightSwift/assets/6455394/5021a822-39f2-40bd-b1f8-2680c2382dd3">
Expand All @@ -49,7 +50,7 @@ let highlight = Highlight()
let attributedText = try await highlight.attributedText(someCode)
```

Add the `language:` parameter to bypass automatic language detection:
Add the `language:` parameter to set the language and disable automatic language detection:
```swift
let attributedText = try await highlight.attributedText(someCode, language: "swift")
```
Expand Down Expand Up @@ -103,48 +104,48 @@ var body: some View {
}
```

Add the `.codeTextColors(_:)` modifier to set the color theme.
The built-in color themes update automatically with Dark Mode to the corresponding dark variant.
The font design is always `.monospaced`.
Other text modifiers can be applied as usual:
```swift
CodeText(someCode)
.codeTextColors(.github)
.font(.callout)
.fontWeight(.semibold)
```

The default style is `.plain` without any background or padding.
Some of the color themes are more legible with their corresponding background color.
Add the `.codeTextStyle(_:)` modifier and choose the `.card` style to show the background:
Add the `.highlightLanguage(_:)` modifier to set the language and disable automatic detection:
```swift
CodeText(someCode)
.codeTextStyle(.card)
.highlightLanguage(.swift)
```

The `.card` style has a few customization options, for example:
#### Colors
Add the `.codeTextColors(_:)` modifier to set the color theme. The built-in color themes update automatically with Dark Mode to the corresponding dark variant.
```swift
CodeText(someCode)
.codeTextStyle(.card(cornerRadius: 0, stroke: .separator, verticalPadding: 12))
.codeTextColors(.github)
```

Adjust using standard `Text` modifiers like `.font()`:
Choose the `.custom` option to use any custom CSS color theme. Refer to the official highlight.js [Theme Guide](https://highlightjs.readthedocs.io/en/latest/theme-guide.html#) for more info.
```swift
CodeText(someCode)
.font(.callout)
.fontWeight(.semibold)
.codeTextColors(.custom(dark: .custom(css: someDarkCSS), light: .custom(css: someLightCSS)))
```

Choose the `.custom` option to use any custom CSS color theme.
Refer to the official highlight.js [Theme Guide](https://highlightjs.readthedocs.io/en/latest/theme-guide.html#) for more info.
#### Styles
The default style is `.plain` without any background or padding. Some of the color themes are more legible with their corresponding background color. Add the `.codeTextStyle(_:)` modifier and choose the `.card` style to show the background:
```swift
CodeText(someCode)
.codeTextColors(.custom(dark: .custom(css: someDarkCSS), light: .custom(css: someLightCSS)))
.codeTextStyle(.card)
```

Add the `.highlightLanguage(_:)` modifier to set a language and bypass automatic detection:
The `.card` style has a few customization options, for example:
```swift
CodeText(someCode)
.highlightLanguage(.swift)
.codeTextStyle(.card(cornerRadius: 0, stroke: .separator, verticalPadding: 12))
```

Add `.onHighlightSuccess(_:)` to get the highlight results, including the detected language, relevancy score, background color and other details. Errors are unlikely but can be handled with `.onHighlightFailure(_:)` if necessary.
#### Results
Add `.onHighlightSuccess(_:)` to get the highlight results, including the detected language, relevancy score, background color and other details. Unexpected errors are unlikely but can be handled with `.onHighlightFailure(_:)` if necessary for debugging.
```swift
CodeText(someCode)
.onHighlightSuccess { result in
Expand All @@ -155,24 +156,23 @@ CodeText(someCode)
}
```

Note that failing to match a language to the input is not considered a highlight failure.
Rather, the result will have `isUndefined` set to `true` and the language will be "unknown" with a relevance score of zero.

There is also a combined `.onHighlightResult(_:)` equivalent of the two callbacks above.
```swift
CodeText(someCode)
.onHighlightResult { result in
switch result {
case .success:
case .success:
...
case .failure:
...
}
}
```

A previously stored highlight result can also be passed to the `CodeText`.
This can help in places where it reappears often, such as in a list view.
It is notable that a failure to match a language to the input is actually not a highlight failure. Rather the result will have `isUndefined` set to `true` and the language will be "unknown" with a relevance score of zero.

A previously stored highlight result can be passed to the `CodeText`.
This in combination with `.onHighlightSuccess(_:)` enables persistence of the result when the view might reappear frequently, such as in a list view:
```swift
let someCode: String = """
print(\"Hello World\")
Expand Down Expand Up @@ -204,7 +204,7 @@ var body: some View {
In `Package.swift` add this repository as a dependency:
```swift
dependencies: [
.package(url: "https://github.com/appstefan/highlightswift.git", from: "1.0.0")
.package(url: "https://github.com/appstefan/highlightswift.git", from: "1.1")
],
targets: [
.target(
Expand Down

0 comments on commit 784ca3c

Please sign in to comment.