Skip to content

Commit

Permalink
fix(schemaOrg): expose deduping config tagDuplicateStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 8, 2024
1 parent e864e18 commit c64539b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/schema-org/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function dedupeNodes(nodes: SchemaOrgNode[]) {
for (const key of nodes.keys()) {
const n = nodes[key]
const nodeKey = resolveAsGraphKey(n['@id'] || hash(n)) as Id
if (dedupedNodes[nodeKey])
if (dedupedNodes[nodeKey] && n._dedupeStrategy !== 'replace')
dedupedNodes[nodeKey] = merge(nodes[key], dedupedNodes[nodeKey]) as SchemaOrgNode
else
dedupedNodes[nodeKey] = nodes[key]
Expand Down
1 change: 1 addition & 0 deletions packages/schema-org/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function SchemaOrgUnheadPlugin(config: MetaInput, meta: () => Partial<Met
for (const node of Array.isArray(nodes) ? nodes : [nodes]) {
const newNode = {
...node,
_dedupeStrategy: tag.tagDuplicateStrategy,
_resolver: loadResolver(await node._resolver),
}
graph.push(newNode)
Expand Down
5 changes: 3 additions & 2 deletions packages/schema-org/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getActiveHead, useHead } from 'unhead'
import type { HeadEntryOptions } from '@unhead/schema'
import { UnheadSchemaOrg } from './plugin'
import type {
AggregateOffer,
Expand Down Expand Up @@ -159,7 +160,7 @@ export function defineBookEdition<T extends Record<string, any>>(input?: BookEdi

export type UseSchemaOrgInput = Arrayable<Thing | Record<string, any>>

export function useSchemaOrg(input: UseSchemaOrgInput) {
export function useSchemaOrg(input: UseSchemaOrgInput, options?: Pick<HeadEntryOptions, 'tagDuplicateStrategy'>) {
// lazy initialise the plugin
const head = getActiveHead()
if (!head)
Expand All @@ -173,5 +174,5 @@ export function useSchemaOrg(input: UseSchemaOrgInput) {
nodes: input,
},
],
})
}, options)
}
1 change: 1 addition & 0 deletions packages/schema-org/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export interface Thing {

export interface SchemaOrgNode extends Thing {
_resolver?: SchemaOrgNodeDefinition<any>
_dedupeStrategy?: 'replace' | 'merge'
}

export type WithResolver<T> = T & {
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/head.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hookable, NestedHooks } from 'hookable'
import type { HeadHooks } from './hooks'
import type { Head } from './schema'
import type { HeadTag, ProcessesTemplateParams, TagPosition, TagPriority, TemplateParams } from './tags'
import type { HeadTag, ProcessesTemplateParams, ResolvesDuplicates, TagPosition, TagPriority, TemplateParams } from './tags'

/**
* Side effects are mapped with a key and their cleanup function.
Expand Down Expand Up @@ -83,7 +83,7 @@ export interface CreateHeadOptions {
hooks?: NestedHooks<HeadHooks>
}

export interface HeadEntryOptions extends TagPosition, TagPriority, ProcessesTemplateParams {
export interface HeadEntryOptions extends TagPosition, TagPriority, ProcessesTemplateParams, ResolvesDuplicates {
mode?: RuntimeMode
transform?: (input: unknown) => unknown
head?: Unhead
Expand Down
2 changes: 1 addition & 1 deletion packages/unhead/src/plugins/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineHeadPlugin, HasElementTags, hashTag, tagDedupeKey, tagWeight} from '@unhead/shared'
import { defineHeadPlugin, HasElementTags, hashTag, tagDedupeKey, tagWeight } from '@unhead/shared'
import type { HeadTag } from '@unhead/schema'

const UsesMergeStrategy = new Set(['templateParams', 'htmlAttrs', 'bodyAttrs'])
Expand Down

0 comments on commit c64539b

Please sign in to comment.