New lines not being rendered in SwiftUI's markdown

I'm trying the new markdown functionality in SwiftUI but it seems that trying to render markdown from an Attributed String inside a variable strips away any new lines.

This isn't the case when placing the raw markdown directly in the Text View's initialiser, as shown in the preview below.

Is this a bug or is there a way to retain new lines when using Attributed String? None of the options seem to help.

Thanks!

import SwiftUI

struct markdown_test: View {

    let attStr = try! AttributedString(markdown: """

Hello **World** 👋🏻      \n\n\n\n

[This is a test](www.google.co.uk)
""")

    var body: some View {

        VStack(alignment: .leading) {
            Text(attStr)
            Text("Hello **World** 👋🏻\n[This is a test](www.google.co.uk)")
        }

    }

}

Accepted Reply

This issue appears to have been fixed on Monterey Beta 3.

You have to add the .inlineOnlyPreservingWhitespace option when initialising the AttributedString.

struct MarkdownTest: View {

    let attrStr = try! AttributedString(markdown: "Hello **World**!\n[This is a link](www.google.com)", options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))

    var body: some View {
        Text(attrStr)
            .padding()
    }
} 
  • Perfect answer!

  • Unfortunately this does not really help as the inlineOnlyPreservingWhitespace option discards the paragraph markups

  • Seems like Gregor-Karl is right. .inlinePreservingOnlyWhiteSpace preserves the spacing, and highlights bold-marked text (e.g. text to emphasize), but it leaves, for example, the block quote markup indicators ">" unprocessed as though a literal part of the text.

Add a Comment

Replies

The CommonMark specification says that multiple newlines are equivalent to a single paragraph break, which is what you're seeing here:

Multiple blank lines between paragraph (sic) have no effect.

— 4.8 Paragraph, from v. 0.29

  • Thanks for your response. A single paragraph break would be fine but I haven't found a way to add one in an attributed string with SwiftUI.

    AttributedString(markdown: "Example\n**One**") renders on a single line in a Text view.

    Whereas Text("Example\n**Two**") renders on two, as expected.

  • I don't believe the original poster is asking about multiple line breaks in an AttributedString. He/She is asking about any line breaks using the AttributedString initializer with markdown.

    I've seen this same issue. When using an AttributedString initialized with markdown, any and all line breaks are stripped from the result Text object displayed on the screen. I went ahead and submitted a feedback issue with a sample project. The feedback number is FB9190672..

  • Thanks @rwgrier. I also submitted an example Playground (FB9188344) to demonstrate the issue

Add a Comment

This issue appears to have been fixed on Monterey Beta 3.

You have to add the .inlineOnlyPreservingWhitespace option when initialising the AttributedString.

struct MarkdownTest: View {

    let attrStr = try! AttributedString(markdown: "Hello **World**!\n[This is a link](www.google.com)", options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))

    var body: some View {
        Text(attrStr)
            .padding()
    }
} 
  • Perfect answer!

  • Unfortunately this does not really help as the inlineOnlyPreservingWhitespace option discards the paragraph markups

  • Seems like Gregor-Karl is right. .inlinePreservingOnlyWhiteSpace preserves the spacing, and highlights bold-marked text (e.g. text to emphasize), but it leaves, for example, the block quote markup indicators ">" unprocessed as though a literal part of the text.

Add a Comment

Add two spaces at the end of the line and then add an escape character ' \n'.

AttributedString( markdown: "**Thank you!** Please visit our [website](https://example.com)  \nThis is second line")

Output: