Skip to content
Myr

index.ts

This file

Path

src/helper/html/index.ts

57 lines · 1.8 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–57why, one step back
1/**
2 * @module3 * html Helper for Hono.4 */56import { escapeToBuffer, raw, resolveCallbackSync, stringBufferToString } from '../../utils/html'7import type { HtmlEscaped, HtmlEscapedString, StringBufferWithCallbacks } from '../../utils/html'89export { raw }1011export const html = (12 strings: TemplateStringsArray,13 ...values: unknown[]14): HtmlEscapedString | Promise<HtmlEscapedString> => {15 const buffer: StringBufferWithCallbacks = [''] as StringBufferWithCallbacks1617 for (let i = 0, len = strings.length - 1; i < len; i++) {18 buffer[0] += strings[i]1920 const children = Array.isArray(values[i])21 ? (values[i] as Array<unknown>).flat(Infinity)22 : [values[i]]23 for (let i = 0, len = children.length; i < len; i++) {24 // eslint-disable-next-line @typescript-eslint/no-explicit-any25 const child = children[i] as any26 if (typeof child === 'string') {27 escapeToBuffer(child, buffer)28 } else if (typeof child === 'number') {29 ;(buffer[0] as string) += child30 } else if (typeof child === 'boolean' || child === null || child === undefined) {31 continue32 } else if (typeof child === 'object' && (child as HtmlEscaped).isEscaped) {33 if ((child as HtmlEscapedString).callbacks) {34 buffer.unshift('', child)35 } else {36 const tmp = child.toString()37 if (tmp instanceof Promise) {38 buffer.unshift('', tmp)39 } else {40 buffer[0] += tmp41 }42 }43 } else if (child instanceof Promise) {44 buffer.unshift('', child)45 } else {46 escapeToBuffer(child.toString(), buffer)47 }48 }49 }50 buffer[0] += strings.at(-1) as string5152 return buffer.length === 153 ? 'callbacks' in buffer54 ? raw(resolveCallbackSync(raw(buffer[0], buffer.callbacks)))55 : raw(buffer[0])56 : stringBufferToString(buffer, buffer.callbacks)57}