Skip to content

Commit 159f5a9

Browse files
authored
Provide access to the language and the content of a code block from the body of the style. (#215)
* Provide access to the language and the content of a code block from the body of the style. * Add deprecations
1 parent 66dccec commit 159f5a9

File tree

8 files changed

+77
-15
lines changed

8 files changed

+77
-15
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import SwiftUI
2+
3+
/// The properties of a Markdown code block.
4+
///
5+
/// The theme ``Theme/codeBlock`` block style receives a `CodeBlockConfiguration`
6+
/// input in its `body` closure.
7+
public struct CodeBlockConfiguration {
8+
/// A type-erased view of a Markdown code block.
9+
public struct Label: View {
10+
init<L: View>(_ label: L) {
11+
self.body = AnyView(label)
12+
}
13+
14+
public let body: AnyView
15+
}
16+
17+
/// The code block language, if present.
18+
public let language: String?
19+
20+
/// The code block contents.
21+
public let content: String
22+
23+
/// The code block view.
24+
public let label: Label
25+
}

Sources/MarkdownUI/Theme/Theme+Basic.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ extension Theme {
8080
.relativePadding(.leading, length: .em(2))
8181
.relativePadding(.trailing, length: .em(1))
8282
}
83-
.codeBlock { label in
83+
.codeBlock { configuration in
8484
ScrollView(.horizontal) {
85-
label
85+
configuration.label
8686
.relativeLineSpacing(.em(0.15))
8787
.relativePadding(.leading, length: .rem(1))
8888
.markdownTextStyle {

Sources/MarkdownUI/Theme/Theme+DocC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ extension Theme {
9292
}
9393
.markdownMargin(top: .em(1.6), bottom: .zero)
9494
}
95-
.codeBlock { label in
95+
.codeBlock { configuration in
9696
ScrollView(.horizontal) {
97-
label
97+
configuration.label
9898
.relativeLineSpacing(.em(0.333335))
9999
.markdownTextStyle {
100100
FontFamilyVariant(.monospaced)

Sources/MarkdownUI/Theme/Theme+GitHub.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ extension Theme {
109109
}
110110
.fixedSize(horizontal: false, vertical: true)
111111
}
112-
.codeBlock { label in
112+
.codeBlock { configuration in
113113
ScrollView(.horizontal) {
114-
label
114+
configuration.label
115115
.relativeLineSpacing(.em(0.225))
116116
.markdownTextStyle {
117117
FontFamilyVariant(.monospaced)

Sources/MarkdownUI/Theme/Theme.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public struct Theme {
162162
public var blockquote = BlockStyle()
163163

164164
/// The code block style.
165-
public var codeBlock = BlockStyle()
165+
public var codeBlock: BlockStyle<CodeBlockConfiguration> = BlockStyle { $0.label }
166166

167167
/// The image style.
168168
public var image = BlockStyle()
@@ -186,14 +186,10 @@ public struct Theme {
186186
public var table = BlockStyle()
187187

188188
/// The table cell style.
189-
public var tableCell: BlockStyle<TableCellConfiguration> = BlockStyle { configuration in
190-
configuration.label
191-
}
189+
public var tableCell: BlockStyle<TableCellConfiguration> = BlockStyle { $0.label }
192190

193191
/// The thematic break style.
194-
public var thematicBreak = BlockStyle {
195-
Divider()
196-
}
192+
public var thematicBreak = BlockStyle { Divider() }
197193

198194
/// Creates a theme with default text styles.
199195
public init() {}
@@ -333,7 +329,7 @@ extension Theme {
333329
/// Adds a code block style to the theme.
334330
/// - Parameter body: A view builder that returns a customized code block.
335331
public func codeBlock<Body: View>(
336-
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
332+
@ViewBuilder body: @escaping (_ configuration: CodeBlockConfiguration) -> Body
337333
) -> Theme {
338334
var theme = self
339335
theme.codeBlock = .init(body: body)

Sources/MarkdownUI/Utility/Deprecations.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
import SwiftUI
22

3+
// MARK: - Deprecated after 2.0.2:
4+
5+
extension View {
6+
@available(*, deprecated, message: "Use the version of this function that takes a closure receiving a generic 'Configuration' value.")
7+
public func markdownBlockStyle<Body: View>(
8+
_ keyPath: WritableKeyPath<Theme, BlockStyle<CodeBlockConfiguration>>,
9+
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
10+
) -> some View {
11+
self.environment(
12+
(\EnvironmentValues.theme).appending(path: keyPath),
13+
.init { configuration in
14+
body(.init(configuration.label))
15+
}
16+
)
17+
}
18+
}
19+
20+
extension Theme {
21+
@available(*, deprecated, message: "Use the version of this function that takes a closure receiving a 'CodeBlockConfiguration' value.")
22+
public func codeBlock<Body: View>(
23+
@ViewBuilder body: @escaping (_ label: BlockConfiguration.Label) -> Body
24+
) -> Theme {
25+
var theme = self
26+
theme.codeBlock = .init { configuration in
27+
body(.init(configuration.label))
28+
}
29+
return theme
30+
}
31+
}
32+
333
// MARK: - Unavailable after 1.1.1:
434

535
extension Heading {

Sources/MarkdownUI/Views/Blocks/BlockNode+View.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension BlockNode: View {
1212
case .taskList(let isTight, let items):
1313
ApplyBlockStyle(\.list, to: TaskListView(isTight: isTight, items: items))
1414
case .codeBlock(let fenceInfo, let content):
15-
ApplyBlockStyle(\.codeBlock, to: CodeBlockView(fenceInfo: fenceInfo, content: content))
15+
CodeBlockView(fenceInfo: fenceInfo, content: content)
1616
case .htmlBlock(let content):
1717
ApplyBlockStyle(\.paragraph, to: HTMLBlockView(content: content))
1818
case .paragraph(let content):

Sources/MarkdownUI/Views/Blocks/CodeBlockView.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwiftUI
22

33
struct CodeBlockView: View {
4+
@Environment(\.theme.codeBlock) private var codeBlock
45
@Environment(\.codeSyntaxHighlighter) private var codeSyntaxHighlighter
56

67
private let fenceInfo: String?
@@ -12,6 +13,16 @@ struct CodeBlockView: View {
1213
}
1314

1415
var body: some View {
16+
codeBlock.makeBody(
17+
configuration: .init(
18+
language: self.fenceInfo,
19+
content: self.content,
20+
label: .init(self.label)
21+
)
22+
)
23+
}
24+
25+
private var label: some View {
1526
self.codeSyntaxHighlighter.highlightCode(self.content, language: self.fenceInfo)
1627
.textStyleFont()
1728
}

0 commit comments

Comments
 (0)