glow icon indicating copy to clipboard operation
glow copied to clipboard

Support hard line breaks

Open jordanbtucker opened this issue 3 years ago • 32 comments

Please add support for hard line breaks.

https://spec.commonmark.org/0.30/#hard-line-breaks

jordanbtucker avatar Dec 01 '20 03:12 jordanbtucker

Yes, please.

tricarte avatar Dec 14 '20 17:12 tricarte

I too endorse this. To reproduce:

$ echo "hello\nworld" > test.md
$ cat test.md
hello
world

whereas

$ glow test.md
Screenshot 2021-05-22 at 13 24 43

gennaro-tedesco avatar May 22 '21 11:05 gennaro-tedesco

Wait... isn't markdown newline equal to two enters?

wantyapps avatar May 24 '21 08:05 wantyapps

@gennaro-tedesco Your code should not work. In order to have a single line break be interpreted as a new line, you need to precede it with at least two spaces, like this (select the text to see that there are two spaces after hello):

hello  
world

Alternatively, you can end your line with a backslash \ like this:

hello\
world

Neither of the above examples work in glow though, which is why I created this issue.

@wantyapps

Wait... isn't markdown newline equal to two enters?

Not quite. Two line breaks in Markdown result in a new <p> element whereas a line break preceded by either two or more spaces or a \ results in a <br> element.

Compare Paragraphs with Hard line breaks.

jordanbtucker avatar May 24 '21 14:05 jordanbtucker

Thank you for the clarification @jordanbtucker, I did not know, so bad on my side; however I do confirm that neither of the above (proper) examples work in Glow.

gennaro-tedesco avatar May 25 '21 11:05 gennaro-tedesco

You're right, we're reflowing the text, but I think we can make that optional fairly easily.

muesli avatar May 25 '21 12:05 muesli

@gennaro-tedesco Your code should not work. In order to have a single line break be interpreted as a new line, you need to precede it with at least two spaces, like this (select the text to see that there are two spaces after hello):


hello  

world

Alternatively, you can end your line with a backslash \ like this:


hello\

world

Neither of the above examples work in glow though, which is why I created this issue.

@wantyapps

Wait... isn't markdown newline equal to two enters?

Not quite. Two line breaks in Markdown result in a new <p> element whereas a line break preceded by either two or more spaces or a \ results in a <br> element.

Compare Paragraphs with Hard line breaks.

Thank you for the clarification.

wantyapps avatar May 25 '21 12:05 wantyapps

This is pretty crucial for me, I'm really enjoying glow for reading my markdown notes in terminal, but I use double-space line breaks all over the place and right now there's no way to get a single line break to render via glow at all as far as I can tell!

seankhl avatar Jul 15 '21 08:07 seankhl

Is there a reason this has not been implemented yet? This makes glow render markdown incorrectly, in a pretty serious way

magnus-ISU avatar Sep 05 '21 23:09 magnus-ISU

Looks like this is solved in the upstream dependency https://github.com/charmbracelet/glamour/pull/115, I updated glamour version in go.mod: github.com/charmbracelet/glamour v0.3.1-0.20210906181425-86a99924d7f6

and added:

diff --git a/main.go b/main.go
index b36d902..1852704 100644
--- a/main.go
+++ b/main.go
@@ -248,6 +248,7 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
                gs,
                glamour.WithWordWrap(int(width)),
                glamour.WithBaseURL(baseURL),
+               glamour.WithPreservedNewLines(),
        )
        if err != nil {
                return err

and my example file test.md renders correctly:

hello  
world
$ glow test.md
hello
world

@muesli @meowgorithm Any chance we could see a new release for glow addressing this issue? Looks simple enough, though I'm probably overlooking something (I'm new to go/the project)

fiskhest avatar Sep 17 '21 09:09 fiskhest

@fiskhest Working on it! Just not sure yet if we shouldn't keep the current setting as our default.

muesli avatar Sep 17 '21 10:09 muesli

That's great, thank you!

I might be misunderstanding your statement, but the current setting is not adhering to the markdown specification and as such should not be the default. If anything, the opposite should be the default, and the current behavior require setting a param.

fiskhest avatar Sep 17 '21 10:09 fiskhest

I agree that the current behaviour shouldn't be preserved, I don't know why you would even offer a compatability flag with the current output, as it isn't standard and I really doubt there is anyone who wanted the behaviour of two-spaces-ending-a-line to not be a line break. Seems like adding needless complexity.

Also, does it also work with backslashes ending a line rather than 2 spaces?

magnus-ISU avatar Sep 17 '21 19:09 magnus-ISU

Any updates on this matter @muesli ?

fiskhest avatar Nov 04 '21 09:11 fiskhest

Also curious about updates on this? @muesli I agree that this behavior should be the default. This issue unfortunately makes using Glow unusable for me. It's a pretty significant compatibility issue.

jjc224 avatar May 07 '22 14:05 jjc224

I think I have the same behaviour. A <br> tag is shown as a double space rather than a break line in glow.

$ cat test.md
Hello
<br>
World

Expectation:

$ glow test.md
  Hello
  World

Reality:

$ glow test.md

  Hello  World

