Need private packages and team management tools?Check out npm Teams »

outdent

0.7.1 • Public • Published

outdent

Removes leading indentation from ES6 template strings

Build Status typings included

ES6 template strings are great, but they preserve everything between the backticks, including leading spaces. Sometimes I want to indent my template literals to make my code more readable without including all those spaces in the string.

Outdent will remove those leading spaces, as well as the leading and trailing newlines.

Usage

Import outdent using your module system of choice.

CommonJS:

const outdent = require('outdent');

ES6 Modules & TypeScript:

import outdent from 'outdent';

Examples

import outdent from 'outdent';
 
const markdown = outdent`
    # My Markdown File
 
    Here is some indented code:
 
        console.log("hello world!");
`;
 
console.log(markdown);
 
fs.writeFileSync('output.md', markdown);

The contents of output.md do not have the leading indentation:

My Markdown File
 
Here is some indented code:
 
    console.log("hello world!");

As a JavaScript string:

var markdown = '# My Markdown File\n' +
               '\n' +
               'Here is some indented code:\n' +
               '\n' +
               '    console.log("hello world!");';

You can pass options to outdent to control its behavior. They are explained in Options.

const output = outdent({trimLeadingNewline: false, trimTrailingNewline: false})`
    Hello world!
`;
 
assert(output === '\nHello world!\n');

You can explicitly specify the indentation level by passing outdent as the first interpolated value. Its position sets the indentation level and it is removed from the output:

const output = outdent`
      ${outdent}
        Yo
    12345
          Hello world
`;
 
assert(output === '  Yo\n345\n    Hello world');

Note: ${outdent} must be alone on its own line without anything before or after it. It cannot be preceded by any non-whitespace characters. If these conditions are not met, outdent will follow normal indentation-detection behavior.

Outdent can also remove indentation from plain strings via the string method.

const output = outdent.string('\n    Hello world!\n');
 
assert(output === 'Hello world!');

Options

trimLeadingNewline

Default: true

trimTrailingNewline

Default: true

Whether or not outdent should remove the leading and/or trailing newline from your template string. For example:

var s = outdent({trimLeadingNewline: false})`
    Hello
`;
 
assert(=== '\nHello');
 
= outdent({trimTrailingNewline: false})`
    Hello
`
 
assert(=== 'Hello\n');
 
= outdent({trimLeadingNewline: false, trimTrailingNewline: false})`
 
`;
 
assert(=== '\n\n');

newline

Default: null

If set to a string, normalize all newlines in the template literal to this value.

If null, newlines are left untouched.

For example, in the absence of a .gitattributes file, source code checked out on Windows will have '\r\n' newlines. This affects template literals as well. The following example would produce different results on Windows and Linux, but can be normalized via newline:

s = outdent `
    first
    second
`;

assert(s === 'first\r\nsecond'); // Only true for git checkout on Windows
assert(s === 'first\nsecond'); // Only true for git checkout on Linux

s = outdent({newline: '\n'}) `
    first
    second
`;

assert(s === 'first\nsecond'); // Always true on any platform

Newlines that get normalized are '\r\n', '\r', and '\n'.

Newlines within interpolated values are never normalized.

Although intended for normalizing to '\n' or '\r\n', you can use any string, for example a space.

const s = outdent({newline: ' '}) `
    Hello
    world!
`;
 
assert(=== 'Hello world!');

Gotchas

Start on a new line

Start the contents of your template string on a new line after the opening backtick. Otherwise, outdent has no choice but to detect indentation from the second line, which does not work in all situations.

// Bad
const output = outdent `* item 1
                          * sub-item
`;
// output === '* item 1\n* sub-item'; Indentation of sub-item is lost
 
// Good
const output = outdent `
    * item 1
      * sub-item
`;

Spaces and tabs

Spaces and tabs are treated identically. outdent does not verify that you are using spaces or tabs consistently; they are all treated as a single character for the purpose of removing indentation. Spaces, tabs, and smart tabs should all work correctly provided you use them consistently.

TypeScript declarations

This module includes TypeScript type declarations so you will get code completion and error-checking without installing anything else.

Questions or Bugs?

File an issue on Github: https://github.com/cspotcode/outdent/issues

Install

npm i outdent

DownloadsWeekly Downloads

207,591

Version

0.7.1

License

MIT

Unpacked Size

44 kB

Total Files

15

Last publish

Collaborators

  • avatar