I'm trying to implement a markdown renderer using SwiftUI. Markdown document contains a variety of blocks, where a block may be embedded in another block, for example, block quotes:
Quote Level 1
Quote Level 2
Quote Level 3
...
The entire document forms a tree-like structure with arbitrary depth, requiring the renderer to take a recursive approach. I adopted the following code structure:
@ViewBuilder
func renderBlock(block: Block) -> some View {
switch block {
// other types of elements
case let block as BlockQuote:
HStack {
GrayRectangle()
ForEach(block.children) { child in
renderBlock(child) // recursion
}
}
}
}
However, the compiler rejects that as it require the return type to be determined during compile phase. Is it possible to generate dynamic view structure like this in SwiftUI?