Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Helmet script props. #19

Merged
merged 2 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

# react-schemaorg

Easily insert valid Schema.org JSON-LD using the custom `<JsonLd>` react
component.
Easily insert valid Schema.org JSON-LD in your React apps.

This library provides `<JsonLd>` for plain React apps, and `helmetJsonLdProp()`
for use with [`<Helmet>`](https://github.com/nfl/react-helmet).

Uses [schema-dts](https://github.com/google/schema-dts) for Schema.org
TypeScript definitions.
Expand All @@ -21,7 +23,9 @@ npm install schema-dts
npm install react-schemaorg
```

Then, to insert a simple JSON-LD snippet:
### Plain React Usage

To insert a simple JSON-LD snippet:

```ts
import { Person } from "schema-dts";
Expand All @@ -43,6 +47,31 @@ export function GraceHopper() {
}
```

### [React Helmet](https://github.com/nfl/react-helmet) Usage

To set JSON-LD in React Helmet, you need to pass it to the `script={[...]}` prop
array in the `Helmet` component:

```tsx
import { Person } from "schema-dts";
import { helmetJsonLdProp } from "react-schemaorg";
import { Helmet } from "react-helmet";

<Helmet script={[
helmetJsonLdProp<Person>({
"@context": "https://schema.org",
"@type": "Person",
name: "Grace Hopper",
alternateName: "Grace Brewster Murray Hopper",
alumniOf: {
"@type": "CollegeOrUniversity",
name: ["Yale University", "Vassar College"]
},
knowsAbout: ["Compilers", "Computer Science"]
}),
]} />
```

## Developers

Use NPM to install dependencies:
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"Semantic Web",
"semantic-web",
"Linked Data",
"linked-data"
"linked-data",
"react-helmet",
"helmet",
"script"
],
"homepage": "https://github.com/google/react-schemaorg#readme",
"bugs": "https://github.com/google/react-schemaorg/issues",
Expand Down
38 changes: 34 additions & 4 deletions src/json-ld.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
import * as React from "react";
import { Thing, WithContext } from "schema-dts";

interface JsonLdOptions {
/** Adds indentation, white space, and line break characters to JSON-LD output. {@link JSON.stringify} */
space?: string | number;
}

/**
* Component that inline-includes a JSON-LD script where specified.
*
* @example
* For Example:
*
* ```tsx
* <JsonLd<Person>
* item={{
Expand All @@ -38,10 +44,8 @@ import { Thing, WithContext } from "schema-dts";
* />
* ```
*/
export class JsonLd<T extends Thing> extends React.Component<{
export class JsonLd<T extends Thing> extends React.Component<JsonLdOptions & {
item: WithContext<T>;
/** Adds indentation, white space, and line break characters to JSON-LD output. {@link JSON.stringify} */
space?: string | number;
}> {
render() {
return (
Expand All @@ -55,6 +59,32 @@ export class JsonLd<T extends Thing> extends React.Component<{
}
}

/**
* Produces a Helmet-style <script> prop for a given JSON-LD datum.
*
* For example:
*
* ```tsx
* <Helmet script={[
* helmetJsonLdProp<Person>({
* "@context": "https://schema.org",
* "@type": "Person",
* name: "Grace Hopper",
* alternateName: "Grace Brewster Murray Hopper",
* alumniOf: {
* "@type": "CollegeOrUniversity",
* name: ["Yale University", "Vassar College"]
* },
* knowsAbout: ["Compilers", "Computer Science"]
* }),
* ]} />
* ```
*/
export const helmetJsonLdProp = <T extends Thing>(item: WithContext<T>, options: JsonLdOptions = {}) => ({
type: "application/ld+json" as const,
innerHTML: JSON.stringify(item, safeJsonLdReplacer, options.space),
});

type JsonValueScalar = string | boolean | number;
type JsonValue =
| JsonValueScalar
Expand Down