gonzalezreal / swift-markdown-ui

Display and customize Markdown text in SwiftUI
MIT License
2.25k stars 268 forks source link

Handle line breaks #92

Closed Urkman closed 2 years ago

Urkman commented 2 years ago

For me MarkdownUI is not rendering line breaks correctly. A single line break is ignored and the text is displayed in one line. Double line breaks are looking fine.

How can I handle this?

gonzalezreal commented 2 years ago

Hi @Urkman,

You can have a look at the CommonMark spec to learn how to create hard line breaks in Markdown.

Urkman commented 2 years ago

Thanks for you answer :) My Problem is, that I get the content from a backend and there we habe simple "\n" as our line breaks. So, there is no chance to convert them?

When I use the building markdown parser like this: AttributedString(markdown: self, options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))

It is displayed fine, bat as we know, the building parser does noch display list and other stuff :(

gonzalezreal commented 2 years ago

Hi, Sorry, at the moment, that is not possible with MarkdownUI.

colintheshots commented 2 years ago

Might I suggest as a workaround until the library supports this, run the following regex before displaying.

I'd recommend eventually adding support for soft breaks as hard, since this is so highly demanded that it's even called out in the CommonMark spec section on soft breaks.

s/(?<!  |\n)\n(?!\n|\* |\d\.)/  \n/g
SilenceLove commented 1 year ago

how to do this

PallavAg commented 1 year ago

I had to manually fix my strings by doing:

str = str.replacingOccurrences(of: "\n", with: "  \n")

This then renders newlines.

hstdt commented 11 months ago

I had to manually fix my strings by doing:

str = str.replacingOccurrences(of: "\n", with: "  \n")

This then renders newlines.

Great workaround. replacingOccurrences(of: "\n", with: " \n", options: .regularExpression) should be better😁

hugo53 commented 8 months ago

replacingOccurrences(of: "\n", with: " \n", options: .regularExpression)

Thank you for this great workaround.