// Safely serialise a value for embedding in a <script type="application/ld+json"> tag.
// Escapes every character that could end the script context or be reinterpreted
// by the HTML parser before the JSON parser sees it: '<', '>', '&', U+2028, U+2029.
export function safeJsonLd(data: unknown): string {
  return JSON.stringify(data)
    .replace(/</g, '\\u003c')
    .replace(/>/g, '\\u003e')
    .replace(/&/g, '\\u0026')
    .replace(/\u2028/g, '\\u2028')
    .replace(/\u2029/g, '\\u2029');
}
