Skip to content

Commit

Permalink
fix(schema.org): support template params
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 1, 2023
1 parent 21f1473 commit 6b2a6ff
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
18 changes: 11 additions & 7 deletions packages/schema-org/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineHeadPlugin } from '@unhead/shared'
import { defineHeadPlugin, processTemplateParams } from '@unhead/shared'
import type { MetaInput, ResolvedMeta } from './types'
import {
createSchemaOrgGraph,
Expand Down Expand Up @@ -30,7 +30,7 @@ export function SchemaOrgUnheadPlugin(config: MetaInput, meta: () => Partial<Met
config = resolveMeta({ ...config })
let graph: SchemaOrgGraph
let resolvedMeta = {} as ResolvedMeta
return defineHeadPlugin({
return defineHeadPlugin(head => ({
key: 'schema-org',
hooks: {
'entries:resolve': function () {
Expand Down Expand Up @@ -80,15 +80,19 @@ export function SchemaOrgUnheadPlugin(config: MetaInput, meta: () => Partial<Met
for (const tag of ctx.tags) {
if (tag.tag === 'script' && tag.key === 'schema-org-graph') {
const minify = options?.minify || process.env.NODE_ENV === 'production'
tag.innerHTML = JSON.stringify({
'@context': 'https://schema.org',
'@graph': graph.resolveGraph({ ...config, ...resolvedMeta, ...(await meta?.() || {}) }),
}, null, minify ? 0 : 2)
tag.innerHTML = processTemplateParams(
JSON.stringify({
'@context': 'https://schema.org',
'@graph': graph.resolveGraph({ ...config, ...resolvedMeta, ...(await meta?.() || {}) }),
}, null, minify ? 0 : 2),
head._templateParams!,
head._separator!,
)
delete tag.props.nodes
return
}
}
},
},
})
}))
}
10 changes: 9 additions & 1 deletion packages/schema/src/head.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Hookable, NestedHooks } from 'hookable'
import type { HeadHooks } from './hooks'
import type { HeadTag, ProcessesTemplateParams, TagPosition, TagPriority } from './tags'
import type { HeadTag, ProcessesTemplateParams, TagPosition, TagPriority, TemplateParams } from './tags'
import type { Head } from './schema'

/**
Expand Down Expand Up @@ -139,6 +139,14 @@ export interface Unhead<Input extends {} = Head> {
* @internal
*/
_scripts?: Record<string, any>
/**
* @internal
*/
_templateParams?: TemplateParams
/**
* @internal
*/
_separator?: string
}

export interface DomState {
Expand Down
7 changes: 5 additions & 2 deletions packages/unhead/src/plugins/templateParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const SupportedAttrs = {
htmlAttrs: 'lang',
} as const

export default defineHeadPlugin({
export default defineHeadPlugin(head => ({
hooks: {
'tags:resolve': (ctx) => {
const { tags } = ctx
Expand Down Expand Up @@ -37,7 +37,10 @@ export default defineHeadPlugin({
})
}
}
// resolved template params
head._templateParams = params
head._separator = sep
ctx.tags = tags.filter(tag => tag.tag !== 'templateParams')
},
},
})
}))
9 changes: 9 additions & 0 deletions test/schema.org/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ describe('schema.org e2e', () => {
],
})

useHead({
templateParams: {
siteDescription: 'hello world',
},
})

useSchemaOrg([
defineWebPage({
name: 'test',
description: '%siteDescription',
}),
])

Expand All @@ -27,6 +34,7 @@ describe('schema.org e2e', () => {
{
\\"@id\\": \\"#webpage\\",
\\"@type\\": \\"WebPage\\",
\\"description\\": \\"hello world\\",
\\"name\\": \\"test\\"
}
]
Expand All @@ -52,6 +60,7 @@ describe('schema.org e2e', () => {
{
\\"@id\\": \\"#webpage\\",
\\"@type\\": \\"WebPage\\",
\\"description\\": \\"hello world\\",
\\"name\\": \\"test\\"
}
]
Expand Down

0 comments on commit 6b2a6ff

Please sign in to comment.