@@ -10,98 +10,32 @@ public func highlightedCodeBlock(
1010 brightMode: Bool ,
1111 fontSize: Double
1212) -> 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)
13+ var language = language
14+ // Workaround: Highlightr uses a different identifier for Objective-C.
15+ if language. lowercased ( ) . hasPrefix ( " objective " ) , language. lowercased ( ) . hasSuffix ( " c " ) {
16+ language = " objectivec "
17+ }
18+ func unhighlightedCode( ) -> NSAttributedString {
19+ return NSAttributedString (
20+ string: code,
21+ attributes: [
22+ . foregroundColor: brightMode ? NSColor . black : NSColor . white,
23+ . font: NSFont . monospacedSystemFont ( ofSize: fontSize, weight: . regular) ,
24+ ]
5125 )
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
10426 }
27+ guard let highlighter = Highlightr ( ) else {
28+ return unhighlightedCode ( )
29+ }
30+ highlighter. setTheme ( to: brightMode ? " xcode " : " atom-one-dark " )
31+ highlighter. theme. setCodeFont ( . monospacedSystemFont( ofSize: fontSize, weight: . regular) )
32+ guard let formatted = highlighter. highlight ( code, as: language) else {
33+ return unhighlightedCode ( )
34+ }
35+ if formatted. string == " undefined " {
36+ return unhighlightedCode ( )
37+ }
38+ return formatted
10539}
10640
10741public func highlighted(
0 commit comments