Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@fluent/dedent

projectfluent8.4kApache-2.00.5.0TypeScript support: included

A template literal tag for dedenting Fluent code

dedent, fluent, ftl

readme

@fluent/dedent

@fluent/dedent provides a template literal tag to dedent Fluent code. It's part of Project Fluent.

Fluent Syntax is indentation-sensitive, and @fluent/dedent offers a convenient way to include Fluent snippets in source code keeping the current level of indentation and without compromising the readability.

Installation

@fluent/dedent can be used both on the client-side and the server-side. You can install it from the npm registry or use it as a standalone script (as the FluentDedent global).

npm install @fluent/dedent

How to use

@fluent/dedent's default export is meant to be used as a template literal tag. By convention, the tag is often called ftl.

import ftl from "@fluent/dedent";

let messages = ftl`
    hello = Hello, world!
    welcome = Welcome, {$userName}!
    `;

The position of the closing backtick defines how much indent will be removed from each line of the content. If the indentation is not sufficient in any of the non-blank lines of the content, a RangeError is thrown.

import ftl from "@fluent/dedent";

let messages = ftl`
    hello = Hello, world!
  welcome = Welcome, {$userName}!
    `;

// → RangeError("Insufficient indentation in line 2.")

Content must start on a new line and must end on a line of its own. The closing delimiter must appear on a new line. The first and the last line of the input will be removed from the output. If any of them contains non-whitespace characters, a RangeError is thrown.

import ftl from "@fluent/dedent";

let message1 = "hello = Hello, world!";
let message2 = ftl`
    hello = Hello, world!
    `;

assert(message1 === message2);

If you wish to include the leading or trailing line breaks in the output, put extra blank lines in the input.

import ftl from "@fluent/dedent";

let message = ftl`

    hello = Hello, world!

    `;

assert(message === "\nhello = Hello, world!\n");

changelog

Changelog

0.5.0 (2023-03-13)

  • Drop Node.js v12 support, add v18 & latest to CI tests (#607)

@fluent/dedent 0.4.0 (September 13, 2021)

  • Remove "type": "commonjs" from the package's root package.json, but add "type": "module" to the esm/ directory. (#556, #567)
  • Set Node.js 12 as the minimum supported version (#557)

@fluent/dedent 0.3.0 (July 2, 2020)

  • Remove the compat.js build and compile everything to ES2018. (#472)

    TypeScript source code is now compiled to ES2018 files in the esm/ directory. These files are then bundled into a single index.js UMD file without any further transpilation.

    The compat.js build (available as @fluent/dedent/compat) was removed. Please use your own transpilation pipeline if ES2018 is too recent for your project.

    Refer to https://github.com/projectfluent/fluent.js/wiki/Compatibility for more information

@fluent/dedent 0.2.0 (March 2, 2020)

  • Migrate @fluent/dedent to TypeScript. (#451)

    There are no functional nor API changes in this release.

@fluent/dedent 0.1.0 (May 23, 2019)

This is the first release of @fluent/dedent as an independent package. It's based on the ftl template tag from the fluent package, but the behavior has changed and the code has been refactored.

The behavior in this version is largely based on the multiline strings specification in Swift. See the README.md for more details.