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

947 Views Asked by At

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"))
    }
}
2

There are 2 best solutions below

4
jnpdx On BEST ANSWER

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")
0
Michał Ziobro On

Ok it is how it works

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

But if you load text from plist it doesn't work with placing there \n What you need to do is add Enter + Option