Skip to content

Commit d43798c

Browse files
committed
Merge branch 'feature/bump-highlightjs-version' into develop
2 parents 93734f3 + f10bdae commit d43798c

3 files changed

Lines changed: 28 additions & 106 deletions

File tree

Copilot for Xcode.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 3 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tool/Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ let package = Package(
3939
.package(url: "https://github.com/ChimeHQ/JSONRPC", exact: "0.6.0"),
4040
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.6.0"),
4141
.package(url: "https://github.com/unum-cloud/usearch", from: "0.19.1"),
42-
.package(url: "https://github.com/raspu/Highlightr", from: "2.1.0"),
43-
.package(url: "https://github.com/JohnSundell/Splash", branch: "master"),
42+
.package(url: "https://github.com/intitni/Highlightr", branch: "bump-highlight-js-version"),
4443
.package(
4544
url: "https://github.com/pointfreeco/swift-composable-architecture",
4645
from: "0.55.0"
@@ -148,7 +147,6 @@ let package = Package(
148147
name: "SharedUIComponents",
149148
dependencies: [
150149
"Highlightr",
151-
"Splash",
152150
"Preferences",
153151
]
154152
),

Tool/Sources/SharedUIComponents/SyntaxHighlighting.swift

Lines changed: 24 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import AppKit
22
import Foundation
33
import Highlightr
4-
import Splash
54
import SwiftUI
65

76
public func highlightedCodeBlock(
@@ -10,98 +9,32 @@ public func highlightedCodeBlock(
109
brightMode: Bool,
1110
fontSize: Double
1211
) -> NSAttributedString {
13-
switch language {
14-
case "swift":
15-
let plainTextColor = brightMode
16-
? .black
17-
: #colorLiteral(red: 0.6509803922, green: 0.6980392157, blue: 0.7529411765, alpha: 1)
18-
let highlighter =
19-
SyntaxHighlighter(
20-
format: AttributedStringOutputFormat(theme: .init(
21-
font: .init(size: fontSize),
22-
plainTextColor: plainTextColor,
23-
tokenColors: brightMode
24-
? [
25-
.keyword: #colorLiteral(red: 0.6078431373, green: 0.137254902, blue: 0.5764705882, alpha: 1),
26-
.string: #colorLiteral(red: 0.1371159852, green: 0.3430536985, blue: 0.362406373, alpha: 1),
27-
.type: #colorLiteral(red: 0.2456904352, green: 0.5002114773, blue: 0.5297455192, alpha: 1),
28-
.call: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
29-
.number: #colorLiteral(red: 0.4385872483, green: 0.4995297194, blue: 0.5483990908, alpha: 1),
30-
.comment: #colorLiteral(red: 0.3647058824, green: 0.4235294118, blue: 0.4745098039, alpha: 1),
31-
.property: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
32-
.dotAccess: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
33-
.preprocessing: #colorLiteral(red: 0.3921568627, green: 0.2196078431, blue: 0.1254901961, alpha: 1),
34-
] : [
35-
.keyword: #colorLiteral(red: 0.8258609176, green: 0.5708742738, blue: 0.8922662139, alpha: 1),
36-
.string: #colorLiteral(red: 0.6253595352, green: 0.7963448763, blue: 0.5427476764, alpha: 1),
37-
.type: #colorLiteral(red: 0.9221783876, green: 0.7978314757, blue: 0.5575165749, alpha: 1),
38-
.call: #colorLiteral(red: 0.4466812611, green: 0.742190659, blue: 0.9515134692, alpha: 1),
39-
.number: #colorLiteral(red: 0.8620631099, green: 0.6468816996, blue: 0.4395158887, alpha: 1),
40-
.comment: #colorLiteral(red: 0.4233166873, green: 0.4612616301, blue: 0.5093258619, alpha: 1),
41-
.property: #colorLiteral(red: 0.906378448, green: 0.5044228435, blue: 0.5263597369, alpha: 1),
42-
.dotAccess: #colorLiteral(red: 0.906378448, green: 0.5044228435, blue: 0.5263597369, alpha: 1),
43-
.preprocessing: #colorLiteral(red: 0.3776347041, green: 0.8792117238, blue: 0.4709561467, alpha: 1),
44-
]
45-
))
46-
)
47-
let formatted = NSMutableAttributedString(attributedString: highlighter.highlight(code))
48-
formatted.addAttributes(
49-
[.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)],
50-
range: NSRange(location: 0, length: formatted.length)
12+
var language = language
13+
// Workaround: Highlightr uses a different identifier for Objective-C.
14+
if language.lowercased().hasPrefix("objective"), language.lowercased().hasSuffix("c") {
15+
language = "objectivec"
16+
}
17+
func unhighlightedCode() -> NSAttributedString {
18+
return NSAttributedString(
19+
string: code,
20+
attributes: [
21+
.foregroundColor: brightMode ? NSColor.black : NSColor.white,
22+
.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular),
23+
]
5124
)
52-
func leadingSpacesInCode(_ code: String) -> Int {
53-
var leadingSpaces = 0
54-
for char in code {
55-
if char == " " {
56-
leadingSpaces += 1
57-
} else {
58-
break
59-
}
60-
}
61-
return leadingSpaces
62-
}
63-
64-
// Workaround: Splash has a bug that will insert an extra space at the beginning.
65-
let leadingSpaces = leadingSpacesInCode(code)
66-
let leadingSpacesFormatted = leadingSpacesInCode(formatted.string)
67-
let diff = leadingSpacesFormatted - leadingSpaces
68-
if diff > 0 {
69-
formatted.mutableString.replaceCharacters(
70-
in: .init(location: 0, length: diff),
71-
with: ""
72-
)
73-
}
74-
// End of workaround.
75-
76-
return formatted
77-
default:
78-
var language = language
79-
// Workaround: Highlightr uses a different identifier for Objective-C.
80-
if language.lowercased().hasPrefix("objective"), language.lowercased().hasSuffix("c") {
81-
language = "objectivec"
82-
}
83-
func unhighlightedCode() -> NSAttributedString {
84-
return NSAttributedString(
85-
string: code,
86-
attributes: [
87-
.foregroundColor: brightMode ? NSColor.black : NSColor.white,
88-
.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular),
89-
]
90-
)
91-
}
92-
guard let highlighter = Highlightr() else {
93-
return unhighlightedCode()
94-
}
95-
highlighter.setTheme(to: brightMode ? "xcode" : "atom-one-dark")
96-
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: fontSize, weight: .regular))
97-
guard let formatted = highlighter.highlight(code, as: language) else {
98-
return unhighlightedCode()
99-
}
100-
if formatted.string == "undefined" {
101-
return unhighlightedCode()
102-
}
103-
return formatted
10425
}
26+
guard let highlighter = Highlightr() else {
27+
return unhighlightedCode()
28+
}
29+
highlighter.setTheme(to: brightMode ? "xcode" : "atom-one-dark")
30+
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: fontSize, weight: .regular))
31+
guard let formatted = highlighter.highlight(code, as: language) else {
32+
return unhighlightedCode()
33+
}
34+
if formatted.string == "undefined" {
35+
return unhighlightedCode()
36+
}
37+
return formatted
10538
}
10639

10740
public func highlighted(

0 commit comments

Comments
 (0)