Skip to content

Commit e91ce05

Browse files
committed
add a synchronous placeholder() function to InlineImageProvider
1 parent a9c7615 commit e91ce05

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Sources/MarkdownUI/Extensibility/InlineImageProvider.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public protocol InlineImageProvider {
1313
/// - url: The URL of the image to display.
1414
/// - label: The accessibility label associated with the image.
1515
func image(with url: URL, label: String) async throws -> Image
16+
17+
func placeholder() -> Text?
18+
}
19+
20+
extension InlineImageProvider {
21+
public func placeholder() -> Text? { nil }
1622
}

Sources/MarkdownUI/Renderer/TextInlineRenderer.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ extension Sequence where Element == InlineNode {
66
textStyles: InlineTextStyles,
77
images: [String: Image],
88
softBreakMode: SoftBreak.Mode,
9-
attributes: AttributeContainer
9+
attributes: AttributeContainer,
10+
placeholderImage: Text?
1011
) -> Text {
1112
var renderer = TextInlineRenderer(
1213
baseURL: baseURL,
1314
textStyles: textStyles,
1415
images: images,
1516
softBreakMode: softBreakMode,
16-
attributes: attributes
17+
attributes: attributes,
18+
placeholderImage: placeholderImage
1719
)
1820
renderer.render(self)
1921
return renderer.result
@@ -28,20 +30,23 @@ private struct TextInlineRenderer {
2830
private let images: [String: Image]
2931
private let softBreakMode: SoftBreak.Mode
3032
private let attributes: AttributeContainer
33+
private let placeholderImage: Text?
3134
private var shouldSkipNextWhitespace = false
3235

3336
init(
3437
baseURL: URL?,
3538
textStyles: InlineTextStyles,
3639
images: [String: Image],
3740
softBreakMode: SoftBreak.Mode,
38-
attributes: AttributeContainer
41+
attributes: AttributeContainer,
42+
placeholderImage: Text?
3943
) {
4044
self.baseURL = baseURL
4145
self.textStyles = textStyles
4246
self.images = images
4347
self.softBreakMode = softBreakMode
4448
self.attributes = attributes
49+
self.placeholderImage = placeholderImage
4550
}
4651

4752
mutating func render<S: Sequence>(_ inlines: S) where S.Element == InlineNode {
@@ -103,6 +108,8 @@ private struct TextInlineRenderer {
103108
private mutating func renderImage(_ source: String) {
104109
if let image = self.images[source] {
105110
self.result = self.result + Text(image)
111+
} else if let placeholderImage {
112+
self.result = self.result + placeholderImage
106113
}
107114
}
108115

Sources/MarkdownUI/Views/Inlines/InlineText.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ struct InlineText: View {
2828
),
2929
images: self.inlineImages,
3030
softBreakMode: self.softBreakMode,
31-
attributes: attributes
31+
attributes: attributes,
32+
placeholderImage: self.inlineImageProvider.placeholder()
3233
)
3334
}
3435
.task(id: self.inlines) {

0 commit comments

Comments
 (0)