From 827216ca9427cd68b1c307d658a24201d6a280b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Pota=CC=81c=CC=8Cek?= Date: Wed, 25 Mar 2026 00:27:57 +0100 Subject: [PATCH] children as array of string --- src/Anchorme.test.tsx | 27 +++++++++++++++++++++++++++ src/Anchorme.tsx | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Anchorme.test.tsx b/src/Anchorme.test.tsx index 7b956c3..a645883 100644 --- a/src/Anchorme.test.tsx +++ b/src/Anchorme.test.tsx @@ -131,6 +131,33 @@ it('should render with custom component', () => { expect(customCallback).toHaveBeenCalledWith(URL) }) +it('should render mixed text node and expression children as links', () => { + const { container } = render(testa.com {'testb.com'}) + const links = container.querySelectorAll('a') + expect(links).toHaveLength(2) + expect(links[0]?.href).toBe('http://testa.com/') + expect(links[1]?.href).toBe('http://testb.com/') +}) + +it('should render expression children with surrounding spaces', () => { + const { container } = render({'testa.com'} testb.com) + const links = container.querySelectorAll('a') + expect(links).toHaveLength(2) + expect(links[0]?.href).toBe('http://testa.com/') + expect(links[1]?.href).toBe('http://testb.com/') +}) + +it('should render plain text mixed with expression URL', () => { + const { container, getByText } = render( + hello {'example.com'} world, + ) + const links = container.querySelectorAll('a') + expect(links).toHaveLength(1) + expect(links[0]?.href).toBe('http://example.com/') + expect(getByText(/hello/)).toBeInTheDocument() + expect(getByText(/world/)).toBeInTheDocument() +}) + it('should render with custom inline component', () => { const customCallback = jest.fn() diff --git a/src/Anchorme.tsx b/src/Anchorme.tsx index 85e3792..6f59c8b 100644 --- a/src/Anchorme.tsx +++ b/src/Anchorme.tsx @@ -5,12 +5,12 @@ import { AnchorProps, LinkComponent } from './types' import { Link } from './Link' type Props = { - children: string + children: string | string[] linkComponent?: LinkComponent } & AnchorProps const Anchorme = ({ children, ...rest }: Props) => { - const text = children + const text = Array.isArray(children) ? children.join('') : children const parse = useCallback(() => { const matches = anchorme.list(text)