1- import type { Link , Meta } from '@unhead/vue'
2-
3- type GraphqlDrupalMetatagAttribute = {
4- key : string
5- value : string
6- }
7-
8- type GraphqlDrupalMetatag = {
9- id ?: string
10- tag ?: string
11- attributes : GraphqlDrupalMetatagAttribute [ ]
12- }
1+ import type {
2+ MetatagFragment ,
3+ UseDrupalRouteFragment ,
4+ } from '#graphql-operations'
5+ import type { Link , Meta , Script } from '@unhead/vue'
6+ import type { DrupalRouteMetatags } from '../../types/metatags'
137
148type GraphqlDrupalMetatags = {
15- metatags : GraphqlDrupalMetatag [ ]
16- schema : string
17- }
18-
19- type DrupalRouteMetatags = {
20- title : string
21- link : Link [ ]
22- meta : Link [ ]
23- schema : string
9+ metatags ?: MetatagFragment [ ]
10+ schemaOrgMetatags ?: {
11+ json ?: string
12+ }
2413}
2514
2615/**
2716 * Get the page title from the Drupal metatags.
2817 */
29- function getTitle ( tag : GraphqlDrupalMetatag ) : string | undefined {
18+ function getTitle ( tag : MetatagFragment ) : string | undefined {
3019 if ( tag . id !== 'title' ) {
3120 return
3221 }
@@ -49,52 +38,57 @@ function getTitle(tag: GraphqlDrupalMetatag): string | undefined {
4938 * single object:
5039 * { one: 'foo', two: 'bar' }
5140 */
52- function getTagObject (
53- attributes : GraphqlDrupalMetatagAttribute [ ] ,
54- ) : Link | Meta {
41+ function getTagObject ( attributes : MetatagFragment [ 'attributes' ] ) : Link | Meta {
5542 return attributes . reduce < Record < string , string > > ( ( acc , v ) => {
5643 acc [ v . key ] = v . value
5744 return acc
5845 } , { } )
5946}
6047
6148export function buildDrupalMetatags (
62- data :
63- | GraphqlDrupalMetatags
64- | GraphqlDrupalMetatags [ 'metatags' ]
65- | undefined
66- | null ,
49+ data : GraphqlDrupalMetatags | UseDrupalRouteFragment | undefined | null ,
6750) : DrupalRouteMetatags {
6851 try {
69- const link : Link [ ] = [ ]
70- const meta : Meta [ ] = [ ]
71- let title : string = ''
72- const schema = data && 'schema' in data ? data . schema : ''
52+ if ( data && 'route' in data && data . route && 'metatags' in data . route ) {
53+ const route = data . route
54+ const schemaOrg = route . schemaOrgMetatags ?. json || ''
55+ const tags = route . metatags
56+
57+ const link : Link [ ] = [ ]
58+ const meta : Meta [ ] = [ ]
59+ const script : Script [ ] = [ ]
7360
74- const tags = data && 'metatags' in data ? data . metatags : data
61+ if ( schemaOrg ) {
62+ script . push ( {
63+ type : 'application/ld+json' ,
64+ innerHTML : schemaOrg ,
65+ } )
66+ }
7567
76- if ( tags && Array . isArray ( tags ) ) {
77- for ( let i = 0 ; i < tags . length ; i ++ ) {
78- const tag = tags [ i ] !
79- const tagTitle = getTitle ( tag )
80- if ( tagTitle ) {
81- title = tagTitle
82- } else {
83- const item = getTagObject ( tag . attributes )
84- if ( tag . tag === 'link' ) {
85- link . push ( item )
86- } else if ( tag . tag === 'meta' ) {
87- meta . push ( item )
68+ let title : string = ''
69+ if ( tags && Array . isArray ( tags ) ) {
70+ for ( let i = 0 ; i < tags . length ; i ++ ) {
71+ const tag = tags [ i ] !
72+ const tagTitle = getTitle ( tag )
73+ if ( tagTitle ) {
74+ title = tagTitle
75+ } else {
76+ const item = getTagObject ( tag . attributes )
77+ if ( tag . tag === 'link' ) {
78+ link . push ( item )
79+ } else if ( tag . tag === 'meta' ) {
80+ meta . push ( item )
81+ }
8882 }
8983 }
9084 }
91- }
9285
93- return { link, meta, title, schema }
86+ return { link, meta, title, script, schemaOrg }
87+ }
9488 } catch ( e ) {
9589 console . log ( 'Error in Vuepal:' )
9690 console . log ( e )
9791 }
9892
99- return { link : [ ] , meta : [ ] , title : '' , schema : '' }
93+ return { link : [ ] , meta : [ ] , script : [ ] , title : '' , schemaOrg : '' }
10094}
0 commit comments