@@ -26,7 +26,7 @@ struct CodeSyntaxHighlightView: View {
26
26
}
27
27
28
28
func highlightCode(_ content: String, language: String?) -> Text {
29
- guard language?.lowercased() == "swift" else {
29
+ guard language != nil else {
30
30
return Text(content)
31
31
}
32
32
@@ -50,15 +50,92 @@ struct CodeSyntaxHighlightView: View {
50
50
.markdownCodeSyntaxHighlighter(.splash(theme: .sunset(withFont: .init(size: 16))))
51
51
}
52
52
```
53
+
54
+ More languages to render:
55
+
56
+ ```
57
+ A plain code block without the specifying a language name.
58
+ ```
59
+
60
+ ```cpp
61
+ #include <iostream>
62
+ #include <vector>
63
+
64
+ int main() {
65
+ std::vector<std::string> fruits = {"apple", "banana", "orange"};
66
+ for (const std::string& fruit : fruits) {
67
+ std::cout << "I love " << fruit << "s!" << std::endl;
68
+ }
69
+ return 0;
70
+ }
71
+ ```
72
+
73
+ ```typescript
74
+ interface Person {
75
+ name: string;
76
+ age: number;
77
+ }
78
+
79
+ const person = Person();
80
+ ```
81
+
82
+ ```ruby
83
+ fruits = ["apple", "banana", "orange"]
84
+ fruits.each do |fruit|
85
+ puts "I love #{fruit}s!"
86
+ end
87
+ ```
88
+
53
89
"""#
54
90
55
91
var body : some View {
56
92
DemoView {
57
93
Markdown ( self . content)
94
+ . markdownBlockStyle ( \. codeBlock) {
95
+ codeBlock ( $0)
96
+ }
58
97
. markdownCodeSyntaxHighlighter ( . splash( theme: self . theme) )
59
98
}
60
99
}
61
100
101
+ @ViewBuilder
102
+ private func codeBlock( _ configuration: CodeBlockConfiguration ) -> some View {
103
+ VStack ( spacing: 0 ) {
104
+ HStack {
105
+ Text ( configuration. language ?? " plain text " )
106
+ . font ( . system( . caption, design: . monospaced) )
107
+ . fontWeight ( . semibold)
108
+ . foregroundColor ( Color ( theme. plainTextColor) )
109
+ Spacer ( )
110
+
111
+ Image ( systemName: " clipboard " )
112
+ . onTapGesture {
113
+ copyToClipboard ( configuration. content)
114
+ }
115
+ }
116
+ . padding ( . horizontal)
117
+ . padding ( . vertical, 8 )
118
+ . background {
119
+ Color ( theme. backgroundColor)
120
+ }
121
+
122
+ Divider ( )
123
+
124
+ ScrollView ( . horizontal) {
125
+ configuration. label
126
+ . relativeLineSpacing ( . em( 0.25 ) )
127
+ . markdownTextStyle {
128
+ FontFamilyVariant ( . monospaced)
129
+ FontSize ( . em( 0.85 ) )
130
+ }
131
+ . padding ( )
132
+ }
133
+ }
134
+ . background ( Color ( . secondarySystemBackground) )
135
+ . clipShape ( RoundedRectangle ( cornerRadius: 8 ) )
136
+ . markdownMargin ( top: . zero, bottom: . em( 0.8 ) )
137
+ }
138
+
62
139
private var theme : Splash . Theme {
63
140
// NOTE: We are ignoring the Splash theme font
64
141
switch self . colorScheme {
@@ -68,6 +145,17 @@ struct CodeSyntaxHighlightView: View {
68
145
return . sunset( withFont: . init( size: 16 ) )
69
146
}
70
147
}
148
+
149
+ private func copyToClipboard( _ string: String ) {
150
+ #if os(macOS)
151
+ if let pasteboard = NSPasteboard . general {
152
+ pasteboard. clearContents ( )
153
+ pasteboard. setString ( string, forType: . string)
154
+ }
155
+ #elseif os(iOS)
156
+ UIPasteboard . general. string = string
157
+ #endif
158
+ }
71
159
}
72
160
73
161
struct CodeSyntaxHighlightView_Previews : PreviewProvider {
0 commit comments