@@ -5,9 +5,14 @@ import Intl from 'ember-intl/services/intl';
55import pathJoin from 'ember-osf-web/utils/path-join' ;
66import toArray from 'ember-osf-web/utils/to-array' ;
77
8- export type Content = string | number | null | undefined ;
8+ interface MetaTagAuthor {
9+ givenName : string ;
10+ familyName : string ;
11+ }
12+
13+ type Content = string | number | null | undefined | MetaTagAuthor ;
914
10- export type DataContent = Content | Content [ ] ;
15+ type DataContent = Content | Content [ ] ;
1116
1217export interface MetaTagsData {
1318 title ?: DataContent ;
@@ -30,22 +35,22 @@ export interface MetaTagsData {
3035 fbAppId ?: DataContent ;
3136 twitterSite ?: DataContent ;
3237 twitterCreator ?: DataContent ;
33- author ?: DataContent ;
38+ contributors ?: DataContent ;
3439 keywords ?: DataContent ;
3540}
3641
37- export interface MetaTagsDefs {
42+ interface MetaTagsDefs {
3843 [ s : string ] : DataContent ;
3944}
4045
4146export interface NameMetaTagAttrs {
4247 name : string ;
43- content : Content ;
48+ content : DataContent ;
4449}
4550
4651export interface PropMetaTagAttrs {
4752 property : string ;
48- content : Content ;
53+ content : DataContent ;
4954}
5055
5156export interface LinkMetaTagAttrs {
@@ -57,7 +62,7 @@ export interface ScriptTagAttrs {
5762 type : string ;
5863}
5964
60- export type MetaTagAttrs = NameMetaTagAttrs | PropMetaTagAttrs | LinkMetaTagAttrs | ScriptTagAttrs ;
65+ type MetaTagAttrs = NameMetaTagAttrs | PropMetaTagAttrs | LinkMetaTagAttrs | ScriptTagAttrs ;
6166
6267export interface HeadTagDef {
6368 type : string ;
@@ -70,14 +75,98 @@ export default class MetaTags extends Service {
7075 @service router ! : any ;
7176 @service headTags ! : HeadTagsService ;
7277
78+ /**
79+ * buildPersonScriptTag
80+ * ENG-6331 requested more granularity for authors
81+ * This abstracted method get the granulatority required
82+ *
83+ * @param contributors The list of contributors
84+ *
85+ * @returns {HeadTagDef }
86+ */
87+ private buildPersonScriptTag ( contributors : DataContent ) : HeadTagDef {
88+ const contributor = [ ] ;
89+ if ( Array . isArray ( contributors ) ) {
90+ contributors . forEach ( ( person : MetaTagAuthor ) => {
91+ contributor . push ( Object ( {
92+ '@type' : 'schema:Person' ,
93+ givenName : person . givenName ,
94+ familyName : person . familyName ,
95+ } ) ) ;
96+ } ) ;
97+ } else {
98+ contributor . push ( Object ( {
99+ '@type' : 'schema:Person' ,
100+ givenName : ( contributors as MetaTagAuthor ) . givenName ,
101+ familyName : ( contributors as MetaTagAuthor ) . familyName ,
102+ } ) ) ;
103+ }
104+
105+ return {
106+ type : 'script' ,
107+ content : JSON . stringify ( {
108+ '@context' : {
109+ dc : 'http://purl.org/dc/elements/1.1/' ,
110+ schema : 'http://schema.org' ,
111+ } ,
112+ '@type' : 'schema:CreativeWork' ,
113+ contributor,
114+ } ) ,
115+ attrs : {
116+ type : 'application/ld+json' ,
117+ } ,
118+ } ;
119+ }
120+
121+ /**
122+ * Get head tag definitions suitable for the head-tags service.
123+ *
124+ * @method getHeadTags
125+ * @param {MetaTagsData } metaTagsData Data values to use for meta tags.
126+ * @return {MetaTagAttrs[] } Returns head tag defintions.
127+ */
128+ public getHeadTags ( metaTagsData : MetaTagsData ) : HeadTagDef [ ] {
129+ const metaTagsDefs = this . getMetaTags ( metaTagsData ) ;
130+ // Morph MetaTagsDefs into an array of MetaTagAttrs.
131+ const headTagsAttrs : MetaTagAttrs [ ] = Object . entries ( metaTagsDefs )
132+ . reduce (
133+ ( acc : MetaTagAttrs [ ] , [ name , content ] ) => acc . concat (
134+ toArray ( content ) . map ( contentMember => this . makeMetaTagAttrs ( name , contentMember ) ) ,
135+ ) , [ ] ,
136+ ) ;
137+
138+
139+ const headTags = headTagsAttrs
140+ . filterBy ( 'content' ) // Remove tags with no content.
141+ . map ( attrs => ( { type : 'meta' , attrs } ) ) ;
142+
143+ if ( metaTagsData ?. contributors ) {
144+ headTags . push ( this . buildPersonScriptTag ( metaTagsData . contributors ) ) ;
145+ }
146+
147+ return headTags ;
148+ }
149+
150+ public updateHeadTags ( ) {
151+ this . headTags . collectHeadTags ( ) ;
152+
153+ // https://www.zotero.org/support/dev/exposing_metadata#force_zotero_to_refresh_metadata
154+ const ev = new Event ( 'ZoteroItemUpdated' , {
155+ bubbles : true ,
156+ cancelable : true ,
157+ } ) ;
158+ document . dispatchEvent ( ev ) ;
159+ }
160+
161+
73162 /**
74163 * Get meta tag definitions.
75164 *
76165 * @method getMetaTags
77166 * @param {MetaTagsData } metaTagsOverrides Data values to override defaults.
78167 * @return {MetaTagsDefs } Returns meta tag definitions.
79168 */
80- getMetaTags ( metaTagsOverrides : MetaTagsData ) : MetaTagsDefs {
169+ private getMetaTags ( metaTagsOverrides : MetaTagsData ) : MetaTagsDefs {
81170 // Default values.
82171 const currentUrl = window . location . href ;
83172 const metaTagsData : MetaTagsData = {
@@ -109,7 +198,7 @@ export default class MetaTags extends Service {
109198 citation_doi : metaTagsData . doi ,
110199 citation_publisher : metaTagsData . siteName ,
111200 citation_author_institution : metaTagsData . institution ,
112- citation_author : metaTagsData . author ,
201+ citation_author : metaTagsData . contributors ,
113202 citation_description : metaTagsData . description ,
114203 citation_public_url : metaTagsData . url ,
115204 citation_publication_date : metaTagsData . publishedDate ,
@@ -123,7 +212,7 @@ export default class MetaTags extends Service {
123212 'dct.created' : metaTagsData . publishedDate ,
124213 'dc.publisher' : metaTagsData . siteName ,
125214 'dc.language' : metaTagsData . language ,
126- 'dc.contributor' : metaTagsData . author ,
215+ 'dc.contributor' : metaTagsData . contributors ,
127216 'dc.subject' : metaTagsData . keywords ,
128217 // Open Graph/Facebook
129218 'fb:app_id' : metaTagsData . fbAppId ,
@@ -151,45 +240,31 @@ export default class MetaTags extends Service {
151240 }
152241
153242 /**
154- * Get head tag definitions suitable for the head-tags service.
243+ * buildMetaTagContent
244+ * ENG-6331 requested more granularity for authors
245+ * This abstracted method get the granulatority required
155246 *
156- * @method getHeadTags
157- * @param { MetaTagsData } metaTagsData Data values to use for meta tags.
158- * @return { HeadTagDef[] } Returns head tag defintions.
247+ * @param name The name of the attribute
248+ * @param content The content
249+ * @returns { Content }
159250 */
160- getHeadTags ( metaTagsData : MetaTagsData ) : HeadTagDef [ ] {
161- const metaTagsDefs = this . getMetaTags ( metaTagsData ) ;
162- // Morph MetaTagsDefs into an array of MetaTagAttrs.
163- const headTagsAttrs : MetaTagAttrs [ ] = Object . entries ( metaTagsDefs )
164- . reduce (
165- ( acc : MetaTagAttrs [ ] , [ name , content ] ) => acc . concat (
166- toArray ( content ) . map ( contentMember => this . makeMetaTagAttrs ( name , contentMember ) ) ,
167- ) , [ ] ,
168- ) ;
251+ private buildMetaTagContent ( name : string , content : Content ) : Content {
252+ if ( [ 'citation_author' , 'dc.contributor' ] . includes ( name ) && typeof content === 'object' ) {
253+ return `${ content ?. familyName } , ${ content ?. givenName } ` ;
254+ }
169255
170- return headTagsAttrs
171- . filterBy ( 'content' ) // Remove tags with no content.
172- . map ( attrs => ( { type : 'meta' , attrs } ) ) ;
256+ return content ;
173257 }
174258
175- makeMetaTagAttrs ( name : string , content : Content ) : MetaTagAttrs {
259+ private makeMetaTagAttrs ( name : string , content : Content ) : MetaTagAttrs {
176260 // Open Graph/Facebook tags use 'property' instead of 'name'.
261+ content = this . buildMetaTagContent ( name , content ) ;
177262 if ( [ 'fb:' , 'og:' ] . includes ( name . substring ( 0 , 3 ) ) ) {
178- return { property : name , content } ;
263+ return { property : name , content} ;
179264 }
180- return { name, content } ;
265+ return { name, content} ;
181266 }
182267
183- updateHeadTags ( ) {
184- this . headTags . collectHeadTags ( ) ;
185-
186- // https://www.zotero.org/support/dev/exposing_metadata#force_zotero_to_refresh_metadata
187- const ev = new Event ( 'ZoteroItemUpdated' , {
188- bubbles : true ,
189- cancelable : true ,
190- } ) ;
191- document . dispatchEvent ( ev ) ;
192- }
193268}
194269
195270declare module '@ember/service' {
0 commit comments