{"version":3,"file":"renderText.mjs","names":[],"sources":["../../../../../src/components/Message/renderText/renderText.tsx"],"sourcesContent":["import React from 'react';\nimport ReactMarkdown, { defaultUrlTransform } from 'react-markdown';\nimport { find } from 'linkifyjs';\nimport remarkGfm from 'remark-gfm';\nimport type { ComponentType, JSX, ReactNode } from 'react';\nimport type { Options } from 'react-markdown';\nimport type { PluggableList } from 'unified';\n\nimport { Anchor, Emoji, Mention } from './componentRenderers';\nimport { detectHttp, matchMarkdownLinks, messageCodeBlocks } from './regex';\nimport {\n  createMentionPluginAndDisplayTextSet,\n  emojiMarkdownPlugin,\n} from './rehypePlugins';\nimport {\n  htmlToTextPlugin,\n  imageToLink,\n  keepLineBreaksPlugin,\n  plusPlusToEmphasis,\n} from './remarkPlugins';\nimport { ErrorBoundary } from '../../UtilityComponents';\nimport type { MentionProps } from './componentRenderers/Mention';\nimport type { RenderTextMentionEntity } from './rehypePlugins';\n\nexport type RenderTextPluginConfigurator = (\n  defaultPlugins: PluggableList,\n) => PluggableList;\n\ntype IntrinsicElementTagName = keyof JSX.IntrinsicElements & string;\n\nexport const defaultAllowedTagNames: Array<\n  IntrinsicElementTagName | 'emoji' | 'mention'\n> = [\n  'html',\n  'text',\n  'br',\n  'p',\n  'em',\n  'strong',\n  'a',\n  'ol',\n  'ul',\n  'li',\n  'code',\n  'pre',\n  'blockquote',\n  'del',\n  'table',\n  'thead',\n  'tbody',\n  'th',\n  'tr',\n  'td',\n  'tfoot',\n  // Custom tag names produced by the SDK's emoji and mention plugins.\n  'emoji',\n  'mention',\n  'h1',\n  'h2',\n  'h3',\n  'h4',\n  'h5',\n  'h6',\n  'ins',\n];\n\nfunction formatUrlForDisplay(url: string) {\n  try {\n    return decodeURIComponent(url).replace(detectHttp, '');\n  } catch (e) {\n    return url;\n  }\n}\n\nfunction encodeDecode(url: string) {\n  try {\n    return encodeURI(decodeURIComponent(url));\n  } catch (error) {\n    return url;\n  }\n}\n\nconst urlTransform = (uri: string) =>\n  uri.startsWith('app://') ? uri : defaultUrlTransform(uri);\n\nconst getPluginsForward: RenderTextPluginConfigurator = (plugins: PluggableList) =>\n  plugins;\n\nexport const markDownRenderers: RenderTextOptions['customMarkDownRenderers'] = {\n  a: Anchor,\n  emoji: Emoji,\n  mention: Mention,\n};\n\nexport type RenderTextOptions = {\n  allowedTagNames?: Array<IntrinsicElementTagName | 'emoji' | 'mention' | (string & {})>;\n  customMarkDownRenderers?: Options['components'] &\n    Partial<{\n      emoji: ComponentType;\n      mention: ComponentType<MentionProps>;\n    }>;\n  getRehypePlugins?: RenderTextPluginConfigurator;\n  getRemarkPlugins?: RenderTextPluginConfigurator;\n  /**\n   * Mention entities to highlight in the rendered text, including `@channel`, `@here`, roles and\n   * user groups.\n   */\n  messageMentionEntities?: RenderTextMentionEntity[];\n};\n\nexport type RenderTextFunction = (\n  text?: string,\n  options?: RenderTextOptions,\n) => ReactNode;\n\nexport function renderText(\n  text?: string,\n  {\n    allowedTagNames = defaultAllowedTagNames,\n    customMarkDownRenderers,\n    getRehypePlugins = getPluginsForward,\n    getRemarkPlugins = getPluginsForward,\n    messageMentionEntities = [],\n  }: RenderTextOptions = {},\n) {\n  if (!text) return null;\n  if (text.trim().length === 1) return <>{text}</>;\n\n  let newText = text;\n  const { mentionDisplayTextSet, plugin: mentionsPlugin } =\n    createMentionPluginAndDisplayTextSet(messageMentionEntities);\n  const markdownLinks = matchMarkdownLinks(newText);\n  const codeBlocks = messageCodeBlocks(newText);\n\n  // URLs (and e-mails that match a mention display text) become Markdown links, iterating from\n  // the last match so the added characters do not shift the positions of earlier ones.\n  const links = [...find(newText, 'email'), ...find(newText, 'url')];\n  for (let i = links.length - 1; i >= 0; i--) {\n    const { end, href, start, type, value } = links[i];\n    const linkIsInBlock = codeBlocks.some((block) => block?.includes(value));\n\n    // Skip links that are already Markdown links.\n    const noParsingNeeded =\n      markdownLinks &&\n      markdownLinks.filter((text) => {\n        const strippedHref = href?.replace(detectHttp, '');\n        const strippedText = text?.replace(detectHttp, '');\n\n        if (!strippedHref || !strippedText) return false;\n\n        return strippedHref.includes(strippedText) || strippedText.includes(strippedHref);\n      });\n\n    if (noParsingNeeded.length > 0 || linkIsInBlock) continue;\n\n    try {\n      // A mention display text can look like an e-mail; the match counts as a mention when an\n      // `@` directly precedes it.\n      if (type === 'email') {\n        const mentionDisplayText = `@${value}`;\n        if (mentionDisplayTextSet.has(mentionDisplayText)) {\n          // FIXME: breaks if the mention symbol is not '@'\n          const isMention = newText.charAt(start - 1) === '@';\n          // A mention is left as is for `mentionsMarkdownPlugin` to handle.\n          newText =\n            newText.slice(0, start) +\n            (isMention ? value : `[${value}](${encodeDecode(href)})`) +\n            newText.slice(end);\n        }\n      } else {\n        const displayLink = formatUrlForDisplay(href);\n\n        newText =\n          newText.slice(0, start) +\n          `[${displayLink}](${encodeDecode(href)})` +\n          newText.slice(end);\n      }\n    } catch (e) {\n      void e;\n    }\n  }\n\n  const remarkPlugins: PluggableList = [\n    htmlToTextPlugin,\n    keepLineBreaksPlugin,\n    [remarkGfm, { singleTilde: false }],\n    plusPlusToEmphasis,\n    imageToLink,\n  ];\n  const rehypePlugins: PluggableList = [emojiMarkdownPlugin];\n\n  if (messageMentionEntities.length) {\n    rehypePlugins.push(mentionsPlugin);\n  }\n\n  return (\n    <ErrorBoundary fallback={<>{text}</>}>\n      <ReactMarkdown\n        allowedElements={allowedTagNames}\n        components={{\n          ...markDownRenderers,\n          ...customMarkDownRenderers,\n        }}\n        rehypePlugins={getRehypePlugins(rehypePlugins)}\n        remarkPlugins={getRemarkPlugins(remarkPlugins)}\n        skipHtml\n        unwrapDisallowed\n        urlTransform={urlTransform}\n      >\n        {newText}\n      </ReactMarkdown>\n    </ErrorBoundary>\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA8BA,IAAa,yBAET;CACF;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;CACA;CACA;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