johnxnguyen / Down

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Other
2.24k stars 319 forks source link

Newlines ("\n") get lost and replaced with a whitespace #269

Open ndemie opened 3 years ago

ndemie commented 3 years ago

Please help prevent duplicate issues before submitting a new one:

Report

Newlines seem to get lost when creating an attributed string using DownStyler and get replaced with a whitespace

What did you do?

let string = "Test\nTest"
let down = Down(markdownString: string)
do {
    let attributedString = try down.toAttributedString(styler: DownStyler(configuration: .testConfiguration)
    print(attributedString.string)
}

"Test Test"

What did you expect to happen?

print(attributedString.string)

"Test\nTest"

Before this we used a stylesheet which didn't cause this issue. I've tried replacing newlines by double newlines but that renders two newlines causing too much spacing.

johnxnguyen commented 3 years ago

Hi @ndemie , so there is a specific option to use to treat these "soft breaks" (what you're getting) as "hard breaks" (what you want). By default, all single newlines are considered soft breaks. You want to pass in this option to the parser, so something like:

let attributedString = try down.toAttributedString(.hardBreaks, styler: DownStyler(configuration: .testConfiguration)

Let me know how it goes.

ndemie commented 3 years ago

That fixes it, thanks!

ndemie commented 3 years ago

Multiple breaks still seem to get lost though.

Test\nTest\n\nTest gets rendered as

Test
Test
Test

while I would expect

Test
Test

Test

Any solution for this @johnxnguyen?

ndemie commented 3 years ago

Also, unrelated (so let me know if you want this in a seperate issue), but none of the custom glyph's seem to get rendered for me e.g. codeBlockBackground, quoteStripe and thematicBreak. Any clue where the issue could be?

johnxnguyen commented 3 years ago

@ndemie those custom attributes require the use of the DownLayoutManager, which understands what they are and is capable of drawing them in the text view. In short, you need to insert an instance of this object in your text field stack. Check out the implementation of DownTextView, or open a new issue and we can discuss it in more detail there.

johnxnguyen commented 3 years ago

@ndemie regarding the line break issue, I'll need to try it out myself and do some debugging, not sure if it's an issue with the parsing or the styling. I have a bit of a backlog of things to do for this repo so I won't be able to jump on this immediately. Feel free to come back and remind me if there's no activity here.

applypost commented 2 years ago

Multiple breaks isn't working for me either with the solution.

Is the fix still in the backlog?

ndemie commented 2 years ago

Any update on this @johnxnguyen?

ndemie commented 2 years ago

Hi @johnxnguyen, it's been a while. I was wondering if you had the chance to take a look at this yet?

SilenceLove commented 1 year ago

Do you still have time to fix this issue? @johnxnguyen

YBJust commented 5 months ago

I have encountered the same problem, and this problem has a great impact on me. Do you have time to solve it? Thanks very much!

daehn commented 3 months ago

Running into this issue as well, is this currently unsupported or are there ways to work around this @johnxnguyen? Hope your doing well btw, has been a while!

daehn commented 3 months ago

I was able to fix rendering multiple line breaks by adjusting my custom styler to style paragraphs accordingly.

open func style(paragraph str: NSMutableAttributedString) {
    let style = NSMutableParagraphStyle()
    style.paragraphSpacing = 20
    str.addAttributes([.paragraphStyle: style], range: NSRange(location: 0, length: str.length))
}
johnxnguyen commented 3 months ago

Hi all, apologies for the long delay, I have not had any capacity to work on the project for a long time. Having said that I'm trying to find some time to address current issues.

Coming back to

Multiple breaks still seem to get lost though.

Test\nTest\n\nTest gets rendered as

Test
Test
Test

while I would expect

Test
Test

Test

Any solution for this @johnxnguyen?

@ndemie I think that the markdown Test\nTest\n\nTest is equivalent to

Test
Test

Test

which according to the Commonmark spec, all multiple empty lines between paragraphs are ignored. The spec also says that if you want hard line breaks to create extra spaces in between, you would write:

Test\
Test\
\
\
\
Test

which would render as

Test
Test

Test

Could you please try with this markdown syntax and see if you still have issues?

cc @SilenceLove @YBJust @daehn