diff --git a/packages/typed-message/dom/Renderer/utils/renderText.tsx b/packages/typed-message/dom/Renderer/utils/renderText.tsx index e6277d213eec..c02793d2d2a8 100644 --- a/packages/typed-message/dom/Renderer/utils/renderText.tsx +++ b/packages/typed-message/dom/Renderer/utils/renderText.tsx @@ -34,12 +34,28 @@ export const RenderLinkFragment = memo(function RenderLink( }) function parseText(string: string, Text: NonNullable) { - const links = parseLink(string).flatMap((x, index) => { + const links = parseLink(string).flatMap((x) => { if (x.type === 'text') { - return x.content.split(/(\n)/g).map((x) => (x === '\n' && index !== 0 ?
: )) + return sliceString(x.content).map((x) => (x === '\n' ?
: )) } if (x.category === 'normal' && !x.content.match(/^https?:\/\//gi)) x.content = 'http://' + x.content return }) return links } + +function sliceString(x: string): string[] { + const result: string[] = [] + + let pos = 0 + let index = x.indexOf('\n') + + if (index === -1) return [x] + while (index !== -1) { + result.push(x.slice(pos, index), '\n') + pos = index + 1 + index = x.indexOf('\n', pos) + } + result.push(x.slice(pos)) + return result.filter(Boolean) +}