Skip to content

Commit 0cdd069

Browse files
Added core typings and usage examples to social-urls and schema-org
1 parent e40e71d commit 0cdd069

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

packages/schema-org/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,113 @@ or
1111

1212
## Usage
1313

14+
```js
15+
const SchemaGenerator = require('@tryghost/schema-org');
16+
17+
const schema = new SchemaGenerator();
18+
const makeSchemaTags = json => `<script type="application/ld+json">\n${json}\n</script>`;
19+
20+
// Schema data pertaining to the entire site
21+
const site = {
22+
title: 'Ghost',
23+
url: 'https://demo.ghost.io',
24+
image: {
25+
url: 'https://static.ghost.org/v2.0.0/images/welcome-to-ghost.jpg',
26+
// Optional!
27+
dimensions: {
28+
width: 1600,
29+
height: 1050
30+
}
31+
}
32+
};
33+
34+
// Example: generate schema for a post.
35+
// Other supported options are `author`, `tag`, and `home`
36+
const postObject = ghostPost(); // https://ghost.org/docs/api/v3/content/#posts
37+
38+
// Schema data pertaining to this specific resource
39+
const postMeta = {
40+
author: {
41+
name: postObject.authors[0].name,
42+
url: postObject.authors[0].url,
43+
image: postObject.authors[0].profile_image,
44+
description: postObject.authors[0].meta_description || postObject.authors[0].bio
45+
},
46+
meta: {
47+
title: postObject.title,
48+
url: postObject.url,
49+
datePublished: postObject.published_at,
50+
dateModified: postObject.updated_at,
51+
image: postObject.feature_image,
52+
keywords: postObject.tags.map(({name}) => name),
53+
description: postObject.custom_excerpt || postObject.excerpt
54+
}
55+
};
56+
57+
// Logs a valid JSON-LD script
58+
console.log(
59+
makeSchemaTags(schema.createSchema('post', {site, meta: postMeta}))
60+
);
61+
```
62+
63+
### Supported Schemas:
64+
65+
```typescript
66+
// type = 'author'
67+
interface AuthorSchemaProperties {
68+
site: SiteData; // See `site` in the example
69+
meta: {
70+
name: string;
71+
url: string;
72+
description?: string;
73+
sameAs?: string[]; // must be full URLs
74+
image?: ImageObject; // see `site.image` in the example
75+
}
76+
}
77+
78+
// type = 'home'
79+
interface HomeSchemaProperties {
80+
name: string;
81+
site: SiteData;
82+
meta: {
83+
url: string;
84+
description?: string;
85+
image?: ImageObject;
86+
}
87+
}
88+
89+
// type = 'post'
90+
interface PostSchemaProperties {
91+
site: SiteData;
92+
author?: {
93+
name: string;
94+
url: string;
95+
sameAs?: string[];
96+
image?: ImageData;
97+
description?: string;
98+
};
99+
meta: {
100+
title: string;
101+
url: string;
102+
datePublished?: Date | string;
103+
dateModified?: Date | string;
104+
image?: ImageData;
105+
keywords?: string[];
106+
description?: string;
107+
}
108+
}
109+
110+
// type = 'tag'
111+
interface TagSchemaProperties {
112+
site: SiteData;
113+
meta: {
114+
name: string;
115+
url: string;
116+
image?: ImageObject;
117+
description?: string;
118+
}
119+
}
120+
```
14121

15122
## Develop
16123

packages/schema-org/SchemaGenerator.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,80 @@ const trim = (schema) => {
1919
return schema;
2020
};
2121

22+
/**
23+
* @typedef {{
24+
* url?: string;
25+
* title?: string;
26+
* logo?: ImageObject
27+
* }} SiteData
28+
*
29+
*
30+
* @typedef {{
31+
* url: string;
32+
* dimensions?: {
33+
* width?: number;
34+
* height?: number;
35+
* }
36+
* }} DimensionalImage
37+
*
38+
* @typedef {DimensionalImage | string} ImageObject
39+
*
40+
*
41+
* @typedef {{
42+
* site: SiteData;
43+
* meta: {
44+
* name: string;
45+
* url: string;
46+
* description?: string;
47+
* sameAs?: string[];
48+
* image?: ImageObject;
49+
* }
50+
* }} AuthorSchemaProperties
51+
*
52+
*
53+
* @typedef {{
54+
* name: string;
55+
* site: SiteData;
56+
* meta: {
57+
* url: string;
58+
* description?: string;
59+
* image?: ImageObject;
60+
* }
61+
* }} HomeSchemaProperties
62+
*
63+
*
64+
* @typedef {{
65+
* site: SiteData;
66+
* author?: {
67+
* name: string;
68+
* url: string;
69+
* sameAs?: string[];
70+
* image?: ImageData;
71+
* description?: string;
72+
* }
73+
* meta: {
74+
* title: string;
75+
* url: string;
76+
* datePublished?: Date | string;
77+
* dateModified?: Date | string;
78+
* image?: ImageData;
79+
* keywords?: string[];
80+
* description?: string;
81+
* }
82+
* }} PostSchemaProperties
83+
*
84+
*
85+
* @typedef {{
86+
* site: SiteData;
87+
* meta: {
88+
* name: string;
89+
* url: string;
90+
* image?: ImageObject;
91+
* description?: string;
92+
* }
93+
* }} TagSchemaProperties
94+
*/
95+
2296
class SchemaGenerator {
2397
constructor(options) {
2498
this.options = options || {};
@@ -68,6 +142,10 @@ class SchemaGenerator {
68142
return trim(json);
69143
}
70144

145+
/**
146+
* @param {'home' | 'author' | 'post' | 'tag'} type
147+
* @param {HomeSchemaProperties | AuthorSchemaProperties | PostSchemaProperties | TagSchemaProperties} data
148+
*/
71149
createSchema(type, data) {
72150
if (!_.has(this.templates, type)) {
73151
type = 'home';

packages/social-urls/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ or
1111

1212
## Usage
1313

14+
```js
15+
const {twitter: makeTwitter, facebook: makeFacebook} = require('@tryghost/social-urls');
16+
17+
const socialUrls = [
18+
makeTwitter('@ghost'), // https://twitter.com/ghost
19+
makeFacebook('/ghost') // https://facebook.com/ghost
20+
];
21+
```
1422

1523
## Develop
1624

packages/social-urls/lib/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
/**
2+
* @param {string} username
3+
* @returns {string}
4+
*/
15
module.exports.twitter = function twitter(username) {
26
// Creates the canonical twitter URL without the '@'
37
return 'https://twitter.com/' + username.replace(/^@/, '');
48
};
59

10+
/**
11+
* @param {string} username
12+
* @returns {string}
13+
*/
614
module.exports.facebook = function facebook(username) {
715
// Handles a starting slash, this shouldn't happen, but just in case
816
return 'https://www.facebook.com/' + username.replace(/^\//, '');

0 commit comments

Comments
 (0)