@@ -46,13 +46,17 @@ extension View {
4646 . padding ( . top, 14 )
4747 }
4848
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)
5155 . clipShape ( RoundedRectangle ( cornerRadius: 6 ) )
5256 . overlay ( alignment: . top) {
5357 HStack ( alignment: . center) {
5458 Text ( configuration. language ?? " code " )
55- . foregroundStyle ( . tertiary )
59+ . foregroundStyle ( labelColor )
5660 . font ( . callout)
5761 . padding ( . leading, 8 )
5862 . lineLimit ( 1 )
@@ -63,12 +67,76 @@ extension View {
6367 }
6468 }
6569 }
70+ . overlay {
71+ RoundedRectangle ( cornerRadius: 6 ) . stroke ( Color . primary. opacity ( 0.05 ) , lineWidth: 1 )
72+ }
6673 . markdownMargin ( top: 4 , bottom: 16 )
6774 }
6875}
6976
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+
70134extension 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 {
72140 . gitHub. text {
73141 ForegroundColor ( . primary)
74142 BackgroundColor ( Color . clear)
@@ -80,14 +148,22 @@ extension MarkdownUI.Theme {
80148 if wrapCode {
81149 configuration. label
82150 . codeBlockLabelStyle ( )
83- . codeBlockStyle ( configuration)
151+ . codeBlockStyle (
152+ configuration,
153+ backgroundColor: codeBlockBackgroundColor,
154+ labelColor: codeBlockLabelColor
155+ )
84156 } else {
85157 ScrollView ( . horizontal) {
86158 configuration. label
87159 . codeBlockLabelStyle ( )
88160 }
89161 . workaroundForVerticalScrollingBugInMacOS ( )
90- . codeBlockStyle ( configuration)
162+ . codeBlockStyle (
163+ configuration,
164+ backgroundColor: codeBlockBackgroundColor,
165+ labelColor: codeBlockLabelColor
166+ )
91167 }
92168 }
93169 }
@@ -109,14 +185,22 @@ extension MarkdownUI.Theme {
109185 if wrapCode {
110186 configuration. label
111187 . codeBlockLabelStyle ( )
112- . codeBlockStyle ( configuration)
188+ . codeBlockStyle (
189+ configuration,
190+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
191+ labelColor: Color . secondary. opacity ( 0.7 )
192+ )
113193 } else {
114194 ScrollView ( . horizontal) {
115195 configuration. label
116196 . codeBlockLabelStyle ( )
117197 }
118198 . workaroundForVerticalScrollingBugInMacOS ( )
119- . codeBlockStyle ( configuration)
199+ . codeBlockStyle (
200+ configuration,
201+ backgroundColor: Color ( nsColor: . textBackgroundColor) . opacity ( 0.7 ) ,
202+ labelColor: Color . secondary. opacity ( 0.7 )
203+ )
120204 }
121205 }
122206 . table { configuration in
0 commit comments