I don't know if this is directly linked to this issue as you guys seem to talk about double space and not specifically about <br> tags but that's the same thing right ? (Sorry if I'm wrong).

Anyway, I don't know if it is still planned to fix this (as it has been raised a long time ago) but I would also be interested by a fix :) I wish I could help more with that...

By the way, thanks for your amazing work !

Antiz96 avatar May 23 '22 11:05 Antiz96

@Antiz96 That is a separate but valid issue. I recommend opening an issue for it in the glamour repo, which is the code base that performs the markdown conversion. Technically, this issue should be there too.

jordanbtucker avatar May 23 '22 15:05 jordanbtucker

@jordanbtucker Thanks for your answer ! I didn't knew about glamour. After looking at the currently opened issues in the glamour repo, it seems like a similar issue has already been raised here. It is mostly about double spaces and back slashes, not specifically about <br> tags tho... But if at least one of those 3 options works, I'd be fine with it :)

I'm still going to open an issue anyway in order to get it documented.

Thanks again !

Antiz96 avatar May 23 '22 16:05 Antiz96

@jordanbtucker technically this issue is solved in glamour, only need a default setting flipped in glow.

fiskhest avatar May 23 '22 16:05 fiskhest

@fiskhest @jordanbtucker So should I still post in glamour repo or glow then ?

Antiz96 avatar May 23 '22 16:05 Antiz96

@Antiz96 sadly, I don't know or have the time to verify that your issue is resolved in glamour or not - you could follow the instructions posted in my comment earlier in this issue thread and verify that yourself.

fiskhest avatar May 24 '22 06:05 fiskhest

@muesli Are there any future plans for this? I think this is a serious problem making glow unusable and inconsistent with other md viewers.
It should be specified in the README along with the plans and current workaround.

WieeRd avatar Jun 21 '22 10:06 WieeRd

@muesli, FWIW, this impacts the help command I'm implementing for the Elvish shell. There are a few commands with explicitly multiline usage text. For example: https://elv.sh/ref/builtin.html#str-cmp. That is rendered thusly by glow regardless of whether I end each line with two spaces, a backslash or <br/>:

<  $number... # less  <= $number... # less or equal  == $number... # equal  !=
$number... # not equal  >  $number... # greater  >= $number... # greater or
equal

krader1961 avatar Jul 14 '22 00:07 krader1961

I've tested @fiskhest's solution (https://github.com/charmbracelet/glow/issues/212#issuecomment-921632355) and it still works very well.
As you can see from above I've created a PR #362 based on that.
Hopefully this is the right way to fix it and gets merged soon.

WieeRd avatar Jul 16 '22 10:07 WieeRd

Working on it! Just not sure yet if we shouldn't keep the current setting as our default.

This would technically be a breaking change. So, if users are relying on current behavior, or if they expect minor versions of glow to not change the output, then I would recommend taking a Semantic Versioning approach and implement CommonMark compliant output behind an optional flag in this version. When you release v2, the setting can be turned on by default.

If you've already been changing output in minor versions of glow (i.e. not adhering to semver) then I would just turn the setting on by default.

jordanbtucker avatar Jul 16 '22 21:07 jordanbtucker

Please don't make this not default. People either don't use this functionality or came to complain about it on this issue. Just my thoughts.

magnus-ISU avatar Jul 16 '22 23:07 magnus-ISU

Please don't make this not default. People either don't use this functionality or came to complain about it on this issue. Just my thoughts.

I second this motion.

fiskhest avatar Jul 16 '22 23:07 fiskhest

I've updated my comments to point to the latest version of the CommonMark spec. No conformance changes were made to hard line breaks. Only changes to the prose occurred in that section.

jordanbtucker avatar Jul 17 '22 15:07 jordanbtucker

FWIW, this impacts the help command I'm implementing for the Elvish shell.

@krader1961 Wouldn't that be better placed in a code block, like it is in the doc comment? Glow will maintain line breaks inside code blocks, even without two spaces at the end of the line.

jordanbtucker avatar Jul 17 '22 15:07 jordanbtucker

I think preserving current behavior shouldn't even be an option.
I wrote some thoughts about it in https://github.com/charmbracelet/glow/pull/362#issuecomment-1188688606.
TLDR it should be applied right away in the next release as default.

WieeRd avatar Jul 19 '22 07:07 WieeRd

FWIW, this impacts the help command I'm implementing for the Elvish shell.

@krader1961 Wouldn't that be better placed in a code block, like it is in the doc comment? Glow will maintain line breaks inside code blocks, even without two spaces at the end of the line.

No, because people don't write markdown files to make them compatible with glow. That's why I agree with @WieeRd 's latest comment

magnus-ISU avatar Jul 19 '22 13:07 magnus-ISU

@magnus-ISU I wasn't saying that they should change their Markdown to accommodate glow. I was saying that the example they referenced is already in a code block, so I don't see why they would need to use hard line breaks at all.

But you do make a good point that Markdown users generally don't write Markdown to render with glow, at least not exclusively. They write it to render as HTML typically on some platform like GitHub or in a static site generator like Jekyll, and rendering a line break by ending a line with two spaces has been a feature of Markdown since its inception. So, regardless of breaking changes, the current behavior should be treated like a bug, and I don't see a reason to keep the behavior either.

jordanbtucker avatar Jul 19 '22 16:07 jordanbtucker