{"version":3,"file":"keepLineBreaksPlugin.mjs","names":[],"sources":["../../../../../../src/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.ts"],"sourcesContent":["import type { Plugin } from 'unified';\nimport { visit } from 'unist-util-visit';\nimport type { Node, Parent as UnistParent } from 'unist';\nimport type { Root, RootContent, ThematicBreak } from 'mdast';\n\nfunction isParentWithChildren(\n  node: Node,\n): node is UnistParent & { children: RootContent[] } {\n  const maybe = node as unknown as { children?: unknown };\n  return Array.isArray(maybe.children);\n}\n\n/**\n * A table holds only rows, and the line gap between its rows is not a blank line: the delimiter\n * row between the header and the first data row is syntax. A `<br>` there would render as an\n * empty row and is invalid inside `<tbody>`.\n */\nfunction holdsOnlyTableRows(node: Node): boolean {\n  return node.type === 'table';\n}\n\n/** A `thematicBreak` that `mdast-util-to-hast` renders as `<br>` through `data.hName`. */\nfunction brNode(): ThematicBreak {\n  return { data: { hName: 'br' }, type: 'thematicBreak' };\n}\n\n/**\n * Preserves the blank source lines between sibling block nodes by inserting a `<br>` for each\n * blank line. The rows of a table are left as they are.\n */\nexport const keepLineBreaksPlugin: Plugin<[], Root> = () => (tree) => {\n  visit(\n    tree as unknown as UnistParent, // visit needs a Unist parent-like root\n    isParentWithChildren, // limit to parents with children\n    (parent) => {\n      if (holdsOnlyTableRows(parent)) return;\n\n      const children = parent.children as RootContent[];\n      if (children.length < 2) return;\n\n      const out: RootContent[] = [];\n\n      for (let i = 0; i < children.length; i++) {\n        const curr = children[i];\n        out.push(curr);\n\n        if (i === children.length - 1) break;\n\n        const next = children[i + 1];\n\n        const currEndLine =\n          curr.position && curr.position.end ? curr.position.end.line : undefined;\n        const nextStartLine =\n          next.position && next.position.start ? next.position.start.line : undefined;\n\n        if (typeof currEndLine !== 'number' || typeof nextStartLine !== 'number') {\n          continue;\n        }\n\n        // Each blank source line between the two nodes becomes one `<br>`.\n        const extraBlankLines = Math.max(0, nextStartLine - currEndLine - 1);\n        if (extraBlankLines > 0) {\n          for (let k = 0; k < extraBlankLines; k++) {\n            out.push(brNode());\n          }\n        }\n      }\n\n      parent.children = out;\n    },\n  );\n};\n"],"mappings":";;AAKA,SAAS,qBACP,MACmD;CACnD,MAAM,QAAQ;CACd,OAAO,MAAM,QAAQ,MAAM,QAAQ;AACrC;;;;;;AAOA,SAAS,mBAAmB,MAAqB;CAC/C,OAAO,KAAK,SAAS;AACvB;;AAGA,SAAS,SAAwB;CAC/B,OAAO;EAAE,MAAM,EAAE,OAAO,KAAK;EAAG,MAAM;CAAgB;AACxD;;;;;AAMA,IAAa,8BAAgD,SAAS;CACpE,MACE,MACA,uBACC,WAAW;EACV,IAAI,mBAAmB,MAAM,GAAG;EAEhC,MAAM,WAAW,OAAO;EACxB,IAAI,SAAS,SAAS,GAAG;EAEzB,MAAM,MAAqB,CAAC;EAE5B,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;GACxC,MAAM,OAAO,SAAS;GACtB,IAAI,KAAK,IAAI;GAEb,IAAI,MAAM,SAAS,SAAS,GAAG;GAE/B,MAAM,OAAO,SAAS,IAAI;GAE1B,MAAM,cACJ,KAAK,YAAY,KAAK,SAAS,MAAM,KAAK,SAAS,IAAI,OAAO,KAAA;GAChE,MAAM,gBACJ,KAAK,YAAY,KAAK,SAAS,QAAQ,KAAK,SAAS,MAAM,OAAO,KAAA;GAEpE,IAAI,OAAO,gBAAgB,YAAY,OAAO,kBAAkB,UAC9D;GAIF,MAAM,kBAAkB,KAAK,IAAI,GAAG,gBAAgB,cAAc,CAAC;GACnE,IAAI,kBAAkB,GACpB,KAAK,IAAI,IAAI,GAAG,IAAI,iBAAiB,KACnC,IAAI,KAAK,OAAO,CAAC;EAGvB;EAEA,OAAO,WAAW;CACpB,CACF;AACF"}