-
Notifications
You must be signed in to change notification settings - Fork 56
/
meta-tags.ts
188 lines (169 loc) · 6.61 KB
/
meta-tags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { service } from '@ember-decorators/service';
import Service from '@ember/service';
import HeadTagsService from 'ember-cli-meta-tags/services/head-tags';
import config from 'ember-get-config';
import I18N from 'ember-i18n/services/i18n';
import pathJoin from 'ember-osf-web/utils/path-join';
import toArray from 'ember-osf-web/utils/to-array';
export type Content = string | number | null | undefined;
export type DataContent = Content | Content[];
export interface MetaTagsData {
title?: DataContent;
type?: DataContent;
description?: DataContent;
url?: DataContent;
doi?: DataContent;
identifier?: DataContent;
publishedDate?: DataContent;
modifiedDate?: DataContent;
license?: DataContent;
language?: DataContent;
image?: DataContent;
imageType?: DataContent;
imageWidth?: DataContent;
imageHeight?: DataContent;
imageAlt?: DataContent;
siteName?: DataContent;
institution?: DataContent;
fbAppId?: DataContent;
twitterSite?: DataContent;
twitterCreator?: DataContent;
author?: DataContent;
keywords?: DataContent;
}
export interface MetaTagsDefs {
[s: string]: DataContent;
}
export interface NameMetaTagAttrs {
name: string;
content: Content;
}
export interface PropMetaTagAttrs {
property: string;
content: Content;
}
export type MetaTagAttrs = NameMetaTagAttrs | PropMetaTagAttrs;
export interface HeadTagDef {
type: string;
attrs: MetaTagAttrs;
}
export default class MetaTags extends Service {
@service i18n!: I18N;
@service router!: any;
@service headTags!: HeadTagsService;
/**
* Get meta tag definitions.
*
* @method getMetaTags
* @param {MetaTagsData} metaTagsOverrides Data values to override defaults.
* @return {MetaTagsDefs} Returns meta tag definitions.
*/
getMetaTags(this: MetaTags, metaTagsOverrides: MetaTagsData): MetaTagsDefs {
// Default values.
const metaTagsData: MetaTagsData = {
type: 'article',
description: this.get('i18n').t('general.hosted_on_the_osf'),
url: pathJoin(config.OSF.url, this.get('router').get('currentURL')),
language: this.get('i18n').get('locale'),
image: pathJoin(config.OSF.url, 'static/img/preprints_assets/osf/sharing.png'),
imageType: 'image/png',
imageWidth: 1200,
imageHeight: 630,
imageAlt: this.get('i18n').t('home.brand'),
siteName: this.get('i18n').t('home.brand'),
institution: this.get('i18n').t('general.cos'),
fbAppId: config.FB_APP_ID,
twitterSite: config.social.twitter.viaHandle,
twitterCreator: config.social.twitter.viaHandle,
...metaTagsOverrides,
};
// Include URL, DOI, and any additional identifiers.
const identifiers = toArray(metaTagsData.url)
.concat(toArray(metaTagsData.doi))
.concat(toArray(metaTagsData.identifier));
return {
// Citation
citation_title: metaTagsData.title,
citation_doi: metaTagsData.doi,
citation_publisher: metaTagsData.siteName,
citation_author_institution: metaTagsData.institution,
citation_author: metaTagsData.author,
citation_description: metaTagsData.description,
citation_public_url: metaTagsData.url,
citation_publication_date: metaTagsData.publishedDate,
// Dublin Core
'dc.title': metaTagsData.title,
'dc.type': metaTagsData.type,
'dc.identifier': identifiers,
'dc.abstract': metaTagsData.description,
'dc.license': metaTagsData.license,
'dc.datemodified': metaTagsData.modifiedDate,
'dc.datesubmitted': metaTagsData.publishedDate,
'dc.publisher': metaTagsData.siteName,
'dc.language': metaTagsData.language,
'dc.creator': metaTagsData.author,
'dc.keywords': metaTagsData.keywords,
// Open Graph/Facebook
'fb:app_id': metaTagsData.fbAppId,
'og:ttl': 345600, // 4 days = min value.
'og:title': metaTagsData.title,
'og:type': metaTagsData.type,
'og:site_name': metaTagsData.siteName,
'og:url': metaTagsData.url,
'og:secure_url': metaTagsData.url,
'og:description': metaTagsData.description,
'og:image': metaTagsData.image,
'og:image:type': metaTagsData.imageType,
'og:image:width': metaTagsData.imageWidth,
'og:image:height': metaTagsData.imageHeight,
'og:image:alt': metaTagsData.imageAlt,
// Twitter
'twitter:card': 'summary',
'twitter:site': metaTagsData.twitterSite,
'twitter:creator': metaTagsData.twitterCreator,
'twitter:title': metaTagsData.title,
'twitter:description': metaTagsData.description,
'twitter:image': metaTagsData.image,
'twitter:image:alt': metaTagsData.imageAlt,
};
}
/**
* Get head tag definitions suitable for the head-tags service.
*
* @method getHeadTags
* @param {MetaTagsData} metaTagsData Data values to use for meta tags.
* @return {HeadTagDef[]} Returns head tag defintions.
*/
getHeadTags(this: MetaTags, metaTagsData: MetaTagsData): HeadTagDef[] {
const metaTagsDefs = this.getMetaTags(metaTagsData);
// Morph MetaTagsDefs into an array of MetaTagAttrs.
const headTagsAttrs: MetaTagAttrs[] = Object.entries(metaTagsDefs)
.reduce((acc: MetaTagAttrs[], [name, content]) =>
acc.concat(toArray(content).map(contentMember =>
this.makeMetaTagAttrs(name, contentMember))), []);
return headTagsAttrs
.filterBy('content') // Remove tags with no content.
.map(attrs => ({ type: 'meta', attrs }));
}
makeMetaTagAttrs(name: string, content: Content): MetaTagAttrs {
// Open Graph/Facebook tags use 'property' instead of 'name'.
if (['fb:', 'og:'].includes(name.substring(0, 3))) {
return { property: name, content };
}
return { name, content };
}
updateHeadTags() {
this.headTags.collectHeadTags();
// https://www.zotero.org/support/dev/exposing_metadata#force_zotero_to_refresh_metadata
const ev = new Event('ZoteroItemUpdated', {
bubbles: true,
cancelable: true,
});
document.dispatchEvent(ev);
}
}
declare module '@ember/service' {
interface Registry {
'meta-tags': MetaTags;
}
}