Multiline strings that don’t break indentation

2020 answer: there is still nothing built into the JS stdlib to handle de-denting long lines, although TC39 has discussed adding a new template literal that handles indentation. You have 2 options presently:

  1. The endent and dedent-js packages will handle this. Note the dedent-js package actually works with both tabs and spaces.
    var dedent = require('dedent-js');
    var text = dedent(`
      <div>
        <span>OK</span>
        <div>
          <div></div>
        </div>
      </div>
    `);

Will strip out the proceeding whitespace on each line and the leading carriage return. It also has more users, an issue tracker, and is more easily updated than copypasting from Stack Overflow!

Note: dedent is a separate package from dedent-js and dedent fails on tabs.

  1. Don’t indent long lines, but use an editor that shows long lines as indented. Eg, vsCode – you can simply use long lines, not indent anything, and include carriage returns in a long string. vsCode will show them indented. The string below has no indentation – the second line The empty export... is immediately after the carriage return, but shows up as indented.

enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)