Skip to content

Commit

Permalink
feat: automatic root nodes with defaults plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 21, 2023
1 parent dda9243 commit ddedb9c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/runtime/nuxt/plugin/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Organization, Person } from '@unhead/schema-org'
import { defu } from 'defu'
import {
defineNuxtPlugin,
defineOrganization,
definePerson,
defineWebPage,
defineWebSite,
useSchemaOrg,
useSiteConfig,
useRuntimeConfig,
} from '#imports'

export default defineNuxtPlugin({
name: 'nuxt-schema-org:defaults',
setup() {
const runtimeConfig = useRuntimeConfig()['nuxt-schema-org'] || useRuntimeConfig().public['nuxt-schema-org']
// get the head instance
const siteConfig = useSiteConfig()

// init vendors
useSchemaOrg([
defineWebSite({
name: () => siteConfig?.name || '',
// TODO integrate with nuxt/i18n
inLanguage: () => siteConfig?.currentLocale || '',
description: () => siteConfig?.description || '',
}),
defineWebPage(),
])
if (runtimeConfig.identity || siteConfig.identity) {
const identity = runtimeConfig.identity || siteConfig.identity
let identityPayload: Person | Organization = {
name: siteConfig.name,
url: siteConfig.url,
}
let identityType = 'Organization'
if (typeof identity !== 'string') {
identityPayload = defu(identity, identityPayload)
identityType = identity.type
}
else {
identityType = identity
}
if (siteConfig.twitter) {
// without the @
const id = siteConfig.twitter.startsWith('@')
? siteConfig.twitter.slice(1)
: siteConfig.twitter
identityPayload.sameAs = [
`https://twitter.com/${id}`,
]
}
useSchemaOrg([
identityType === 'Person'
? definePerson(identityPayload)
: defineOrganization(identityPayload),
])
}
},
})

0 comments on commit ddedb9c

Please sign in to comment.