{"version":3,"file":"html.mjs","names":[],"sources":["../../../../src/transpiler/html/html.ts"],"sourcesContent":["import type { TypedNodeModel } from '@intlayer/types/nodeType';\nimport { formatNodeType, HTML } from '@intlayer/types/nodeType';\nimport { getHTMLCustomComponents } from './getHTMLCustomComponents';\nimport { validateHTML } from './validateHTML';\n\ntype PropsType = 'number' | 'string' | 'node';\n\ntype ComponentName = string;\n\nexport type HTMLContentConstructor<\n  Content = unknown,\n  T extends Record<string, any> = {},\n> = TypedNodeModel<typeof HTML, Content, T>;\n\nexport type HTMLContent<\n  Content = unknown,\n  Components extends Record<ComponentName, PropsType> = Record<\n    ComponentName,\n    PropsType\n  >,\n> = HTMLContentConstructor<\n  Content,\n  {\n    tags: string[] | Components;\n  }\n>;\n\n/**\n * Function intended to be used to build intlayer dictionaries.\n *\n * Allow to parse HTML/JSX-like strings and replace tags with components during interpretation.\n *\n * Usage:\n *\n * ```ts\n * html('Hello <b>World</b>')\n * ```\n *\n */\nexport const html = <\n  Components extends Record<string, any> = Record<string, any>,\n  Content = unknown,\n>(\n  content: Content,\n  components?: Components\n): HTMLContent<Content, Components> => {\n  const getComponents = () => {\n    if (components) {\n      return components;\n    }\n\n    if (typeof content === 'string') {\n      const { issues } = validateHTML(content);\n\n      for (const issue of issues) {\n        if (issue.type === 'error') {\n          console.error(`[intlayer/html] ${issue.message}`);\n        } else {\n          console.warn(`[intlayer/html] ${issue.message}`);\n        }\n      }\n\n      return getHTMLCustomComponents(content);\n    }\n\n    let stringContent: any;\n\n    if (typeof content === 'function') {\n      stringContent = content();\n    } else if (typeof (content as Promise<string>).then === 'function') {\n      stringContent = async () =>\n        getHTMLCustomComponents((await (content as Promise<string>)) as string);\n    }\n\n    if (typeof stringContent === 'string') {\n      return getHTMLCustomComponents(stringContent);\n    }\n\n    try {\n      return getHTMLCustomComponents(JSON.stringify(content));\n    } catch (_e) {\n      return [];\n    }\n  };\n\n  return formatNodeType(HTML, content, {\n    tags: getComponents(),\n  });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAuCA,MAAa,QAIX,SACA,eACqC;CACrC,MAAM,sBAAsB;AAC1B,MAAI,WACF,QAAO;AAGT,MAAI,OAAO,YAAY,UAAU;GAC/B,MAAM,EAAE,WAAW,aAAa,QAAQ;AAExC,QAAK,MAAM,SAAS,OAClB,KAAI,MAAM,SAAS,QACjB,SAAQ,MAAM,mBAAmB,MAAM,UAAU;OAEjD,SAAQ,KAAK,mBAAmB,MAAM,UAAU;AAIpD,UAAO,wBAAwB,QAAQ;;EAGzC,IAAI;AAEJ,MAAI,OAAO,YAAY,WACrB,iBAAgB,SAAS;WAChB,OAAQ,QAA4B,SAAS,WACtD,iBAAgB,YACd,wBAAyB,MAAO,QAAuC;AAG3E,MAAI,OAAO,kBAAkB,SAC3B,QAAO,wBAAwB,cAAc;AAG/C,MAAI;AACF,UAAO,wBAAwB,KAAK,UAAU,QAAQ,CAAC;WAChD,IAAI;AACX,UAAO,EAAE;;;AAIb,QAAO,eAAe,MAAM,SAAS,EACnC,MAAM,eAAe,EACtB,CAAC"}