42 lines
2.0 KiB
TypeScript
42 lines
2.0 KiB
TypeScript
type JsonLdSchema = Record<string, unknown>
|
|
|
|
/**
|
|
* Renders one or more schema.org objects as a single JSON-LD @graph in a
|
|
* <script type="application/ld+json">. The plugin owns ONLY the wrapping and the
|
|
* safe serialization — NOT the schema contents. The project builds its own nodes
|
|
* (with the plugin's buildWebSiteJsonLd / buildOrganizationJsonLd /
|
|
* buildLocalBusinessJsonLd / buildSiteNavigationJsonLd helpers) and maps its own
|
|
* company data to the right @type.
|
|
*
|
|
* Why not a <GlobalJsonLd> that reads Company/Header: company data is a business
|
|
* domain that varies by country and industry (NIP/KRS in PL, ABN in AU, EIN in
|
|
* US; barAssociation for a law firm, licenceText for an HVAC business). A fixed
|
|
* model would be a leaky abstraction. So the project decides the @type and
|
|
* fields; the plugin just wraps and renders safely.
|
|
*
|
|
* // project — build nodes from YOUR data, choose the @type for YOUR market
|
|
* import { JsonLdGraph, buildWebSiteJsonLd, buildOrganizationJsonLd } from '@intecion/ipal-kit'
|
|
* const website = buildWebSiteJsonLd({ name, url })
|
|
* const org = buildOrganizationJsonLd({ name, url, logo })
|
|
* const localBusiness = { '@type': 'HVACBusiness', taxID: company.abn, areaServed: company.serviceArea, ... }
|
|
* <JsonLdGraph schemas={[website, org, localBusiness]} />
|
|
*
|
|
* Replaces the ~140-line JsonLd.tsx (only the @graph wrapper + <script> was
|
|
* actually duplicated; the schema mapping stays in the project where it belongs).
|
|
*/
|
|
export function JsonLdGraph({ schemas }: { schemas: Array<JsonLdSchema | null | undefined> }) {
|
|
const graph = schemas.filter((s): s is JsonLdSchema => Boolean(s))
|
|
if (graph.length === 0) {return null}
|
|
|
|
const json = JSON.stringify({ '@context': 'https://schema.org', '@graph': graph })
|
|
|
|
return (
|
|
<script
|
|
// Escape '<' to prevent the JSON from breaking out of the script tag
|
|
// (XSS hardening) — the only safety concern the plugin owns here.
|
|
dangerouslySetInnerHTML={{ __html: json.replace(/</g, '\\u003c') }}
|
|
type="application/ld+json"
|
|
/>
|
|
)
|
|
}
|