@@ -37,75 +37,26 @@ extension View {
3737 var messageBubbleCornerRadius : Double { 8 }
3838
3939 func codeBlockLabelStyle( ) -> some View {
40- CodeBlockLabelStyle ( content: self )
41- }
42-
43- func codeBlockStyle( _ configuration: CodeBlockConfiguration ) -> some View {
44- CodeBlockStyle ( configuration: configuration, content: self )
45- }
46- }
47-
48- struct CodeBlockLabelStyle < Content: View > : View {
49- @AppStorage ( \. syncChatCodeHighlightTheme) var syncCodeHighlightTheme
50- @AppStorage ( \. codeForegroundColorLight) var codeForegroundColorLight
51- @AppStorage ( \. codeBackgroundColorLight) var codeBackgroundColorLight
52- @AppStorage ( \. codeForegroundColorDark) var codeForegroundColorDark
53- @AppStorage ( \. codeBackgroundColorDark) var codeBackgroundColorDark
54-
55- let content : Content
56-
57- var body : some View {
58- content
59- . relativeLineSpacing ( . em( 0.225 ) )
40+ relativeLineSpacing ( . em( 0.225 ) )
6041 . markdownTextStyle {
6142 FontFamilyVariant ( . monospaced)
6243 FontSize ( . em( 0.85 ) )
6344 }
6445 . padding ( 16 )
6546 . padding ( . top, 14 )
6647 }
67- }
68-
69- struct CodeBlockStyle < Content: View > : View {
70- @AppStorage ( \. syncChatCodeHighlightTheme) var syncCodeHighlightTheme
71- @AppStorage ( \. codeForegroundColorLight) var codeForegroundColorLight
72- @AppStorage ( \. codeBackgroundColorLight) var codeBackgroundColorLight
73- @AppStorage ( \. codeForegroundColorDark) var codeForegroundColorDark
74- @AppStorage ( \. codeBackgroundColorDark) var codeBackgroundColorDark
75- @Environment ( \. colorScheme) var colorScheme
7648
77- let configuration : CodeBlockConfiguration
78- let content : Content
79-
80- var body : some View {
81- content
82- . background ( {
83- if syncCodeHighlightTheme {
84- if colorScheme == . light, let color = codeBackgroundColorLight. value {
85- return color. swiftUIColor
86- } else if let color = codeBackgroundColorDark. value {
87- return color. swiftUIColor
88- }
89- }
90-
91- return Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 )
92- } ( ) as Color )
49+ func codeBlockStyle(
50+ _ configuration: CodeBlockConfiguration ,
51+ backgroundColor: Color ,
52+ labelColor: Color
53+ ) -> some View {
54+ background ( backgroundColor)
9355 . clipShape ( RoundedRectangle ( cornerRadius: 6 ) )
9456 . overlay ( alignment: . top) {
9557 HStack ( alignment: . center) {
9658 Text ( configuration. language ?? " code " )
97- . foregroundStyle ( {
98- if syncCodeHighlightTheme {
99- if colorScheme == . light,
100- let color = codeForegroundColorLight. value
101- {
102- return color. swiftUIColor. opacity ( 0.5 )
103- } else if let color = codeForegroundColorDark. value {
104- return color. swiftUIColor. opacity ( 0.5 )
105- }
106- }
107- return Color . secondary. opacity ( 0.7 )
108- } ( ) as Color )
59+ . foregroundStyle ( labelColor)
10960 . font ( . callout)
11061 . padding ( . leading, 8 )
11162 . lineLimit ( 1 )
@@ -123,8 +74,69 @@ struct CodeBlockStyle<Content: View>: View {
12374 }
12475}
12576
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 ( \. chatCodeFontSize) var chatCodeFontSize
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+ fontSize: chatCodeFontSize,
126+ colorChange: colorScheme == . dark
127+ ? codeForegroundColorDark. value? . swiftUIColor
128+ : codeForegroundColorLight. value? . swiftUIColor
129+ )
130+ )
131+ }
132+ }
133+
126134extension MarkdownUI . Theme {
127- static func custom( fontSize: Double ) -> MarkdownUI . Theme {
135+ static func custom(
136+ fontSize: Double ,
137+ codeBlockBackgroundColor: Color ,
138+ codeBlockLabelColor: Color
139+ ) -> MarkdownUI . Theme {
128140 . gitHub. text {
129141 ForegroundColor ( . primary)
130142 BackgroundColor ( Color . clear)
@@ -136,14 +148,22 @@ extension MarkdownUI.Theme {
136148 if wrapCode {
137149 configuration. label
138150 . codeBlockLabelStyle ( )
139- . codeBlockStyle ( configuration)
151+ . codeBlockStyle (
152+ configuration,
153+ backgroundColor: codeBlockBackgroundColor,
154+ labelColor: codeBlockLabelColor
155+ )
140156 } else {
141157 ScrollView ( . horizontal) {
142158 configuration. label
143159 . codeBlockLabelStyle ( )
144160 }
145161 . workaroundForVerticalScrollingBugInMacOS ( )
146- . codeBlockStyle ( configuration)
162+ . codeBlockStyle (
163+ configuration,
164+ backgroundColor: codeBlockBackgroundColor,
165+ labelColor: codeBlockLabelColor
166+ )
147167 }
148168 }
149169 }
@@ -165,14 +185,22 @@ extension MarkdownUI.Theme {
165185 if wrapCode {
166186 configuration. label
167187 . codeBlockLabelStyle ( )
168- . codeBlockStyle ( configuration)
188+ . codeBlockStyle (
189+ configuration,
190+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
191+ labelColor: Color . secondary. opacity ( 0.7 )
192+ )
169193 } else {
170194 ScrollView ( . horizontal) {
171195 configuration. label
172196 . codeBlockLabelStyle ( )
173197 }
174198 . workaroundForVerticalScrollingBugInMacOS ( )
175- . codeBlockStyle ( configuration)
199+ . codeBlockStyle (
200+ configuration,
201+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
202+ labelColor: Color . secondary. opacity ( 0.7 )
203+ )
176204 }
177205 }
178206 . table { configuration in
0 commit comments