Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/undent. undent removes extra leading spaces from template literals and multi‑line strings. jsr.io/@okikio/undent
Something went wrong. Try again.
Files
- TypeScript 100%
readme.md
[](https://jsr.io/@okikio/undent)
[](https://jsr.io/@okikio/undent/score)
[](https://www.npmjs.com/package/@okikio/undent)
[](https://github.com/okikio/undent/actions/workflows/ci.yml)
# undent
Strip source-code indentation from template literals and strings. Built for Deno, Node.js, Bun, etc... Works everywhere JavaScript runs.
Indented code is readable code. But template literals carry that indentation
straight into the output:
```ts
function greet(name: string) {
return `
Hello, ${name}!
Welcome aboard.
`;
}
console.log(greet("Ayo"));
```
The output looks weird because of the leading spaces:
```
Hello, Ayo!
Welcome aboard.
```
You could smash the template to column 0, but then your code becomes unreadable.
```ts
function greet(name: string) {
return `
Hello, ${name}!
Welcome aboard.
`;
}
console.log(greet("Ayo"));
```
> TL;DR: it looks kinda ugly...plus it messes up code folding in some editors.
`undent` fixes this issues by letting you write nicely indented templates without the extra spaces in the output:
```ts
import { undent } from "@okikio/undent";
function greet(name: string) {
return undent`
Hello, ${name}!
Welcome aboard.
`;
}
console.log(greet("Ayo"));
```
```
Hello, Ayo!
Welcome aboard.
```
It finds the shared indentation across all lines, strips it, and trims the blank
lines that come from the backtick placement. Beyond that, it handles several
things most dedent libraries get wrong:
- **Preserves newline styles** — `\n`, `\r\n`, and `\r` pass through
byte-for-byte, never silently normalized
- **Aligns multi-line interpolations** — when a `${value}` spans multiple lines,
subsequent lines stay pinned to the insertion column
- **Composes templates** — embed one template inside another without indentation
drift
- **Work…