2

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"))
    }
}
Rumbles
  • 806
  • 1
  • 8
  • 15
  • It works as expected if you use the markdown string directly: `Text("**Line 1**\n\nLine 2")` – jnpdx Mar 18 '22 at 18:12
  • Curious. This appears to be a feature (?!) of string literals and Text(). Doesn't get parsed if you place it into a string variable first. In either event it does not do a full parsing of the string. So, my problem remains... but thanks for the look. – Rumbles Mar 18 '22 at 19:01
  • @Rumbles Have you been able to render both headings and newlines? It seems the hardest of the jobs, what should be a simple thing – Andrea Mario Lufino Jul 03 '23 at 10:41

2 Answers2

4

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")
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Thanks for this. It does "work" insofar as it allows single newlines to act as paragraph separators. However, I must be missing some thing big about the user of markdown... the only syntax that appears supported is bold and italic. Headings, etc do not appear to be parsed at all?! – Rumbles Mar 19 '22 at 17:21
  • Could be. I’d need to experiment more with the available options. If this answers the original question, though, please consider accepting and or upvoting. – jnpdx Mar 19 '22 at 18:34
  • 1
    It so hard to do such simple thing :( – Michał Ziobro Feb 23 '23 at 09:45
  • This *seems* to disregard blockquote markdown syntax: character ">". When I add this attribute it does the the double-asterisk emphasizing of text as expected, but treats ">" like it's "\>". But without this option, though the white space in the original text is disappears, so do the ">" characters, which means it seems to recognize that ">" is markdown. – clearlight Mar 17 '23 at 19:16
-2

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

Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143