Skip to content

Commit c1f4859

Browse files
committed
feat: schemaOrgMetatags support
1 parent 172b340 commit c1f4859

File tree

7 files changed

+81
-93
lines changed

7 files changed

+81
-93
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuepal",
3-
"version": "3.0.0-alpha.3",
3+
"version": "3.0.0-alpha.4",
44
"description": "Nuxt+Vue Drupal Integration",
55
"license": "MIT",
66
"homepage": "https://github.com/liip/vuepal",

src/build/features/drupalRoute/index.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ export default defineVuepalFeature<{
4343

4444
helper.addGraphqlFile('fragment.metatag.graphql')
4545

46-
// Conditionally add the breadcrumb on the route fragment.
47-
const breadcrumbSpread = helper.hasFeatureEnabled('breadcrumb')
48-
? 'breadcrumb { ...breadcrumb }'
49-
: ''
50-
51-
// Conditionally add the languageSwitchLinks on the route fragment.
52-
const languageSwitchLinksSpread = helper.hasFeatureEnabled(
53-
'languageSwitchLinks',
54-
)
55-
? 'languageSwitchLinks { ...languageSwitchLink }'
56-
: ''
46+
const routeSpreadParts: string[] = ['metatags { ...metatag }']
47+
48+
if (helper.graphql.schemaHasType('SchemaMetatag')) {
49+
helper.addGraphqlFile('fragment.schemaMetatag.graphql')
50+
routeSpreadParts.push('schemaOrgMetatags { ...schemaMetatag }')
51+
}
52+
53+
if (helper.hasFeatureEnabled('breadcrumb')) {
54+
routeSpreadParts.push('breadcrumb { ...breadcrumb }')
55+
}
56+
57+
if (helper.hasFeatureEnabled('languageSwitchLinks')) {
58+
routeSpreadParts.push('languageSwitchLinks { ...languageSwitchLink }')
59+
}
60+
61+
const routeSpreads = routeSpreadParts.join('\n ')
5762

5863
helper.graphql.addDocument(
5964
'fragment.drupalRoute.graphql',
@@ -64,32 +69,19 @@ fragment useDrupalRoute on Query {
6469
path
6570
6671
... on InternalUrl {
67-
${breadcrumbSpread}
68-
${languageSwitchLinksSpread}
69-
70-
metatags {
71-
...metatag
72-
}
73-
7472
routeName
73+
${routeSpreads}
7574
}
7675
7776
... on EntityUrl {
78-
${breadcrumbSpread}
79-
${languageSwitchLinksSpread}
80-
81-
metatags {
82-
...metatag
83-
}
84-
77+
routeName
8578
drupalRouteEntity: entity {
8679
uuid
8780
entityBundle
8881
entityTypeId
8982
id
9083
}
91-
92-
routeName
84+
${routeSpreads}
9385
}
9486
9587
... on RedirectUrl {
Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
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

148
type 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

6148
export 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
}

src/runtime/composables/useDrupalRoute/index.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Link } from '@unhead/vue'
21
import type { HookResult } from '@nuxt/schema'
32
import type { RouteLocationRaw } from 'vue-router'
43
import type { UseDrupalRouteFragment } from '#graphql-operations'
@@ -15,12 +14,7 @@ import {
1514
type Ref,
1615
} from '#imports'
1716
import type { DrupalRoute, UseDrupalRoute } from './../../helpers/drupalRoute'
18-
19-
type DrupalRouteMetatags = {
20-
title: string
21-
link: Link[]
22-
meta: Link[]
23-
}
17+
import type { DrupalRouteMetatags } from '../../types/metatags'
2418

2519
type Options = {
2620
/**
@@ -91,13 +85,9 @@ export async function useDrupalRoute<T extends object = object>(
9185
}
9286
})
9387

94-
const metatags = computed<DrupalRouteMetatags>(() => {
95-
const route = query.value?.route
96-
if (route && 'metatags' in route) {
97-
return buildDrupalMetatags(route.metatags)
98-
}
99-
return { link: [], meta: [], title: '', schema: '' }
100-
})
88+
const metatags = computed<DrupalRouteMetatags>(() =>
89+
buildDrupalMetatags(query.value),
90+
)
10191

10292
useHead(metatags)
10393

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment schemaMetatag on SchemaMetatag {
2+
json
3+
}

src/runtime/types/metatags.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Link, Meta, Script } from '@unhead/vue'
2+
3+
export type DrupalRouteMetatags = {
4+
title: string
5+
link: Link[]
6+
meta: Meta[]
7+
script: Script[]
8+
schemaOrg: string
9+
}

0 commit comments

Comments
 (0)