prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Muliple NBSPs in code secontion in markdown string are collapsed

Open nschonni opened this issue 3 years ago • 3 comments

Prettier 2.7.1 Playground link

--parser markdown

Input:

For example, the string `"a   b"` (three spaces between _a_ and _b_) separates _a_ and _b_ more than `"a b"` (one space between _a_ and _b_).

Output:

For example, the string `"a b"` (three spaces between _a_ and _b_) separates _a_ and _b_ more than `"a b"` (one space between _a_ and _b_).

Expected behavior: I replaced regular spaces with Non-breaking spaces to prevent the collapsing of the spaces that occurs with regular spaces, but if you look at "a   b" it shouldn't be collapsed as it renders correctly.

nschonni avatar Sep 16 '22 00:09 nschonni

https://github.com/prettier/prettier/blob/cd3e530c2e51fb8296c0fb7738a9afdd3a3a4410/src/language-markdown/print-preprocess.js#L31-L39

fisker avatar Sep 16 '22 02:09 fisker

This replace logic dates back to https://github.com/prettier/prettier/pull/2943 (line). I agree that it’s best not to touch the contents of ``, just sharing the origin for historical context.

kachkaev avatar Sep 16 '22 17:09 kachkaev

Yeah, and original the doc had 3 regular spaces "a b" that does collapse down, so Prettier is doing the right thing in that case. This appears to just be an odd edge case, and could always be prettier-ignore'd

nschonni avatar Sep 16 '22 17:09 nschonni

original the doc had 3 regular spaces "a b" that does collapse down

It's collapsed by the browser. In the HTML markup, it's still 3 spaces. I'd vote for removing this replace unless there really is a case when it's doing the right thing.

Actually, according to the CommonMark spec, the only correct replacement would be to replace line endings with spaces.

thorn0 avatar Sep 22 '22 21:09 thorn0

Just ran into this while editing a .md file, this regex search: ^( )+(?![ \n]) (two spaces between first pair of parentheses) ... became: ^( )+(?![ \n]) (one space between first pair of parentheses)

Another vote for dropping this behavior.

mrienstra avatar Oct 05 '22 08:10 mrienstra

You are right @thorn0: interior spaces should not be collapsed, according to CommonMark.

`foo   bar 
baz`

<p><code>foo   bar  baz</code></p>

I can create a PR with the fix (hopefully by the end of this week).

kachkaev avatar Oct 05 '22 13:10 kachkaev

Potential fix: https://github.com/prettier/prettier/pull/13590

kachkaev avatar Oct 08 '22 21:10 kachkaev

Fixed by #13590 in next

thorn0 avatar Oct 14 '22 07:10 thorn0