{"version":3,"file":"imageToLink.mjs","names":[],"sources":["../../../../../../src/components/Message/renderText/remarkPlugins/imageToLink.ts"],"sourcesContent":["import { SKIP, visit, type VisitorResult } from 'unist-util-visit';\nimport type { Image, Link, Parent, Text } from 'mdast';\nimport type { Node } from 'unist';\n\ntype ImgVisitor = (\n  node: Image,\n  index: number | null,\n  parent: Parent | null,\n) => VisitorResult;\n\nexport type ImageToLinkPluginOptions = {\n  getTextLabelFrom?: 'alt' | 'title' | 'url';\n};\n\nconst text = (value: string): Text => ({ type: 'text', value });\n\n/**\n * Converts a Markdown image, e.g. `![Minion](https://octodex.github.com/images/minion.png)`, to a\n * link to the image URL. The link text is the URL by default, so a server can generate or enrich\n * a preview from it.\n *\n * @param params.getTextLabelFrom - Image field used as the link text, falling back to the URL.\n * Defaults to `'url'`.\n */\nexport function imageToLink({ getTextLabelFrom = 'url' }: ImageToLinkPluginOptions = {}) {\n  return (tree: Node) => {\n    const visitor: ImgVisitor = (node, index, parent) => {\n      if (parent == null || index == null) return;\n\n      const label = node[getTextLabelFrom] ?? node.url; // node.alt || node.title || node.url;\n      const link: Link = {\n        children: [text(label)],\n        title: node.title ?? node.alt ?? node.url,\n        type: 'link',\n        url: node.url,\n      };\n\n      parent.children.splice(index, 1, link);\n      return [SKIP, index + 1] as const;\n    };\n\n    visit(tree, 'image', visitor);\n  };\n}\n"],"mappings":";;AAcA,IAAM,QAAQ,WAAyB;CAAE,MAAM;CAAQ;AAAM;;;;;;;;;AAU7D,SAAgB,YAAY,EAAE,mBAAmB,UAAoC,CAAC,GAAG;CACvF,QAAQ,SAAe;EACrB,MAAM,WAAuB,MAAM,OAAO,WAAW;GACnD,IAAI,UAAU,QAAQ,SAAS,MAAM;GAGrC,MAAM,OAAa;IACjB,UAAU,CAAC,KAFC,KAAK,qBAAqB,KAAK,GAEtB,CAAC;IACtB,OAAO,KAAK,SAAS,KAAK,OAAO,KAAK;IACtC,MAAM;IACN,KAAK,KAAK;GACZ;GAEA,OAAO,SAAS,OAAO,OAAO,GAAG,IAAI;GACrC,OAAO,CAAC,MAAM,QAAQ,CAAC;EACzB;EAEA,MAAM,MAAM,SAAS,OAAO;CAC9B;AACF"}