{"version":3,"file":"plusPlusToEmphasis.mjs","names":[],"sources":["../../../../../../src/components/Message/renderText/remarkPlugins/plusPlusToEmphasis.ts"],"sourcesContent":["import type { Plugin } from 'unified';\nimport { SKIP, visit } from 'unist-util-visit';\nimport type { Visitor } from 'unist-util-visit';\nimport type { Parent, PhrasingContent, Text } from 'mdast';\n\n/**\n * Matches `++text++` where the text starts and ends with a non-whitespace character and may span\n * lines, so `++a++` matches but `++++` and `++ ++` do not.\n */\nconst INS_REGEX = /\\+\\+(\\S(?:[\\s\\S]*?\\S)?)\\+\\+/g;\nconst IGNORE_NODE_TYPES = new Set([\n  'code',\n  'inlineCode',\n  'link',\n  'linkReference',\n  'definition',\n  'math',\n  'inlineMath',\n]);\n\n/**\n * Converts `++text++` to an `<ins>` element. Code, link, definition and math nodes are skipped.\n */\nexport const plusPlusToEmphasis: Plugin<[]> = () => {\n  const visitor: Visitor = (node, index, parent) => {\n    // Do not descend into ignored nodes.\n    if (IGNORE_NODE_TYPES.has(node.type)) return SKIP;\n\n    // Only text nodes with a parent and an index are transformed.\n    if (node.type !== 'text' || parent == null || typeof index !== 'number') return;\n\n    const value = (node as Text).value;\n\n    // The regex is global and shared, so each node must start scanning from index 0.\n    INS_REGEX.lastIndex = 0;\n\n    let match: RegExpExecArray | null;\n    let last = 0;\n    const out: PhrasingContent[] = [];\n\n    while ((match = INS_REGEX.exec(value))) {\n      const [full, inner] = match;\n      const start = match.index;\n\n      if (start > last) out.push({ type: 'text', value: value.slice(last, start) });\n\n      // `data.hName` makes remark-rehype render this node as `<ins>`.\n      out.push({\n        children: [{ type: 'text', value: inner }],\n        data: { hName: 'ins' },\n        type: 'emphasis',\n      });\n\n      last = start + full.length;\n    }\n\n    if (out.length === 0) return;\n    if (last < value.length) out.push({ type: 'text', value: value.slice(last) });\n\n    (parent as Parent).children.splice(index, 1, ...out);\n\n    // Skip re-visiting the replaced range; continue after inserted nodes\n    return [SKIP, index + out.length];\n  };\n\n  return (tree) => visit(tree, visitor);\n};\n"],"mappings":";;;;;;AASA,IAAM,YAAY;AAClB,IAAM,oCAAoB,IAAI,IAAI;CAChC;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;;;;AAKD,IAAa,2BAAuC;CAClD,MAAM,WAAoB,MAAM,OAAO,WAAW;EAEhD,IAAI,kBAAkB,IAAI,KAAK,IAAI,GAAG,OAAO;EAG7C,IAAI,KAAK,SAAS,UAAU,UAAU,QAAQ,OAAO,UAAU,UAAU;EAEzE,MAAM,QAAS,KAAc;EAG7B,UAAU,YAAY;EAEtB,IAAI;EACJ,IAAI,OAAO;EACX,MAAM,MAAyB,CAAC;EAEhC,OAAQ,QAAQ,UAAU,KAAK,KAAK,GAAI;GACtC,MAAM,CAAC,MAAM,SAAS;GACtB,MAAM,QAAQ,MAAM;GAEpB,IAAI,QAAQ,MAAM,IAAI,KAAK;IAAE,MAAM;IAAQ,OAAO,MAAM,MAAM,MAAM,KAAK;GAAE,CAAC;GAG5E,IAAI,KAAK;IACP,UAAU,CAAC;KAAE,MAAM;KAAQ,OAAO;IAAM,CAAC;IACzC,MAAM,EAAE,OAAO,MAAM;IACrB,MAAM;GACR,CAAC;GAED,OAAO,QAAQ,KAAK;EACtB;EAEA,IAAI,IAAI,WAAW,GAAG;EACtB,IAAI,OAAO,MAAM,QAAQ,IAAI,KAAK;GAAE,MAAM;GAAQ,OAAO,MAAM,MAAM,IAAI;EAAE,CAAC;EAE5E,OAAmB,SAAS,OAAO,OAAO,GAAG,GAAG,GAAG;EAGnD,OAAO,CAAC,MAAM,QAAQ,IAAI,MAAM;CAClC;CAEA,QAAQ,SAAS,MAAM,MAAM,OAAO;AACtC"}