Skip to content

Commit 269ea51

Browse files
committedMar 26, 2025·
fix: warn when using Nuxt Icon wrong in OG Images
Fixes #344
1 parent 24a5390 commit 269ea51

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
 

‎playground/components/OgImage/NuxtIcon.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const containerStyles = {
2121
<div :style="containerStyles">
2222
<div>{{ title }}</div>
2323
<div>{{ description }}</div>
24-
<Icon name="carbon:book" size="250px" class="text-blue-500/30" style="margin: 0 auto;" />
25-
<Icon name="carbon:star" size="250px" class="text-blue-500" style="margin: 0 auto;" />
26-
<Icon name="uil:github" size="250px" class="text-blue-500" style="margin: 0 auto;" />
24+
<Icon mode="svg" name="carbon:book" size="250px" class="size-30 text-blue-500/30" style="margin: 0 auto;" />
25+
<Icon name="carbon:star" size="250px" class="size-30 text-blue-500" style="margin: 0 auto;" />
26+
<Icon mode="svg" name="uil:github" size="250px" class="size-30 text-blue-500" style="margin: 0 auto;" />
2727
</div>
2828
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { VNode } from '../../../../types'
2+
import { logger } from '../../../util/logger'
3+
import { defineSatoriTransformer } from '../utils'
4+
5+
export default defineSatoriTransformer([
6+
// need to make sure parent div has flex for the emoji to render inline
7+
{
8+
filter: (node: VNode) =>
9+
node.type === 'span' && node.props?.class?.includes('iconify'),
10+
transform: (node: VNode, e) => {
11+
if (import.meta.dev) {
12+
logger.warn(`When using the Nuxt Icon components in \`${e.options.component}\` you must provide \`mode="svg"\` to ensure correct rendering.`)
13+
}
14+
},
15+
},
16+
// need to make sure parent div has flex for the emoji to render inline
17+
{
18+
filter: (node: VNode) =>
19+
node.type === 'svg' && node.props?.class?.includes('iconify'),
20+
transform: (node: VNode, e) => {
21+
// strip classes
22+
node.props.class = node.props.class
23+
.split(' ')
24+
.filter(c => !c.startsWith('iconify'))
25+
.join(' ')
26+
},
27+
},
28+
])

‎src/runtime/server/og-image/satori/vnodes.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import emojis from './plugins/emojis'
77
import encoding from './plugins/encoding'
88
import flex from './plugins/flex'
99
import imageSrc from './plugins/imageSrc'
10+
import nuxtIcon from './plugins/nuxt-icon'
1011
import unocss from './plugins/unocss'
1112
import { applyEmojis } from './transforms/emojis'
1213
import { applyInlineCss } from './transforms/inlineCss'
@@ -38,6 +39,7 @@ export async function createVNodes(ctx: OgImageRenderEventContext): Promise<VNod
3839
classes,
3940
flex,
4041
encoding,
42+
nuxtIcon,
4143
])
4244
// do async transforms
4345
await Promise.all(walkSatoriTree(ctx, satoriTree, [

0 commit comments

Comments
 (0)
Please sign in to comment.