feat: Working on node builder
This commit is contained in:
46
Sources/CliDoc/Nodes/LabeledContent.swift
Normal file
46
Sources/CliDoc/Nodes/LabeledContent.swift
Normal file
@@ -0,0 +1,46 @@
|
||||
public struct LabeledContent: NodeRepresentable {
|
||||
|
||||
@usableFromInline
|
||||
let label: any NodeRepresentable
|
||||
|
||||
@usableFromInline
|
||||
let content: any NodeRepresentable
|
||||
|
||||
@usableFromInline
|
||||
let separator: any NodeRepresentable
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
separator: (any NodeRepresentable)? = nil,
|
||||
@NodeBuilder label: () -> any NodeRepresentable,
|
||||
@NodeBuilder content: () -> any NodeRepresentable
|
||||
) {
|
||||
self.separator = separator ?? "\n"
|
||||
self.label = label()
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
Group(separator: separator) {
|
||||
label
|
||||
content
|
||||
}.render()
|
||||
}
|
||||
}
|
||||
|
||||
public extension LabeledContent {
|
||||
|
||||
@inlinable
|
||||
init(
|
||||
_ label: String,
|
||||
separator: (any NodeRepresentable)? = nil,
|
||||
@NodeBuilder content: () -> any NodeRepresentable
|
||||
) {
|
||||
self.init(separator: separator) {
|
||||
Text(label)
|
||||
} content: {
|
||||
content()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user