Skip to content

Commit 3f9677f

Browse files
fix(content): decode HTML entities in OG image markup (#2774)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent c3189a5 commit 3f9677f

7 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎packages/content/og/src/lib/og.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Credit for modified source: https://github.com/etherCorps/sveltekit-og/blob/main/src/lib/api.ts
22

33
import satori from 'satori';
4-
import { html as toReactElement } from 'satori-html';
54
import sharp from 'sharp';
65

76
import { ImageResponseOptions } from './options';
7+
import { parseHtml } from './parse-html';
88

99
export const generateImage = async (
1010
element: string,
1111
options: ImageResponseOptions,
1212
): Promise<Buffer> => {
13-
const elementHtml = toReactElement(element);
13+
const elementHtml = parseHtml(element);
1414
const svg = await satori(elementHtml as any, {
1515
width: options.width || 1200,
1616
height: options.height || 630,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { parseHtml } from './parse-html';
4+
5+
describe('parseHtml', () => {
6+
it('decodes entities in text and attribute values', () => {
7+
const node = parseHtml(
8+
'<div title="Q&amp;A"><span>Research &amp; Development</span> &copy; 2026</div>',
9+
);
10+
11+
expect(JSON.stringify(node)).toContain('"title":"Q&A"');
12+
expect(JSON.stringify(node)).toContain('"Research & Development"');
13+
expect(JSON.stringify(node)).toContain('"© 2026"');
14+
});
15+
16+
it('keeps escaped markup as text', () => {
17+
const node = parseHtml('<div>&lt;b&gt;bold&lt;/b&gt;</div>');
18+
19+
expect(JSON.stringify(node)).toContain('"<b>bold</b>"');
20+
expect(JSON.stringify(node)).not.toContain('"type":"b"');
21+
});
22+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { decodeHTML, decodeHTMLAttribute } from 'entities';
2+
import { html } from 'satori-html';
3+
4+
interface ParsedNode {
5+
type: string;
6+
props: {
7+
children?: ParsedChild | ParsedChild[];
8+
[prop: string]: unknown;
9+
};
10+
}
11+
12+
type ParsedChild = string | ParsedNode;
13+
14+
// satori-html leaves entities in text and attribute values undecoded.
15+
export function parseHtml(element: string): ParsedNode {
16+
const node: ParsedNode = html(element);
17+
decodeNode(node);
18+
return node;
19+
}
20+
21+
function decodeNode(node: ParsedNode): void {
22+
const { props } = node;
23+
for (const [key, value] of Object.entries(props)) {
24+
if (key !== 'children' && typeof value === 'string') {
25+
props[key] = decodeHTMLAttribute(value);
26+
}
27+
}
28+
29+
const { children } = props;
30+
props.children = Array.isArray(children)
31+
? children.map(decodeChild)
32+
: children && decodeChild(children);
33+
}
34+
35+
function decodeChild(child: ParsedChild): ParsedChild {
36+
if (typeof child === 'string') {
37+
return decodeHTML(child);
38+
}
39+
decodeNode(child);
40+
return child;
41+
}

‎packages/content/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
}
8888
},
8989
"dependencies": {
90+
"entities": "catalog:",
9091
"tslib": "catalog:"
9192
},
9293
"devDependencies": {

‎packages/content/vite.config.lib.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default defineConfig({
4949
/^rxjs/,
5050
/^md4x/,
5151
/^vite/,
52+
'entities',
5253
'front-matter',
5354
'sharp',
5455
'tslib',

‎pnpm-lock.yaml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ catalog:
114114
eslint-plugin-playwright: ^2.10.1
115115
eslint-plugin-storybook: 10.3.5
116116
front-matter: ^4.0.2
117+
entities: ^8.0.0
117118
es-toolkit: ^1.45.1
118119
execa: ^9.6.1
119120
fast-glob: ^3.3.3

0 commit comments

Comments
 (0)