Markdown parser should respect single newlines #5273

Closed
opened 2025-11-02 06:19:56 -06:00 by GiteaMirror · 9 comments
Owner

Originally created by @silverwind on GitHub (Apr 20, 2020).

Given markdown

a
b

GitHub renders two lines because of <br>:

<p>a<br>
b</p>

Gitea renders one line because \n is converted to according to HMTL whitespace rules:

<p>a
b</p>

Might be a divergation from the spec in GFM, but it'd be more intuitive if we'd insert those <br> and trim the newline off the <p>. Alternatively, adding white-space: pre on the <p> in CSS would also work but may have more unintended side effects.

Originally created by @silverwind on GitHub (Apr 20, 2020). Given markdown ```md a b ``` GitHub renders two lines because of `<br>`: ```html <p>a<br> b</p> ``` Gitea renders one line because `\n` is converted to ` ` according to HMTL whitespace rules: ```html <p>a b</p> ``` Might be a divergation from the spec in GFM, but it'd be more intuitive if we'd insert those `<br>` and trim the newline off the `<p>`. Alternatively, adding `white-space: pre` on the `<p>` in CSS would also work but may have more unintended side effects.
GiteaMirror added the topic/ui label 2025-11-02 06:19:56 -06:00
Author
Owner

@zeripath commented on GitHub (Apr 20, 2020):

@yuin what do you think about this? Is this an expected GFM diversion from the spec?

@zeripath commented on GitHub (Apr 20, 2020): @yuin what do you think about this? Is this an expected GFM diversion from the spec?
Author
Owner

@silverwind commented on GitHub (Apr 20, 2020):

Relevant section of GFM:

https://github.github.com/gfm/#soft-line-breaks

Specifically this seems to be in use:

A renderer may also provide an option to render soft line breaks as hard line breaks.

@silverwind commented on GitHub (Apr 20, 2020): Relevant section of GFM: https://github.github.com/gfm/#soft-line-breaks Specifically this seems to be in use: > A renderer may also provide an option to render soft line breaks as hard line breaks.
Author
Owner

@silverwind commented on GitHub (Apr 20, 2020):

Goldmark already has option WithHardWraps which makes the rendering match GitHub.

@silverwind commented on GitHub (Apr 20, 2020): Goldmark already has [option `WithHardWraps`](https://github.com/yuin/goldmark#html-renderer-options) which makes the rendering match GitHub.
Author
Owner

@silverwind commented on GitHub (Apr 20, 2020):

Further investigation reveals we already have option markdown.ENABLE_HARD_LINE_BREAK that maps to WithHardWraps but it defaults to false. I think we should flip that default.

@silverwind commented on GitHub (Apr 20, 2020): Further investigation reveals we already have option `markdown.ENABLE_HARD_LINE_BREAK` that maps to `WithHardWraps` but it defaults to `false`. I think we should flip that default.
Author
Owner

@zeripath commented on GitHub (Apr 20, 2020):

ah yeah now I recall. I bet this was because of some blackfriday default. I suspect our unit tests expect not HardWraps.

@zeripath commented on GitHub (Apr 20, 2020): ah yeah now I recall. I bet this was because of some blackfriday default. I suspect our unit tests expect not HardWraps.
Author
Owner

@silverwind commented on GitHub (Apr 20, 2020):

Will follow up with a PR that changes the default and improve the documentation around that option.

@silverwind commented on GitHub (Apr 20, 2020): Will follow up with a PR that changes the default and improve the documentation around that option.
Author
Owner

@a1012112796 commented on GitHub (Apr 20, 2020):

Hello, I don't think it's a wrong thing, Because in markdown usually format rulers, we should have more than two spaces after words, or mardwon won't regard it as a new line:
view:
jm1
jm2
Do you think so? Thanks.

@a1012112796 commented on GitHub (Apr 20, 2020): Hello, I don't think it's a wrong thing, Because in markdown usually format rulers, we should have more than two spaces after words, or mardwon won't regard it as a new line: view: <img width="254" alt="jm1" src="https://user-images.githubusercontent.com/25342410/79775056-3eb25980-8366-11ea-9237-ba7d32d709e7.png"> <img width="218" alt="jm2" src="https://user-images.githubusercontent.com/25342410/79775070-45d96780-8366-11ea-9522-ce1b51146957.png"> Do you think so? Thanks.
Author
Owner

@silverwind commented on GitHub (Apr 20, 2020):

The trailing whitespace "trick" is needed for the original Markdown version but in GFM and CommonMark soft-breaks can optionally be treated as hard-breaks and this is what I'm proposing to default to because it seems the most intuitive.

@silverwind commented on GitHub (Apr 20, 2020): The trailing whitespace "trick" is needed for the original Markdown version but in GFM and CommonMark soft-breaks can optionally be treated as hard-breaks and this is what I'm proposing to default to because it seems the most intuitive.
Author
Owner

@yuin commented on GitHub (Apr 21, 2020):

@zeripath As @silverwind mentioned, GFM defines rendering soft line breaks as hard line breaks as an optional functionality. goldmark has the WithHardWraps option. goldmark users will have the decision on which turning it on.

@yuin commented on GitHub (Apr 21, 2020): @zeripath As @silverwind mentioned, GFM defines rendering soft line breaks as hard line breaks as an **optional** functionality. goldmark has the WithHardWraps option. goldmark users will have the decision on which turning it on.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#5273