HTML Tag Generator

2.0.0 · maintenance · verified Sun Apr 19

The `html-tag` package provides a utility for generating HTML elements programmatically from JavaScript objects. It enables users to create HTML tags by specifying the tag name, an optional attributes object, and optional text content. The library correctly handles void (self-closing) elements and boolean attributes, simplifying basic HTML string construction. As of version 2.0.0, it offers a stable and lightweight solution for basic HTML generation, requiring Node.js 0.10.0 or higher. The package appears to be in maintenance mode, providing a focused API without frequent new feature releases. Its key differentiator is its straightforward, functional approach to HTML string creation, contrasting with more complex templating engines or JSX-based libraries, by focusing solely on direct string output for basic use cases. It does not provide advanced features like comprehensive HTML escaping (beyond basic attribute handling) or DOM manipulation, making it suitable for server-side HTML fragment generation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate various HTML elements, including attributes, text content, void elements, and handling of boolean attributes using the `html-tag` function.

const tag = require('html-tag');

// Generate a simple anchor tag with text and attributes
console.log(tag('a', {href: 'https://example.com', target: '_blank'}, 'Visit Example'));
// Expected: <a href="https://example.com" target="_blank">Visit Example</a>

// Generate an image tag (a void element) with attributes
console.log(tag('img', {src: 'path/to/image.jpg', alt: 'Descriptive alt text'}));
// Expected: <img src="path/to/image.jpg" alt="Descriptive alt text">

// Generate a div with text content
console.log(tag('div', 'Hello, World!'));
// Expected: <div>Hello, World!</div>

// Generate a details tag with a boolean 'open' attribute
console.log(tag('details', {open: true}));
// Expected: <details open></details>

// Force a tag not to render its closing tag (for XML compatibility)
console.log(tag('p', 'Some text for XML...', false));
// Expected: <p>Some text for XML...

view raw JSON →