~james/core

core/web/src/lib/seo/jsonLd.ts -rw-r--r-- 499 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
// JSON.stringify leaves `<` alone, so a user-written bio or description containing
// `</script>` would close the tag and run whatever follows. escaping these keeps the
// output valid JSON that parses to the same value
const UNSAFE: Record<string, string> = {
	"<": "\\u003c",
	">": "\\u003e",
	"&": "\\u0026",
	"\u2028": "\\u2028",
	"\u2029": "\\u2029"
};

export const serializeJsonLd = (schema: unknown): string =>
	JSON.stringify(schema).replace(/[<>&\u2028\u2029]/g, (char) => UNSAFE[char]);