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.
npm install html-tagVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to generate various HTML elements, including attributes, text content, void elements, and handling of boolean attributes using the `html-tag` function.
Always sanitize or escape user-provided text content before passing it to `html-tag` to prevent XSS attacks. Consider using a dedicated HTML escaping utility for text nodes.
Ensure you pass a strict boolean `true` or `false` for attributes that should behave as boolean HTML attributes. For example, `tag('details', {open: false})` will omit the 'open' attribute, while `tag('details', {open: 'false'})` will produce `open="false"`.The most reliable way to use `html-tag` is via the CommonJS `require` syntax: `const tag = require('html-tag');`. If using ESM, ensure your build setup or Node.js version (>=12 with default interop) correctly handles CJS imports.Avoid using the `false` argument unless you specifically need to generate XML-like output or are certain your HTML will tolerate unclosed tags. For standard HTML, allow the library to auto-close or recognize void elements.
Use a default import for ESM (`import tag from 'html-tag';`) or the CommonJS `require` syntax (`const tag = require('html-tag');`).Change your import statement to use `import tag from 'html-tag';` if your environment supports ESM. Alternatively, ensure your file is treated as a CommonJS module (e.g., by using a `.cjs` extension or setting `"type": "commonjs"` in `package.json`).
Use a direct default import `import tag from 'html-tag';` instead of destructuring `import { tag } from 'html-tag';` for this CommonJS package.No dependency data recorded yet.