UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

42 lines (40 loc) 1.39 kB
import { getHTMLCustomComponents } from "./getHTMLCustomComponents.mjs"; import { validateHTML } from "./validateHTML.mjs"; import { HTML, formatNodeType } from "@intlayer/types/nodeType"; //#region src/transpiler/html/html.ts /** * Function intended to be used to build intlayer dictionaries. * * Allow to parse HTML/JSX-like strings and replace tags with components during interpretation. * * Usage: * * ```ts * html('Hello <b>World</b>') * ``` * */ const html = (content, components) => { const getComponents = () => { if (components) return components; if (typeof content === "string") { const { issues } = validateHTML(content); for (const issue of issues) if (issue.type === "error") console.error(`[intlayer/html] ${issue.message}`); else console.warn(`[intlayer/html] ${issue.message}`); return getHTMLCustomComponents(content); } let stringContent; if (typeof content === "function") stringContent = content(); else if (typeof content.then === "function") stringContent = async () => getHTMLCustomComponents(await content); if (typeof stringContent === "string") return getHTMLCustomComponents(stringContent); try { return getHTMLCustomComponents(JSON.stringify(content)); } catch (_e) { return []; } }; return formatNodeType(HTML, content, { tags: getComponents() }); }; //#endregion export { html }; //# sourceMappingURL=html.mjs.map