TypeScript package which smartly trims and strips indentation from multi-line strings.
import { dedent } from 'ts-dedent';
console.log(dedent`A string that gets so long you need to break it over
multiple lines. Luckily dedent is here to keep it
readable without lots of spaces ending up in the string
itself.`);
console.log(dedent`
A string that gets so long you need to break it over
multiple lines. Luckily dedent is here to keep it
readable without lots of spaces ending up in the string
itself.
`);A string that gets so long you need to break it over
multiple lines. Luckily dedent is here to keep it
readable without lots of spaces ending up in the string
itself.console.log(dedent`
Leading and trailing lines will be trimmed, so you can write something like
this and have it work as you expect:
* how convenient it is
* that I can use an indented list
- and still have it do the right thing
That's all.
`);Leading and trailing lines will be trimmed, so you can write something like
this and have it work as you expect:
* how convenient it is
* that I can use an indented list
- and still have it do the right thing
That's all.console.log(dedent`
Also works fine
${1}. With any kind of
${2}. Placeholders
`);Also works fine
1. With any kind of
2. Placeholdersconsole.log(dedent(`
Wait! I lied. Dedent can also be used as a function.
`);Wait! I lied. Dedent can also be used as a function.dedent runs the following steps on the string, in order:
- Strip a single leading line break. If the string starts with a newline (the common case when you put your first line of text below the opening backtick), that one newline is removed. A second blank line is kept, so you can intentionally start the output with an empty line.
- Strip a single trailing line break and its indentation. A trailing newline followed only by spaces/tabs is removed. This is what lets you put the closing backtick on its own indented line without adding a blank line to the result.
- Find the common indentation. For every line that contains actual
content,
dedentcounts its leading spaces and tabs. The "common indentation" is the smallest of those counts — i.e. the line with the fewest leading whitespace characters. Blank lines are ignored, and a line that starts with a non-whitespace character counts as0(so if any content line has no indentation, nothing is stripped). - Remove the common indentation from every line. Exactly that many leading whitespace characters are removed from the start of each line. Lines that were indented further keep the difference, so relative indentation (nested lists, code blocks, etc.) is preserved.
- Interpolate the placeholders.
${...}values are inserted. If a value is itself a multi-line string, every line after its first is re-indented to match the placeholder's position, so nested/multi-line values stay aligned.
So to answer the common question directly: it removes the indentation of the line with the fewest leading whitespace characters — not the first indented line — and applies that same removal to every line.
A few details worth knowing:
- Indentation is counted in characters, where each tab and each space counts as one. Removal also works on raw characters, so mixing tabs and spaces in your leading whitespace can give surprising results — pick one and be consistent.
- Only one leading and one trailing line break are trimmed. Additional blank lines at the very start or end are preserved.
// Lines indented by 4, 2, and 6 spaces. The minimum is 2, so 2 spaces are
// removed from every line:
console.log(dedent`
four
two
six
`); four
two
sixMIT
See history for more details.