@@ -40,11 +40,7 @@ struct Documentation: Component {
40
40
Declaration ( of: symbol, in: module)
41
41
42
42
ForEach ( in: documentation. discussionParts) { part in
43
- if part is SwiftMarkup . Documentation . Callout {
44
- Callout ( part as! SwiftMarkup . Documentation . Callout )
45
- } else {
46
- Fragment { " \( part) " }
47
- }
43
+ DiscussionPart ( part, for: symbol, in: module)
48
44
}
49
45
50
46
if !documentation. parameters. isEmpty {
@@ -100,27 +96,8 @@ struct Documentation: Component {
100
96
if !documentation. discussionParts. isEmpty {
101
97
fragments. append ( #"""
102
98
<div class="discussion">
103
- \#( documentation. discussionParts. compactMap { part -> HypertextLiteral . HTML ? in
104
- if let part = part as? SwiftMarkup . Documentation . Callout {
105
- return Callout ( part) . html
106
- } else if let part = part as? String {
107
- if part. starts ( with: " ``` " ) ,
108
- let codeBlock = ( try ? CommonMark . Document ( part) ) ? . children. compactMap ( { $0 as? CodeBlock } ) . first,
109
- ( codeBlock. fenceInfo ?? " " ) == " " ||
110
- codeBlock. fenceInfo? . compare ( " swift " , options: . caseInsensitive) == . orderedSame,
111
- let source = codeBlock. literal
112
- {
113
- var html = try ! SwiftSyntaxHighlighter . highlight ( source: source, using: Xcode . self)
114
- html = linkCodeElements ( of: html, for: symbol, in: module)
115
- return HTML ( html)
116
- } else {
117
- var html = ( try ! CommonMark . Document ( part) ) . render ( format: . html, options: [ . unsafe] )
118
- html = linkCodeElements ( of: html, for: symbol, in: module)
119
- return HTML ( html)
120
- }
121
- } else {
122
- return nil
123
- }
99
+ \#( documentation. discussionParts. compactMap { part -> HTML ? in
100
+ DiscussionPart ( part, for: symbol, in: module) . html
124
101
} )
125
102
</div>
126
103
"""# as HypertextLiteral . HTML )
@@ -197,29 +174,86 @@ struct Documentation: Component {
197
174
}
198
175
199
176
extension Documentation {
200
- struct Callout : Component {
201
- var callout : SwiftMarkup . Documentation . Callout
202
-
203
- init ( _ callout: SwiftMarkup . Documentation . Callout ) {
204
- self . callout = callout
177
+ struct DiscussionPart : Component {
178
+ var symbol : Symbol
179
+ var module : Module
180
+ var part : SwiftMarkup . DiscussionPart
181
+
182
+ init ( _ part: SwiftMarkup . DiscussionPart , for symbol: Symbol , in module: Module ) {
183
+ self . part = part
184
+ self . symbol = symbol
185
+ self . module = module
205
186
}
206
187
207
188
// MARK: - Component
208
189
209
190
var fragment : Fragment {
210
- Fragment {
211
- """
212
- > \( callout. delimiter. rawValue. capitalized) : \( callout. content)
213
- """
191
+ switch part {
192
+ case . blockQuote( let blockquote) :
193
+ return Fragment {
194
+ blockquote. render ( format: . commonmark)
195
+ }
196
+ case . callout( let callout) :
197
+ return Fragment {
198
+ BlockQuote {
199
+ " \( callout. delimiter. rawValue. capitalized) : \( callout. content) "
200
+ }
201
+ }
202
+ case . codeBlock( let codeBlock) :
203
+ return Fragment {
204
+ codeBlock. render ( format: . commonmark)
205
+ }
206
+ case . heading( let heading) :
207
+ return Fragment {
208
+ heading. render ( format: . commonmark)
209
+ }
210
+ case . htmlBlock( let htmlBlock) :
211
+ return Fragment {
212
+ htmlBlock. literal ?? " "
213
+ }
214
+ case . list( let list) :
215
+ return Fragment {
216
+ list. render ( format: . commonmark)
217
+ }
218
+ case . paragraph( let paragraph) :
219
+ return Fragment {
220
+ paragraph. render ( format: . commonmark)
221
+ }
214
222
}
215
223
}
216
224
217
225
var html : HypertextLiteral . HTML {
218
- return #"""
219
- <aside class= \#( callout. delimiter. rawValue) >
220
- \#( commonmark: callout. content)
221
- </aside>
222
- """#
226
+ switch part {
227
+ case . blockQuote( let blockquote) :
228
+ return HTML ( blockquote. render ( format: . html, options: [ . unsafe] ) )
229
+ case . callout( let callout) :
230
+ return #"""
231
+ <aside class= \#( callout. delimiter. rawValue) >
232
+ \#( commonmark: callout. content)
233
+ </aside>
234
+ """# as HTML
235
+ case . codeBlock( let codeBlock) :
236
+ if ( codeBlock. fenceInfo ?? " " ) == " " ||
237
+ codeBlock. fenceInfo? . compare ( " swift " , options: . caseInsensitive) == . orderedSame,
238
+ let source = codeBlock. literal
239
+ {
240
+ var html = try ! SwiftSyntaxHighlighter . highlight ( source: source, using: Xcode . self)
241
+ html = linkCodeElements ( of: html, for: symbol, in: module)
242
+ return HTML ( html)
243
+ } else {
244
+ var html = codeBlock. render ( format: . html, options: [ . unsafe] )
245
+ html = linkCodeElements ( of: html, for: symbol, in: module)
246
+ return HTML ( html)
247
+ }
248
+ case . heading( let heading) :
249
+ return HTML ( heading. render ( format: . html, options: [ . unsafe] ) )
250
+ case . htmlBlock( let htmlBlock) :
251
+ return HTML ( htmlBlock. literal ?? " " )
252
+ case . list( let list) :
253
+ return HTML ( list. render ( format: . html, options: [ . unsafe] ) )
254
+ case . paragraph( let paragraph) :
255
+ return HTML ( paragraph. render ( format: . html, options: [ . unsafe] ) )
256
+ }
223
257
}
224
258
}
225
259
}
0 commit comments