Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/handler/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`
Expand Down Expand Up @@ -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)) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions test/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ describe('Image', () => {
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should render svg with image using xlinkHref', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 32,
fontWeight: 600,
}}
>
<svg
width='100'
height='100'
viewBox='0 0 100 100'
fill='none'
xmlns='http://www.w3.org/2000/svg'
xmlnsXlink='http://www.w3.org/1999/xlink'
>
<image
id='image0_1_3'
width='100'
height='100'
xlinkHref='data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzNiAzNiI+PHBhdGggZmlsbD0iI0ZGQ0M0RCIgZD0iTTM2IDE4YzAgOS45NDEtOC4wNTkgMTgtMTggMTgtOS45NCAwLTE4LTguMDU5LTE4LTE4QzAgOC4wNiA4LjA2IDAgMTggMGM5Ljk0MSAwIDE4IDguMDYgMTggMTgiLz48ZWxsaXBzZSBmaWxsPSIjNjY0NTAwIiBjeD0iMTEuNSIgY3k9IjEyLjUiIHJ4PSIyLjUiIHJ5PSI1LjUiLz48ZWxsaXBzZSBmaWxsPSIjNjY0NTAwIiBjeD0iMjQuNSIgY3k9IjEyLjUiIHJ4PSIyLjUiIHJ5PSI1LjUiLz48cGF0aCBmaWxsPSIjNjY0NTAwIiBkPSJNMTggMjJjLTMuNjIzIDAtNi4wMjctLjQyMi05LTEtLjY3OS0uMTMxLTIgMC0yIDIgMCA0IDQuNTk1IDkgMTEgOSA2LjQwNCAwIDExLTUgMTEtOSAwLTItMS4zMjEtMi4xMzItMi0yLTIuOTczLjU3OC01LjM3NyAxLTkgMXoiLz48cGF0aCBmaWxsPSIjRkZGIiBkPSJNOSAyM3MzIDEgOSAxIDktMSA5LTEtMiA0LTkgNC05LTQtOS00eiIvPjwvc3ZnPg=='
/>
</svg>
</div>,
{ width: 100, height: 100, fonts }
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should throw error when relative path is used', async () => {
await expect(
satori(
Expand Down