iansan5653 / compress-tag

:abcd: Template literal tag for painless multiline strings in JS.
https://www.npmjs.com/compress-tag
MIT License
14 stars 2 forks source link

Keep line breaks, remove indentation #52

Open jtmueller opened 1 month ago

jtmueller commented 1 month ago

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`;
}
iansan5653 commented 1 month ago

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?

jtmueller commented 2 weeks ago

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.