forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemedMarkdownText.swift
More file actions
108 lines (98 loc) · 3.74 KB
/
ThemedMarkdownText.swift
File metadata and controls
108 lines (98 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Foundation
import MarkdownUI
import SwiftUI
struct ThemedMarkdownText: View {
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
@AppStorage(\.chatFontSize) var chatFontSize
@AppStorage(\.chatCodeFont) var chatCodeFont
@Environment(\.colorScheme) var colorScheme
let content: MarkdownContent
init(_ text: String) {
content = .init(text)
}
init(_ content: MarkdownContent) {
self.content = content
}
var body: some View {
Markdown(content)
.textSelection(.enabled)
.markdownTheme(.custom(
fontSize: chatFontSize,
codeFont: chatCodeFont.value.nsFont,
codeBlockBackgroundColor: {
if syncCodeHighlightTheme {
if colorScheme == .light, let color = codeBackgroundColorLight.value {
return color.swiftUIColor
} else if let color = codeBackgroundColorDark.value {
return color.swiftUIColor
}
}
return Color(nsColor: .textBackgroundColor).opacity(0.7)
}(),
codeBlockLabelColor: {
if syncCodeHighlightTheme {
if colorScheme == .light,
let color = codeForegroundColorLight.value
{
return color.swiftUIColor.opacity(0.5)
} else if let color = codeForegroundColorDark.value {
return color.swiftUIColor.opacity(0.5)
}
}
return Color.secondary.opacity(0.7)
}()
))
}
}
// MARK: - Theme
extension MarkdownUI.Theme {
static func custom(
fontSize: Double,
codeFont: NSFont,
codeBlockBackgroundColor: Color,
codeBlockLabelColor: Color
) -> MarkdownUI.Theme {
.gitHub.text {
ForegroundColor(.primary)
BackgroundColor(Color.clear)
FontSize(fontSize)
}
.codeBlock { configuration in
let wrapCode = UserDefaults.shared.value(for: \.wrapCodeInChatCodeBlock)
|| ["plaintext", "text", "markdown", "sh", "bash", "shell", "latex", "tex"]
.contains(configuration.language)
if wrapCode {
AsyncCodeBlockView(
fenceInfo: configuration.language,
content: configuration.content,
font: codeFont
)
.codeBlockLabelStyle()
.codeBlockStyle(
configuration,
backgroundColor: codeBlockBackgroundColor,
labelColor: codeBlockLabelColor
)
} else {
ScrollView(.horizontal) {
AsyncCodeBlockView(
fenceInfo: configuration.language,
content: configuration.content,
font: codeFont
)
.codeBlockLabelStyle()
}
.workaroundForVerticalScrollingBugInMacOS()
.codeBlockStyle(
configuration,
backgroundColor: codeBlockBackgroundColor,
labelColor: codeBlockLabelColor
)
}
}
}
}