Keep line breaks, remove indentation
Opened this issue · 2 comments
This is very handy, but it would be nice in some cases to have the option to preserve line breaks, but condense consecutive white-space characters after each line break. That would allow the creation of multi-line strings that are indented in the code, but not in the output.
function generateLetter() {
return `Dear Sir or Madam,
We are writing to inform you that this example will clearly show that as of
${new Date().toLocaleString()} line breaks are preserved, but leading space from
each line is stripped away.
Sincerely,
Us`;
}
This is possible now, by using \n
as the linebreak character (see the documentation). For example:
function generateLetter() {
return c`Dear Sir or Madam,\n
\n
We are writing to inform you that this example will clearly show that as of
${new Date().toLocaleString()} line breaks are preserved, but leading space from
each line is stripped away.
\n
Sincerely, \n
Us`;
}
It's not as pretty, but it does allow you to pick specifically which newlines to preserve. Does that solve your problem?
It's a workaround for sure, and in fact it's pretty much exactly what I ended up doing. It would still be nice to have the option of not needing explicit line-break notation.
For example, if I were to share a block of text from code to a non-technical person who wants to revise the text, I have to either remove all the \n
and then put them back in manually, or explain to a non-technical person what \n
is and where it belongs. In such a case it would be less-tedious to simply copy the text and shift-tab to remove leading spaces, and then paste in the revised text and indent it appropriately, without having to manually deal with explicit line-break characters.
Not a deal breaker, and thanks for this handy utility, but I do think it would be a useful option in some cases.