Skip to content

Commit f2989c7

Browse files
committed
Allow customizing soft break mode
Resolved #341
1 parent 9a8119b commit f2989c7

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Sources/MarkdownUI/DSL/Inlines/SoftBreak.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ public struct SoftBreak: InlineContentProtocol {
1111
.init(inlines: [.softBreak])
1212
}
1313
}
14+
15+
extension SoftBreak {
16+
public enum Mode {
17+
/// Treat a soft break like a space
18+
case space
19+
20+
/// Treat a soft break like a new line
21+
case newLine
22+
}
23+
}

Sources/MarkdownUI/Renderer/TextInlineRenderer.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ extension Sequence where Element == InlineNode {
55
baseURL: URL?,
66
textStyles: InlineTextStyles,
77
images: [String: Image],
8+
softBreakMode: SoftBreak.Mode,
89
attributes: AttributeContainer
910
) -> Text {
1011
var renderer = TextInlineRenderer(
1112
baseURL: baseURL,
1213
textStyles: textStyles,
1314
images: images,
15+
softBreakMode: softBreakMode,
1416
attributes: attributes
1517
)
1618
renderer.render(self)
@@ -24,18 +26,21 @@ private struct TextInlineRenderer {
2426
private let baseURL: URL?
2527
private let textStyles: InlineTextStyles
2628
private let images: [String: Image]
29+
private let softBreakMode: SoftBreak.Mode
2730
private let attributes: AttributeContainer
2831
private var shouldSkipNextWhitespace = false
2932

3033
init(
3134
baseURL: URL?,
3235
textStyles: InlineTextStyles,
3336
images: [String: Image],
37+
softBreakMode: SoftBreak.Mode,
3438
attributes: AttributeContainer
3539
) {
3640
self.baseURL = baseURL
3741
self.textStyles = textStyles
3842
self.images = images
43+
self.softBreakMode = softBreakMode
3944
self.attributes = attributes
4045
}
4146

@@ -72,10 +77,16 @@ private struct TextInlineRenderer {
7277
}
7378

7479
private mutating func renderSoftBreak() {
75-
if self.shouldSkipNextWhitespace {
76-
self.shouldSkipNextWhitespace = false
77-
} else {
78-
self.defaultRender(.softBreak)
80+
switch self.softBreakMode {
81+
case .space:
82+
if self.shouldSkipNextWhitespace {
83+
self.shouldSkipNextWhitespace = false
84+
} else {
85+
self.defaultRender(.softBreak)
86+
}
87+
case .newLine:
88+
self.shouldSkipNextWhitespace = true
89+
self.defaultRender(.lineBreak)
7990
}
8091
}
8192

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import SwiftUI
2+
3+
extension View {
4+
/// Sets the soft break mode for inline texts in a view hierarchy.
5+
///
6+
/// - parameter softBreakMode: If set to `space`, treats all soft breaks as spaces, keeping sentences whole. If set to `newLine`, treats soft breaks as full line breaks
7+
///
8+
/// - Returns: A view that uses the specified soft break mode for itself and its child views.
9+
public func softBreakMode(_ softBreakMode: SoftBreak.Mode) -> some View {
10+
self.environment(\.softBreakMode, softBreakMode)
11+
}
12+
}
13+
14+
extension EnvironmentValues {
15+
var softBreakMode: SoftBreak.Mode {
16+
get { self[SoftBreakModeKey.self] }
17+
set { self[SoftBreakModeKey.self] = newValue }
18+
}
19+
}
20+
21+
private struct SoftBreakModeKey: EnvironmentKey {
22+
static let defaultValue: SoftBreak.Mode = .space
23+
}

Sources/MarkdownUI/Views/Inlines/InlineText.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ struct InlineText: View {
44
@Environment(\.inlineImageProvider) private var inlineImageProvider
55
@Environment(\.baseURL) private var baseURL
66
@Environment(\.imageBaseURL) private var imageBaseURL
7+
@Environment(\.softBreakMode) private var softBreakMode
78
@Environment(\.theme) private var theme
89

910
@State private var inlineImages: [String: Image] = [:]
@@ -26,6 +27,7 @@ struct InlineText: View {
2627
link: self.theme.link
2728
),
2829
images: self.inlineImages,
30+
softBreakMode: self.softBreakMode,
2931
attributes: attributes
3032
)
3133
}

0 commit comments

Comments
 (0)