Utility Template Tags for ES2015+

1.8.2 · active · verified Tue Apr 21

common-tags provides a collection of reusable template literal tag functions for JavaScript (ES2015+). These tags offer functionalities like HTML escaping (`html`, `safeHtml`), string stripping and indentation (`stripIndent`, `oneLine`), and list formatting (`commaLists`, `inlineLists`). The current stable version is 1.8.2. While there's a pre-release v2.0.0-alpha.1 hinting at future TypeScript adoption and documentation rewrite, the package has a relatively stable, though not very frequent, release cadence for major features, with minor patches addressing bugs or internal tooling. It differentiates itself by offering a robust, well-tested set of common string manipulation utilities often needed when working with template literals, particularly for generating clean, formatted output like HTML or SQL, without the overhead of larger templating engines. It also provides an API for creating custom tags, emphasizing modularity and extensibility for custom text processing scenarios. Since version 1.8.0, it is entirely dependency-free, making it a lightweight choice for modern JavaScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates the use of `html` for basic templating with variable interpolation, `stripIndent` for cleaning up multiline strings, and `oneLine` for condensing strings to a single line, showcasing common use cases for the library's tags.

import { html, stripIndent, oneLine } from 'common-tags';

const user = {
  name: "Jane Doe",
  email: "jane.doe@example.com",
  id: "user-123",
  description: `
    Jane is a passionate software engineer
    with a focus on front-end development.
    She loves open source and contributes
    to several projects.
  `
};

// Using 'html' tag for safe HTML generation and 'stripIndent' for formatting
const userCard = html`
  <div id="${user.id}">
    <h2>${user.name}</h2>
    <p>Email: <a href="mailto:${user.email}">${user.email}</a></p>
    <h3>About:</h3>
    ${stripIndent`${user.description}`}
  </div>
`;

// Using 'oneLine' tag for compact output
const compactInfo = oneLine`
  User: ${user.name}, ID: ${user.id}, 
  Email: ${user.email}
`;

console.log(userCard);
console.log("\n--- Compact Info ---");
console.log(compactInfo);

view raw JSON →