@@ -46,13 +46,17 @@ extension View {
46
46
. padding ( . top, 14 )
47
47
}
48
48
49
- func codeBlockStyle( _ configuration: CodeBlockConfiguration ) -> some View {
50
- background ( Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) )
49
+ func codeBlockStyle(
50
+ _ configuration: CodeBlockConfiguration ,
51
+ backgroundColor: Color ,
52
+ labelColor: Color
53
+ ) -> some View {
54
+ background ( backgroundColor)
51
55
. clipShape ( RoundedRectangle ( cornerRadius: 6 ) )
52
56
. overlay ( alignment: . top) {
53
57
HStack ( alignment: . center) {
54
58
Text ( configuration. language ?? " code " )
55
- . foregroundStyle ( . tertiary )
59
+ . foregroundStyle ( labelColor )
56
60
. font ( . callout)
57
61
. padding ( . leading, 8 )
58
62
. lineLimit ( 1 )
@@ -63,12 +67,76 @@ extension View {
63
67
}
64
68
}
65
69
}
70
+ . overlay {
71
+ RoundedRectangle ( cornerRadius: 6 ) . stroke ( Color . primary. opacity ( 0.05 ) , lineWidth: 1 )
72
+ }
66
73
. markdownMargin ( top: 4 , bottom: 16 )
67
74
}
68
75
}
69
76
77
+ struct ThemedMarkdownText : View {
78
+ @AppStorage ( \. syncChatCodeHighlightTheme) var syncCodeHighlightTheme
79
+ @AppStorage ( \. codeForegroundColorLight) var codeForegroundColorLight
80
+ @AppStorage ( \. codeBackgroundColorLight) var codeBackgroundColorLight
81
+ @AppStorage ( \. codeForegroundColorDark) var codeForegroundColorDark
82
+ @AppStorage ( \. codeBackgroundColorDark) var codeBackgroundColorDark
83
+ @AppStorage ( \. chatFontSize) var chatFontSize
84
+ @AppStorage ( \. chatCodeFont) var chatCodeFont
85
+ @Environment ( \. colorScheme) var colorScheme
86
+
87
+ let text : String
88
+
89
+ init ( _ text: String ) {
90
+ self . text = text
91
+ }
92
+
93
+ var body : some View {
94
+ Markdown ( text)
95
+ . textSelection ( . enabled)
96
+ . markdownTheme ( . custom(
97
+ fontSize: chatFontSize,
98
+ codeBlockBackgroundColor: {
99
+ if syncCodeHighlightTheme {
100
+ if colorScheme == . light, let color = codeBackgroundColorLight. value {
101
+ return color. swiftUIColor
102
+ } else if let color = codeBackgroundColorDark. value {
103
+ return color. swiftUIColor
104
+ }
105
+ }
106
+
107
+ return Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 )
108
+ } ( ) ,
109
+ codeBlockLabelColor: {
110
+ if syncCodeHighlightTheme {
111
+ if colorScheme == . light,
112
+ let color = codeForegroundColorLight. value
113
+ {
114
+ return color. swiftUIColor. opacity ( 0.5 )
115
+ } else if let color = codeForegroundColorDark. value {
116
+ return color. swiftUIColor. opacity ( 0.5 )
117
+ }
118
+ }
119
+ return Color . secondary. opacity ( 0.7 )
120
+ } ( )
121
+ ) )
122
+ . markdownCodeSyntaxHighlighter (
123
+ ChatCodeSyntaxHighlighter (
124
+ brightMode: colorScheme != . dark,
125
+ font: chatCodeFont. value. nsFont,
126
+ colorChange: colorScheme == . dark
127
+ ? codeForegroundColorDark. value? . swiftUIColor
128
+ : codeForegroundColorLight. value? . swiftUIColor
129
+ )
130
+ )
131
+ }
132
+ }
133
+
70
134
extension MarkdownUI . Theme {
71
- static func custom( fontSize: Double ) -> MarkdownUI . Theme {
135
+ static func custom(
136
+ fontSize: Double ,
137
+ codeBlockBackgroundColor: Color ,
138
+ codeBlockLabelColor: Color
139
+ ) -> MarkdownUI . Theme {
72
140
. gitHub. text {
73
141
ForegroundColor ( . primary)
74
142
BackgroundColor ( Color . clear)
@@ -80,14 +148,22 @@ extension MarkdownUI.Theme {
80
148
if wrapCode {
81
149
configuration. label
82
150
. codeBlockLabelStyle ( )
83
- . codeBlockStyle ( configuration)
151
+ . codeBlockStyle (
152
+ configuration,
153
+ backgroundColor: codeBlockBackgroundColor,
154
+ labelColor: codeBlockLabelColor
155
+ )
84
156
} else {
85
157
ScrollView ( . horizontal) {
86
158
configuration. label
87
159
. codeBlockLabelStyle ( )
88
160
}
89
161
. workaroundForVerticalScrollingBugInMacOS ( )
90
- . codeBlockStyle ( configuration)
162
+ . codeBlockStyle (
163
+ configuration,
164
+ backgroundColor: codeBlockBackgroundColor,
165
+ labelColor: codeBlockLabelColor
166
+ )
91
167
}
92
168
}
93
169
}
@@ -109,14 +185,22 @@ extension MarkdownUI.Theme {
109
185
if wrapCode {
110
186
configuration. label
111
187
. codeBlockLabelStyle ( )
112
- . codeBlockStyle ( configuration)
188
+ . codeBlockStyle (
189
+ configuration,
190
+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
191
+ labelColor: Color . secondary. opacity ( 0.7 )
192
+ )
113
193
} else {
114
194
ScrollView ( . horizontal) {
115
195
configuration. label
116
196
. codeBlockLabelStyle ( )
117
197
}
118
198
. workaroundForVerticalScrollingBugInMacOS ( )
119
- . codeBlockStyle ( configuration)
199
+ . codeBlockStyle (
200
+ configuration,
201
+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
202
+ labelColor: Color . secondary. opacity ( 0.7 )
203
+ )
120
204
}
121
205
}
122
206
. table { configuration in
0 commit comments