create-html-element
Version:
Create a HTML element string
64 lines (52 loc) • 1.15 kB
TypeScript
import {type MergeExclusive} from 'type-fest';
import stringifyAttributes, {type HTMLAttributes} from 'stringify-attributes';
export type BaseOptions = {
/**
HTML tag attributes.
*/
readonly attributes?: HTMLAttributes;
/**
HTML tag name.
@default 'div'
*/
readonly name?: string;
};
export type HtmlOptions = {
/**
HTML tag value in unescaped HTML.
*/
readonly html?: string;
};
export type TextOptions = {
/**
HTML tag value in escaped HTML.
*/
readonly text?: string;
};
export type Options = BaseOptions & MergeExclusive<HtmlOptions, TextOptions>;
/**
Create a HTML element string.
@example
```
import createHtmlElement from 'create-html-element';
createHtmlElement({
name: 'h1',
attributes: {
class: 'unicorn',
rainbow: true,
horse: false,
number: 1,
multiple: [
'a',
'b'
]
},
html: '🦄'
});
//=> '<h1 class="unicorn" rainbow number="1" multiple="a b">🦄</h1>'
createHtmlElement({text: 'Hello <em>World</em>'});
//=> '<div>Hello <em>World</em></div>'
```
*/
export default function createHtmlElement(options?: Options): string;
export {HTMLAttributes} from 'stringify-attributes';