@@ -189,31 +189,22 @@ import SwiftUI
189
189
/// )
190
190
/// ```
191
191
public struct Markdown : View {
192
- private enum Storage : Equatable {
193
- case text( String )
194
- case markdownContent( MarkdownContent )
195
-
196
- var markdownContent : MarkdownContent {
197
- switch self {
198
- case . text( let markdown) :
199
- return MarkdownContent ( markdown)
200
- case . markdownContent( let markdownContent) :
201
- return markdownContent
202
- }
203
- }
204
- }
205
-
206
192
@Environment ( \. colorScheme) private var colorScheme
207
193
@Environment ( \. theme. text) private var text
208
194
209
- @State private var blocks : [ Block ] = [ ]
210
-
211
- private let storage : Storage
195
+ private let content : MarkdownContent
212
196
private let baseURL : URL ?
213
197
private let imageBaseURL : URL ?
214
198
215
- private init ( storage: Storage , baseURL: URL ? , imageBaseURL: URL ? ) {
216
- self . storage = storage
199
+ /// Creates a Markdown view from a Markdown content value.
200
+ /// - Parameters:
201
+ /// - content: The Markdown content value.
202
+ /// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
203
+ /// URLs absolute. The default is `nil`.
204
+ /// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
205
+ /// determine image URLs using the `baseURL` parameter. The default is `nil`.
206
+ public init ( _ content: MarkdownContent , baseURL: URL ? = nil , imageBaseURL: URL ? = nil ) {
207
+ self . content = content
217
208
self . baseURL = baseURL
218
209
self . imageBaseURL = imageBaseURL ?? baseURL
219
210
}
@@ -226,18 +217,13 @@ public struct Markdown: View {
226
217
. modifier ( ScaledFontSizeModifier ( attributes. fontProperties? . size) )
227
218
}
228
219
. textStyle ( self . text)
229
- . onAppear {
230
- // Delay Markdown parsing until the view appears for the first time
231
- if self . blocks. isEmpty {
232
- self . blocks = self . storage. markdownContent. colorScheme ( self . colorScheme) . blocks
233
- }
234
- }
235
- . onChange ( of: self . storage) { storage in
236
- self . blocks = storage. markdownContent. blocks
237
- }
238
220
. environment ( \. baseURL, self . baseURL)
239
221
. environment ( \. imageBaseURL, self . imageBaseURL)
240
222
}
223
+
224
+ private var blocks : [ Block ] {
225
+ self . content. colorScheme ( self . colorScheme) . blocks
226
+ }
241
227
}
242
228
243
229
extension Markdown {
@@ -249,18 +235,7 @@ extension Markdown {
249
235
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
250
236
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
251
237
public init ( _ markdown: String , baseURL: URL ? = nil , imageBaseURL: URL ? = nil ) {
252
- self . init ( storage: . text( markdown) , baseURL: baseURL, imageBaseURL: imageBaseURL)
253
- }
254
-
255
- /// Creates a Markdown view from a Markdown content value.
256
- /// - Parameters:
257
- /// - content: The Markdown content value.
258
- /// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
259
- /// URLs absolute. The default is `nil`.
260
- /// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
261
- /// determine image URLs using the `baseURL` parameter. The default is `nil`.
262
- public init ( _ content: MarkdownContent , baseURL: URL ? = nil , imageBaseURL: URL ? = nil ) {
263
- self . init ( storage: . markdownContent( content) , baseURL: baseURL, imageBaseURL: imageBaseURL)
238
+ self . init ( MarkdownContent ( markdown) , baseURL: baseURL, imageBaseURL: imageBaseURL)
264
239
}
265
240
266
241
/// Creates a Markdown view composed of any number of blocks.
0 commit comments