1+ import Preferences
12import SwiftUI
23
34public struct CodeBlock : View {
@@ -11,6 +12,7 @@ public struct CodeBlock: View {
1112 public let firstLinePrecedingSpaceCount : Int
1213 public let fontSize : Double
1314 public let droppingLeadingSpaces : Bool
15+ public let proposedForegroundColor : Color ?
1416
1517 public init (
1618 code: String ,
@@ -20,7 +22,8 @@ public struct CodeBlock: View {
2022 colorScheme: ColorScheme ,
2123 firstLinePrecedingSpaceCount: Int = 0 ,
2224 fontSize: Double ,
23- droppingLeadingSpaces: Bool
25+ droppingLeadingSpaces: Bool ,
26+ proposedForegroundColor: Color ?
2427 ) {
2528 self . code = code
2629 self . language = language
@@ -30,6 +33,7 @@ public struct CodeBlock: View {
3033 self . droppingLeadingSpaces = droppingLeadingSpaces
3134 self . firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
3235 self . fontSize = fontSize
36+ self . proposedForegroundColor = proposedForegroundColor
3337 let padding = firstLinePrecedingSpaceCount > 0
3438 ? String ( repeating: " " , count: firstLinePrecedingSpaceCount)
3539 : " "
@@ -44,17 +48,21 @@ public struct CodeBlock: View {
4448 commonPrecedingSpaceCount = result. commonLeadingSpaceCount
4549 highlightedCode = result. code
4650 }
51+
52+ var foregroundColor : Color {
53+ proposedForegroundColor ?? ( colorScheme == . dark ? . white : . black)
54+ }
4755
4856 public var body : some View {
4957 VStack ( spacing: 2 ) {
5058 ForEach ( 0 ..< highlightedCode. endIndex, id: \. self) { index in
5159 HStack ( alignment: . firstTextBaseline, spacing: 4 ) {
5260 Text ( " \( index + startLineIndex + 1 ) " )
5361 . multilineTextAlignment ( . trailing)
54- . foregroundColor ( . secondary )
62+ . foregroundColor ( foregroundColor . opacity ( 0.5 ) )
5563 . frame ( minWidth: 40 )
5664 Text ( AttributedString ( highlightedCode [ index] ) )
57- . foregroundColor ( . white . opacity ( 0.1 ) )
65+ . foregroundColor ( foregroundColor . opacity ( 0.3 ) )
5866 . frame ( maxWidth: . infinity, alignment: . leading)
5967 . multilineTextAlignment ( . leading)
6068 . lineSpacing ( 4 )
@@ -63,7 +71,7 @@ public struct CodeBlock: View {
6371 Text ( " \( commonPrecedingSpaceCount + 1 ) " )
6472 . padding ( . top, - 12 )
6573 . font ( . footnote)
66- . foregroundStyle ( colorScheme == . dark ? . white : . black )
74+ . foregroundStyle ( foregroundColor )
6775 . opacity ( 0.3 )
6876 }
6977 }
@@ -110,7 +118,8 @@ struct CodeBlock_Previews: PreviewProvider {
110118 colorScheme: . dark,
111119 firstLinePrecedingSpaceCount: 0 ,
112120 fontSize: 12 ,
113- droppingLeadingSpaces: true
121+ droppingLeadingSpaces: true ,
122+ proposedForegroundColor: nil
114123 )
115124 }
116125}
0 commit comments