Skip to content

Commit

Permalink
Allow customizing soft break mode
Browse files Browse the repository at this point in the history
Resolved #341
  • Loading branch information
freak4pc committed Aug 9, 2024
1 parent 9a8119b commit f2989c7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Sources/MarkdownUI/DSL/Inlines/SoftBreak.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ public struct SoftBreak: InlineContentProtocol {
.init(inlines: [.softBreak])
}
}

extension SoftBreak {
public enum Mode {
/// Treat a soft break like a space
case space

/// Treat a soft break like a new line
case newLine
}
}
19 changes: 15 additions & 4 deletions Sources/MarkdownUI/Renderer/TextInlineRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ extension Sequence where Element == InlineNode {
baseURL: URL?,
textStyles: InlineTextStyles,
images: [String: Image],
softBreakMode: SoftBreak.Mode,
attributes: AttributeContainer
) -> Text {
var renderer = TextInlineRenderer(
baseURL: baseURL,
textStyles: textStyles,
images: images,
softBreakMode: softBreakMode,
attributes: attributes
)
renderer.render(self)
Expand All @@ -24,18 +26,21 @@ private struct TextInlineRenderer {
private let baseURL: URL?
private let textStyles: InlineTextStyles
private let images: [String: Image]
private let softBreakMode: SoftBreak.Mode
private let attributes: AttributeContainer
private var shouldSkipNextWhitespace = false

init(
baseURL: URL?,
textStyles: InlineTextStyles,
images: [String: Image],
softBreakMode: SoftBreak.Mode,
attributes: AttributeContainer
) {
self.baseURL = baseURL
self.textStyles = textStyles
self.images = images
self.softBreakMode = softBreakMode
self.attributes = attributes
}

Expand Down Expand Up @@ -72,10 +77,16 @@ private struct TextInlineRenderer {
}

private mutating func renderSoftBreak() {
if self.shouldSkipNextWhitespace {
self.shouldSkipNextWhitespace = false
} else {
self.defaultRender(.softBreak)
switch self.softBreakMode {
case .space:
if self.shouldSkipNextWhitespace {
self.shouldSkipNextWhitespace = false
} else {
self.defaultRender(.softBreak)
}
case .newLine:
self.shouldSkipNextWhitespace = true
self.defaultRender(.lineBreak)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import SwiftUI

extension View {
/// Sets the soft break mode for inline texts in a view hierarchy.
///
/// - 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
///
/// - Returns: A view that uses the specified soft break mode for itself and its child views.
public func softBreakMode(_ softBreakMode: SoftBreak.Mode) -> some View {
self.environment(\.softBreakMode, softBreakMode)
}
}

extension EnvironmentValues {
var softBreakMode: SoftBreak.Mode {
get { self[SoftBreakModeKey.self] }
set { self[SoftBreakModeKey.self] = newValue }
}
}

private struct SoftBreakModeKey: EnvironmentKey {
static let defaultValue: SoftBreak.Mode = .space
}
2 changes: 2 additions & 0 deletions Sources/MarkdownUI/Views/Inlines/InlineText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct InlineText: View {
@Environment(\.inlineImageProvider) private var inlineImageProvider
@Environment(\.baseURL) private var baseURL
@Environment(\.imageBaseURL) private var imageBaseURL
@Environment(\.softBreakMode) private var softBreakMode
@Environment(\.theme) private var theme

@State private var inlineImages: [String: Image] = [:]
Expand All @@ -26,6 +27,7 @@ struct InlineText: View {
link: self.theme.link
),
images: self.inlineImages,
softBreakMode: self.softBreakMode,
attributes: attributes
)
}
Expand Down

0 comments on commit f2989c7

Please sign in to comment.