dmnd/dedent

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

dword-design opened this issue · 11 comments

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

`

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

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)

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?

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.

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?

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.

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

@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!

@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.

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