swiftuimarkdownattributedstring

Text() is ignoring multiple paragraphs from AttributedString(markdown: ...)


Given the markdown string "**Line 1**\n\nLine 2" I expect an output of

Line 1

Line 2

Instead I get

Line 1Line 2

Surely this isn't a limitation of markdown or AttributedString. What am I missing?! How do I specify multiple paragraphs if not with two blank lines?

struct DemoView_Previews: PreviewProvider {
    static var previews: some View {
        Text(try! AttributedString(markdown: "**Line 1**\n\nLine 2"))
    }
}

Solution

  • As discovered via the Apple Developer forums, .inlineOnlyPreservingWhitespace is needed:

    Text(try! AttributedString(markdown: "**Line 1**\n\nLine 2", 
                               options: AttributedString.MarkdownParsingOptions(interpretedSyntax: 
                                                           .inlineOnlyPreservingWhitespace)))
    

    And, of course, for those that may come along this answer later, it's worth mentioning that if you don't need to use AttributedString directly or aren't passing a variable to Text, you can use the string literal with markdown directly:

    Text("**Line 1**\n\nLine 2")