dmnd / dedent

⬅️ ES6 string tag that strips indentation from multi-line strings.
MIT License
910 stars 35 forks source link

Should only trim one newline at start and end so that you can add custom newlines #41

Open dword-design opened 1 year ago

dword-design commented 1 year ago

Looks like the module removes all newlines from start and end, but I think it makes more sense to only remove one newline at start and end so that I can add newlines without adding \n.

// Adds a newline at the end. Currently the newline would be deleted.
dedent`
  foo
  bar

`
JoshuaKGoldberg commented 1 year ago

Blurgh, and then I missed you filing this issue - sorry about that @dword-design!

I think this is pretty reasonable. Dedenting is a separate action from trimming. I think it'd be reasonable to stop trimming any newlines at all. Now that we have a place for options (#65), it might be good to add an option like trim that's off by default. What do you think?

cc @G-Rath and @Haroenv since you've been active here recently too, thoughts?

G-Rath commented 1 year ago

I think dedent should definitely trim at least the first and last newline by default, otherwise it defeats the point right?

I'm not sure if you need a dedicated trim function when you can just call .trim() on the string (if you go with just trimming off the one first/last newline)

Haroenv commented 1 year ago

I think trimming makes sense by default, but maybe when there are multiple it should be preserved? Not sure what the actual use case is that does need a non-indented string, but also has newlines that have a use case?

dword-design commented 1 year ago

Trimming one newline at begin and end and keep the others is basically what I have proposed in this PR and would be the expected behavior from my side.

mariomui commented 9 months ago
const markdown2 = `---
list: me
bird: two
---
poo
`;
const markdown = dedent`
    ---
    list: me
    bird: two
    ---
    poo\n
    `;

currently i have to do this because of the trimming. maybe have an option to turn off trimming?

CxRes commented 2 months ago

Could we bring @dword-design PR back under a flag, please? This way it is not a breaking change and those who want can opt into this behaviour.

dword-design commented 2 months ago

I myself don't need it anymore since I now do it with \n, but I'm happy to help.

CxRes commented 2 months ago

@dword-design Call me peculiar, but doing it with \n (which I do too) looks just ugly. Also, string.dedent proposal will work the way you are suggesting. A flag seems like a reasonable compromise without breaking backwards compatibility!

dword-design commented 2 months ago

@CxRes I mainly used it for a trailing newline in file-based tests and I finally decided that

dedent`
  # bla

  blub\n
`

is more compact than

dedent`
  # bla

  blub

`

But this is just my particular use case.

CxRes commented 2 months ago

\n\n @dword-design Sure, this is small potatos! I just find this funny that we should start writing like this.\n :)