├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {type MergeExclusive} from 'type-fest'; 2 | import stringifyAttributes, {type HTMLAttributes} from 'stringify-attributes'; 3 | 4 | export type BaseOptions = { 5 | /** 6 | HTML tag attributes. 7 | */ 8 | readonly attributes?: HTMLAttributes; 9 | 10 | /** 11 | HTML tag name. 12 | 13 | @default 'div' 14 | */ 15 | readonly name?: string; 16 | }; 17 | 18 | export type HtmlOptions = { 19 | /** 20 | HTML tag value in unescaped HTML. 21 | */ 22 | readonly html?: string; 23 | }; 24 | 25 | export type TextOptions = { 26 | /** 27 | HTML tag value in escaped HTML. 28 | */ 29 | readonly text?: string; 30 | }; 31 | 32 | export type Options = BaseOptions & MergeExclusive; 33 | 34 | /** 35 | Create a HTML element string. 36 | 37 | @example 38 | ``` 39 | import createHtmlElement from 'create-html-element'; 40 | 41 | createHtmlElement({ 42 | name: 'h1', 43 | attributes: { 44 | class: 'unicorn', 45 | rainbow: true, 46 | horse: false, 47 | number: 1, 48 | multiple: [ 49 | 'a', 50 | 'b' 51 | ] 52 | }, 53 | html: '🦄' 54 | }); 55 | //=> '

🦄

' 56 | 57 | createHtmlElement({text: 'Hello World'}); 58 | //=> '
Hello <em>World</em>
' 59 | ``` 60 | */ 61 | export default function createHtmlElement(options?: Options): string; 62 | 63 | export {HTMLAttributes} from 'stringify-attributes'; 64 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import stringifyAttributes from 'stringify-attributes'; 2 | import {voidHtmlTags as voidHtmlTagsArray} from 'html-tags'; 3 | import {htmlEscape} from 'escape-goat'; 4 | 5 | const voidHtmlTags = new Set(voidHtmlTagsArray); 6 | 7 | export default function createHtmlElement( 8 | { 9 | name = 'div', 10 | attributes = {}, 11 | html = '', 12 | text, 13 | } = {}, 14 | ) { 15 | if (html && text) { 16 | throw new Error('The `html` and `text` options are mutually exclusive'); 17 | } 18 | 19 | const content = text ? htmlEscape(text) : html; 20 | let result = `<${name}${stringifyAttributes(attributes)}>`; 21 | 22 | if (!voidHtmlTags.has(name)) { 23 | result += `${content}`; 24 | } 25 | 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType, expectError} from 'tsd'; 2 | import createHtmlElement from './index.js'; 3 | 4 | expectType(createHtmlElement()); 5 | expectType( 6 | createHtmlElement({ 7 | attributes: { 8 | class: 'unicorn', 9 | rainbow: true, 10 | horse: false, 11 | number: 1, 12 | multiple: [ 13 | 'a', 14 | 'b', 15 | ], 16 | }, 17 | }), 18 | ); 19 | expectType(createHtmlElement({name: 'foo'})); 20 | expectType(createHtmlElement({html: '🦄'})); 21 | expectType(createHtmlElement({text: 'Hello World'})); 22 | expectError(createHtmlElement({html: '🦄', text: 'Hello World'})); 23 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "create-html-element", 3 | "version": "5.0.0", 4 | "description": "Create a HTML element string", 5 | "license": "MIT", 6 | "repository": "sindresorhus/create-html-element", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": "^18.20.0 || >=20.10.0" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "html", 31 | "element", 32 | "create", 33 | "document", 34 | "string", 35 | "make", 36 | "tag" 37 | ], 38 | "dependencies": { 39 | "escape-goat": "^4.0.0", 40 | "html-tags": "^4.0.0", 41 | "stringify-attributes": "^4.0.0", 42 | "type-fest": "^4.27.0" 43 | }, 44 | "devDependencies": { 45 | "ava": "^6.2.0", 46 | "tsd": "^0.31.2", 47 | "xo": "^0.59.3" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # create-html-element 2 | 3 | > Create a HTML element string 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install create-html-element 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import createHtmlElement from 'create-html-element'; 15 | 16 | createHtmlElement({ 17 | name: 'h1', 18 | attributes: { 19 | class: 'unicorn', 20 | rainbow: true, 21 | horse: false, 22 | number: 1, 23 | multiple: [ 24 | 'a', 25 | 'b' 26 | ] 27 | }, 28 | html: '🦄' 29 | }); 30 | //=> '

🦄

' 31 | 32 | createHtmlElement({text: 'Hello World'}); 33 | //=> '
Hello <em>World</em>
' 34 | ``` 35 | 36 | ## API 37 | 38 | ### createHtmlElement(options) 39 | 40 | #### options 41 | 42 | Type: `object` 43 | 44 | ##### name 45 | 46 | Type: `string`\ 47 | Default: `'div'` 48 | 49 | HTML tag name. 50 | 51 | ##### attributes 52 | 53 | Type: `object` 54 | 55 | HTML tag attributes. 56 | 57 | ##### html 58 | 59 | HTML tag value in unescaped HTML. 60 | 61 | This option is mutually exclusive with the `text` option. 62 | 63 | ##### text 64 | 65 | HTML tag value in escaped HTML. 66 | 67 | This option is mutually exclusive with the `html` option. 68 | 69 | ## Related 70 | 71 | - [stringify-attributes](https://github.com/sindresorhus/stringify-attributes) - Turn an object into a string of HTML attributes 72 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import createHtmlElement from './index.js'; 3 | 4 | test('creates element', t => { 5 | t.is( 6 | createHtmlElement({ 7 | name: 'h1', 8 | attributes: { 9 | class: 'unicorn', 10 | rainbow: true, 11 | horse: false, 12 | number: 1, 13 | multiple: ['a', 'b'], 14 | }, 15 | html: '🦄', 16 | }), 17 | '

🦄

', 18 | ); 19 | 20 | t.is( 21 | createHtmlElement({}), 22 | '
', 23 | ); 24 | }); 25 | 26 | test('creates void element', t => { 27 | t.is( 28 | createHtmlElement({ 29 | name: 'img', 30 | attributes: { 31 | foo: 'bar', 32 | }, 33 | html: 'noop', 34 | }), 35 | '', 36 | ); 37 | }); 38 | 39 | test('supports boolean and non-string attribute values', t => { 40 | t.is( 41 | createHtmlElement({ 42 | attributes: { 43 | foo: true, 44 | bar: false, 45 | one: 1, 46 | }, 47 | }), 48 | '
', 49 | ); 50 | }); 51 | 52 | test('escapes text content', t => { 53 | t.is( 54 | createHtmlElement({text: '🦄 & 🐐'}), 55 | '
🦄 & 🐐
', 56 | ); 57 | }); 58 | --------------------------------------------------------------------------------