UNPKG

@intlayer/core

Version:

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

35 lines (33 loc) 1.07 kB
import { getInsertionValues } from "./getInsertionValues.mjs"; import { NodeType, formatNodeType } from "@intlayer/types"; //#region src/transpiler/insertion/insertion.ts /** * Function intended to be used to build intlayer dictionaries. * * Allow to identify insertions inside a content. * * Usage: * * ```ts * insertion('Hi, my name is {{name}} and I am {{age}} years old.') * ``` * */ const insertion = (content) => { const getInsertions = () => { if (typeof content === "string") return getInsertionValues(content); let stringContent; if (typeof content === "function") stringContent = content(); else if (typeof content.then === "function") stringContent = async () => getInsertionValues(await content); if (typeof stringContent === "string") return getInsertionValues(stringContent); try { return getInsertionValues(JSON.stringify(content)); } catch (_e) { return []; } }; return formatNodeType(NodeType.Insertion, content, { fields: getInsertions() }); }; //#endregion export { insertion as insert }; //# sourceMappingURL=insertion.mjs.map