diff --git a/src/handler/preprocess.ts b/src/handler/preprocess.ts index d7f18b9d..3e2bb1db 100644 --- a/src/handler/preprocess.ts +++ b/src/handler/preprocess.ts @@ -122,7 +122,7 @@ function translateSVGNodeToSVGString( _v = currentColor } - if (k === 'href' && type === 'image') { + if ((k === 'href' || k === 'xlinkHref') && type === 'image') { return ` ${ATTRIBUTE_MAPPING[k] || k}="${cache.get(_v as string)[0]}"` } return ` ${ATTRIBUTE_MAPPING[k] || k}="${_v}"` @@ -156,10 +156,13 @@ export async function preProcessNode(node: ReactNode) { return } else if (typeof _node === 'object') { if (_node.type === 'image') { - if (set.has(_node.props.href)) { - // do nothing - } else { - set.add(_node.props.href) + const imageSrc = _node.props.href || _node.props.xlinkHref + if (imageSrc) { + if (set.has(imageSrc)) { + // do nothing + } else { + set.add(imageSrc) + } } } else if (_node.type === 'img') { if (set.has(_node.props.src)) { diff --git a/test/__image_snapshots__/image-test-tsx-test-image-test-tsx-image-should-render-svg-with-image-using-xlink-href-1-snap.png b/test/__image_snapshots__/image-test-tsx-test-image-test-tsx-image-should-render-svg-with-image-using-xlink-href-1-snap.png new file mode 100644 index 00000000..e70d7129 Binary files /dev/null and b/test/__image_snapshots__/image-test-tsx-test-image-test-tsx-image-should-render-svg-with-image-using-xlink-href-1-snap.png differ diff --git a/test/image.test.tsx b/test/image.test.tsx index 0a52a02e..102766a3 100644 --- a/test/image.test.tsx +++ b/test/image.test.tsx @@ -138,6 +138,42 @@ describe('Image', () => { expect(toImage(svg, 100)).toMatchImageSnapshot() }) + it('should render svg with image using xlinkHref', async () => { + const svg = await satori( +
+ + + +
, + { width: 100, height: 100, fonts } + ) + expect(toImage(svg, 100)).toMatchImageSnapshot() + }) + it('should throw error when relative path is used', async () => { await expect( satori(