Skip to content

Commit

Permalink
Feature/dataset jsonld comp (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myoxocephalus committed Feb 21, 2020
1 parent db9cada commit 0add384
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 2 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Version One docs can be found [here](https://github.com/garmeeh/next-seo/tree/su
- [Breadcrumb](#breadcrumb)
- [Blog](#blog)
- [Course](#course)
- [Dataset](#dataset)
- [Corporate Contact](#corporate-contact)
- [FAQ Page](#faq-page)
- [Job Posting](#job-posting)
Expand Down Expand Up @@ -814,6 +815,7 @@ Below you will find a very basic page implementing each of the available JSON-LD
- [Breadcrumb](#breadcrumb)
- [Blog](#blog)
- [Course](#course)
- [Dataset](#dataset)
- [Corporate Contact](#corporate-contact)
- [FAQ Page](#faq-page)
- [Job Posting](#job-posting)
Expand Down Expand Up @@ -957,6 +959,37 @@ export default () => (
| ------------- | ------------------------------- |
| `providerUrl` | The url to the course provider. |

### Dataset

```jsx
import React from 'react';
import { DatasetJsonLd } from 'next-seo';

export default () => (
<>
<h1>Dataset JSON-LD</h1>
<DatasetJsonLd
description="The description needs to be at least 50 characters long"
name="name of the dataset"
license="https//www.example.com"
/>
</>
);
```

**Required properties**

| Property | Info |
| ------------- | --------------------------------------------------------------------------------- |
| `description` | A short summary describing a dataset. Needs to be between 50 and 5000 characters. |
| `name` | A license under which the dataset is distributed. |

**Recommended properties**

| Property | Info |
| --------- | ------------------------------- |
| `license` | The url to the course provider. |

### Corporate Contact

```jsx
Expand Down
30 changes: 29 additions & 1 deletion cypress/e2e/jsonld.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertSchema } from '@cypress/schema-tools';
import schemas from '../schemas';

const expectedJSONResults = 13;
const expectedJSONResults = 14;

const articleLdJsonIndex = 0;
const breadcrumbLdJsonIndex = 1;
Expand All @@ -16,6 +16,7 @@ const newsarticleLdJsonIndex = 9;
const faqPageLdJsonIndex = 10;
const jobPostingLdJsonIndex = 11;
const eventLdJsonIndex = 12;
const datasetLdJsonIndex = 13;

describe('Validates JSON-LD For:', () => {
it('Article', () => {
Expand Down Expand Up @@ -645,4 +646,31 @@ describe('Validates JSON-LD For:', () => {
});
});
});

it('Dataset', () => {
cy.visit('http://localhost:3000/jsonld');
cy.get('head script[type="application/ld+json"]')
.should('have.length', expectedJSONResults)
.then(tags => {
const jsonLD = JSON.parse(tags[datasetLdJsonIndex].innerHTML);
assertSchema(schemas)('Dataset', '1.0.0')(jsonLD);
});
});

it('Dataset Matches', () => {
cy.visit('http://localhost:3000/jsonld');
cy.get('head script[type="application/ld+json"]')
.should('have.length', expectedJSONResults)
.then(tags => {
const jsonLD = JSON.parse(tags[datasetLdJsonIndex].innerHTML);
expect(jsonLD).to.deep.equal({
'@context': 'http://schema.org',
'@type': 'Dataset',
description:
'The description needs to be at least 50 characters long',
name: 'name of the dataset',
license: 'https//www.example.com',
});
});
});
});
48 changes: 48 additions & 0 deletions cypress/schemas/dataset-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { versionSchemas } from '@cypress/schema-tools';

const dataset100 = {
version: {
major: 1,
minor: 0,
patch: 0,
},
schema: {
type: 'object',
title: 'Dataset',
description: 'An example schema describing JSON-LD for type: Dataset',
properties: {
'@context': {
type: 'string',
description: 'Schema.org context',
},
'@type': {
type: 'string',
description: 'Dataset',
},
description: {
type: 'string',
description: 'Description of the dataset',
},
name: {
type: 'string',
description: 'Name of the dataset',
},
license: {
type: 'string',
description: 'License for the dataset',
},
},
required: true,
additionalProperties: false,
},
example: {
'@context': 'http://schema.org',
'@type': 'Dataset',
description: 'The description needs to be at least 50 characters long',
name: 'name of the dataset',
license: 'https//www.example.com',
},
};

const dataset = versionSchemas(dataset100);
export default dataset;
2 changes: 2 additions & 0 deletions cypress/schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import newsarticleVersions from './newsarticle-schema';
import faqPageVersion from './faq-page-schema';
import jobPostingVersions from './job-posting-schema';
import eventVersion from './event-schema';
import datasetVersion from './dataset-schema';

const schemas = combineSchemas(
articleVersions,
Expand All @@ -28,5 +29,6 @@ const schemas = combineSchemas(
faqPageVersion,
jobPostingVersions,
eventVersion,
datasetVersion,
);
export default schemas;
10 changes: 9 additions & 1 deletion e2e/pages/jsonld.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
FAQPageJsonLd,
JobPostingJsonLd,
EventJsonLd,
DatasetJsonLd,
} from '../../lib';
import Links from '../components/links';

Expand Down Expand Up @@ -148,7 +149,7 @@ export default () => (
{
author: {
type: 'Person',
name: 'Jim'
name: 'Jim',
},
publisher: {
type: 'Organization',
Expand Down Expand Up @@ -298,6 +299,13 @@ export default () => (
images={['https://example.com/photos/photo.jpg']}
description="My event @ my place"
/>

<DatasetJsonLd
description="The description needs to be at least 50 characters long"
name="name of the dataset"
license="https//www.example.com"
/>

<Links />
</>
);
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
} from './jsonld/jobPosting';
export { default as BlogJsonLd, BlogJsonLdProps } from './jsonld/blog';
export { default as CourseJsonLd, CourseJsonLdProps } from './jsonld/course';
export { default as DatasetJsonLd, DatasetJsonLdProps } from './jsonld/dataset';
export {
default as LocalBusinessJsonLd,
LocalBusinessJsonLdProps,
Expand Down
40 changes: 40 additions & 0 deletions src/jsonld/dataset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { FC } from 'react';
import Head from 'next/head';

import markup from '../utils/markup';

export interface DatasetJsonLdProps {
description: string;
name: string;
license?: string;
}

const DatasetJsonLd: FC<DatasetJsonLdProps> = ({
description,
name,
license,
}) => {
const jslonld = `{
"@context": "http://schema.org",
"@type": "Dataset",
"description": "${description}",
"name": "${name}"${
license
? `,
"license": "${license}"`
: ''
}
}`;

return (
<Head>
<script
type="application/ld+json"
dangerouslySetInnerHTML={markup(jslonld)}
key="jsonld-dataset"
/>
</Head>
);
};

export default DatasetJsonLd;

0 comments on commit 0add384

Please sign in to comment.