JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3258904
  • Score
    100M100P100Q191632F
  • License MIT

Remove leading indentation from ES6 template literals.

Package Exports

  • outdent

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (outdent) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

outdent

Removes leading indentation from ES6 template strings

Build Status

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.

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' +
    '    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');

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(s === '\nHello');
s = outdent({trimTrailingNewline: false})`
    Hello
`
assert(s === 'Hello\n');
s = outdent({trimLeadingNewline: false, trimTrailingNewline: false})`
    
`;
assert(s === '\n\n');

Gotchas

You must start the contents of your template string on a new line after the opening backtick. Otherwise, outdent has no way to detect how much leading indentation to remove.

// Bad
const output = outdent`Hello
    world
`;
// Good
const output = outdent`
    Hello
    world
`;

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.

Questions or Bugs?

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