@technobuddha/library
Version:
A large library of useful functions
27 lines (24 loc) • 705 B
text/typescript
import { space } from '../constants';
import build from '../build';
import escapeHTML from '../escapeHTML';
/**
* Surround text with an HTML tag
*
* @param input The text to surround
* @param tagName The name of the tag
* @param attributes A dictionary of name value pairs to use for attributes
* @returns HTML tag with text
*/
export function tag(input: string, tagName = 'span', attributes: Record<string, string> = {}): string {
return build(
'<',
tagName,
Object.entries(attributes).flatMap(([ k, v ]) => [ space, k, '="', escapeHTML(v), '"' ]),
'>',
escapeHTML(input),
'</',
tagName,
'>'
);
}
export default tag;