{"id":50801767,"url":"https://github.com/appstefan/HighlightSwift","last_synced_at":"2026-06-30T03:00:37.847Z","repository":{"id":164621240,"uuid":"639499009","full_name":"appstefan/HighlightSwift","owner":"appstefan","description":"Code syntax highlighting in Swift and SwiftUI","archived":false,"fork":false,"pushed_at":"2024-11-27T17:42:48.000Z","size":776,"stargazers_count":202,"open_issues_count":9,"forks_count":37,"subscribers_count":6,"default_branch":"main","last_synced_at":"2026-05-29T00:28:06.356Z","etag":null,"topics":["ios","macos","swift","swiftui","syntax-highlighting","tvos"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/appstefan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-11T15:17:41.000Z","updated_at":"2026-05-04T16:13:11.000Z","dependencies_parsed_at":"2023-11-15T07:24:42.391Z","dependency_job_id":"822ac744-005e-4cde-9261-80a248341dc7","html_url":"https://github.com/appstefan/HighlightSwift","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/appstefan/HighlightSwift","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appstefan%2FHighlightSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appstefan%2FHighlightSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appstefan%2FHighlightSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appstefan%2FHighlightSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appstefan","download_url":"https://codeload.github.com/appstefan/HighlightSwift/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appstefan%2FHighlightSwift/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34950330,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-30T02:00:05.919Z","response_time":92,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","macos","swift","swiftui","syntax-highlighting","tvos"],"created_at":"2026-06-12T21:00:28.531Z","updated_at":"2026-06-30T03:00:37.838Z","avatar_url":"https://github.com/appstefan.png","language":"Swift","funding_links":[],"categories":["Utilities and Extensions"],"sub_categories":[],"readme":"# HighlightSwift\n\n![](https://img.shields.io/github/v/release/appstefan/highlightswift)\n![](https://img.shields.io/github/license/appstefan/highlightswift)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fappstefan%2FHighlightSwift%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/appstefan/HighlightSwift)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fappstefan%2FHighlightSwift%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/appstefan/HighlightSwift)\n\nSyntax Highlighting for Swift and SwiftUI\n\n\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://github.com/appstefan/HighlightSwift/assets/6455394/01155e77-1819-4ba7-bc95-1af82bde43d7\"\u003e\n  \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://github.com/appstefan/HighlightSwift/assets/6455394/1df57ebb-f316-453c-ae40-909bab59ccb1\"\u003e\n  \u003cimg alt=\"CodeCard\" src=\"https://github.com/appstefan/HighlightSwift/assets/6455394/1df57ebb-f316-453c-ae40-909bab59ccb1\" width=100% height=100%\u003e\n\u003c/picture\u003e\n\n## Contents\n\n#### `Highlight`\nSwift class to convert a `String` of code into a syntax highlighted `AttributedString`\n* 🔍 Automatic language detection\n* 📚 Support for 50+ common languages\n* 🌈 Choose from 30 built-in color themes or use custom CSS\n* 🧰 Built with [highlight.js](https://github.com/highlightjs/highlight.js) \u0026 `JavaScriptCore`\n* ☑️ Complete concurrency checking enabled\n* 🖥️ Works on iOS, iPadOS, macOS, and tvOS\n\n#### `CodeText`\nSwiftUI view to display a `String` of code with syntax highlighting\n* 🌗 Color theme syncs automatically with Dark Mode\n* 📜 Theme background color included with `.card` style\n* 🔠 Works with `Text` modifiers like `.bold()` or `.font()`\n* ⚙️ Includes modifiers to set the color theme, style \u0026 language\n* 📫 Callback modifiers to get the highlight results, language \u0026 score\n* 🍃 Memory efficient using an internal `Highlight` environment entry\n\n## Highlight\n\nCreate an instance of `Highlight` and convert a `String` of code into a syntax highlighted `AttributedString`:\n```swift\nlet someCode = \"\"\"\n    print(\\\"Hello World\\\")\n    \"\"\"\nlet highlight = Highlight()\nlet attributedText = try await highlight.attributedText(someCode)\n```\n\nAdd the `language:` parameter to set the language and disable automatic language detection:\n```swift\nlet attributedText = try await highlight.attributedText(someCode, language: \"swift\")\n```\n\nUse the `colors:` parameter to change the color theme.\n```swift\nlet attributedText = try await highlight.attributedText(someCode, colors: .dark(.github))\n```\n\nApply a custom CSS theme with the `.custom` option.\nRefer to the highlight.js [Theme Guide](https://highlightjs.readthedocs.io/en/latest/theme-guide.html#) for details:\n```swift\nlet someCSS = \"\"\"\n    .hljs {\n      display: block;\n      overflow-x: auto;\n      padding: 0.5em;\n    }\n    \"\"\"\nlet attributedText = try await highlight.attributedText(someCode, colors: .custom(css: someCSS))\n```\n\nThe `.request()` function returns a `HighlightResult` with extra information:\n```swift\nlet result: HighlightResult = try await highlight.request(someCode)\nprint(result)\n```\n```swift\nHighlightResult(\n    attributedText: \"...\",\n    relevance: 5,\n    language: \"swift\",\n    languageName: \"Swift?\",\n    backgroundColor: #1F2024FF,\n    hasIllegal: false,\n    isUndefined: false)\n```\n\n##\n### `CodeText`\n\nCreate a `CodeText` view with some code:\n```swift\nlet someCode: String = \"\"\"\n    print(\\\"Hello World\\\")\n    \"\"\"\n\nvar body: some View {\n    CodeText(someCode)\n}\n```\n\nThe font design is always `.monospaced`. \nOther text modifiers can be applied:\n```swift\nCodeText(someCode)\n    .font(.callout)\n    .fontWeight(.semibold)\n```\n\nAdd the `.highlightLanguage()` modifier to set the language and disable automatic detection:\n```swift\nCodeText(someCode)\n    .highlightLanguage(.swift)\n```\n\n#### Colors\nAdd the `.codeTextColors()` modifier to set the color theme. The built-in color themes update automatically with Dark Mode to the corresponding dark variant.\n```swift\nCodeText(someCode)\n    .codeTextColors(.github)\n```\n\nChoose 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.\n```swift\nCodeText(someCode)\n    .codeTextColors(.custom(dark: .custom(css: someDarkCSS), light: .custom(css: someLightCSS)))\n```\n\n#### Styles\nThe 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:\n```swift\nCodeText(someCode)\n    .codeTextStyle(.card)\n```\n\nThe `.card` style has a few customization options, for example:\n```swift\nCodeText(someCode)\n    .codeTextStyle(.card(cornerRadius: 0, stroke: .separator, verticalPadding: 12))\n```\n\n#### Results\nAdd `.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. \n```swift\nCodeText(someCode)\n    .onHighlightSuccess { result in\n        ...\n    }\n    .onHighlightFailure { error in\n        ...\n    }\n```\n\nThere is also a combined `.onHighlightResult()` equivalent of the two callbacks above.\n```swift\nCodeText(someCode)\n    .onHighlightResult { result in\n        switch result {\n            case .success:\n                ...\n            case .failure: \n                ...\n        }\n    }\n```\n\nA previously stored highlight result can be passed to the `CodeText`.\nIn combination with `.onHighlightSuccess()` the result can be persisted when the view might reappear frequently, such as in a list view:\n```swift\nlet someCode: String = \"\"\"\n    print(\\\"Hello World\\\")\n\"\"\"\n\n@State var result: HighlightResult?\n\nvar body: some View {\n    List {\n        ...\n        CodeText(someCode, result: result)\n            .onHighlightSuccess { result in \n                self.result = result\n            }\n        ...\n    }\n}\n```\n\n## Installation\n\n### Project\n\n1. In Xcode, go to `File` \u003e `Add packages...`\n2. Enter `https://github.com/appstefan/highlightswift` in the field and click `Add Package`\n\n### Package\n\nIn `Package.swift` add this repository as a dependency:\n```swift\ndependencies: [\n    .package(url: \"https://github.com/appstefan/highlightswift.git\", from: \"1.1\")\n],\ntargets: [\n    .target(\n        name: \"YourPackageName\",\n        dependencies: [\"HighlightSwift\"]\n    )\n]\n```\n\n## Author\n\nStefan, thrower_ranges.0d@icloud.com\n\n## License\n\nHighlightSwift is available under the MIT license. See the [LICENSE.md](/LICENSE.md) file.\nHighlight.js is available under the BSD license. See the [LICENSE.md](/Sources/HighlightSwift/HighlightJS/LICENSE.md) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappstefan%2FHighlightSwift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappstefan%2FHighlightSwift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappstefan%2FHighlightSwift/lists"}