Skip to content

Commit

Permalink
fix: dedupe nodes before resolving root nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed May 26, 2023
1 parent b2305f8 commit da59e5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/unhead-schema-org/src/core/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Arrayable, Id, MetaInput, ResolvedMeta, SchemaOrgNode, Thing } fro
import { asArray, resolveAsGraphKey } from '../utils'
import { imageResolver } from '../nodes'
import { resolveMeta, resolveNode, resolveNodeId, resolveRelation } from './resolve'
import { dedupeNodes } from './util'
import { dedupeNodes, normaliseNodes } from './util'

export interface SchemaOrgGraph {
nodes: SchemaOrgNode[]
Expand Down Expand Up @@ -37,6 +37,7 @@ export function createSchemaOrgGraph(): SchemaOrgGraph {
}
ctx.nodes[key] = node
})
ctx.nodes = dedupeNodes(ctx.nodes)

ctx.nodes
.forEach((node) => {
Expand All @@ -53,7 +54,7 @@ export function createSchemaOrgGraph(): SchemaOrgGraph {
delete node._resolver
})

return dedupeNodes(ctx.nodes)
return normaliseNodes(ctx.nodes)
},
nodes: [],
meta: {} as ResolvedMeta,
Expand Down
14 changes: 14 additions & 0 deletions packages/unhead-schema-org/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ function groupBy<T>(array: T[], predicate: (value: T, index: number, array: T[])
* @param nodes
*/
export function dedupeNodes(nodes: SchemaOrgNode[]) {
// assign based on id to dedupe across context
const dedupedNodes: Record<Id, SchemaOrgNode> = {}
for (const key of nodes.keys()) {
const n = nodes[key]
const nodeKey = resolveAsGraphKey(n['@id'] || hash(n)) as Id
if (dedupedNodes[nodeKey])
dedupedNodes[nodeKey] = defu(nodes[key], dedupedNodes[nodeKey]) as SchemaOrgNode
else
dedupedNodes[nodeKey] = nodes[key]
}
return Object.values(dedupedNodes)
}

export function normaliseNodes(nodes: SchemaOrgNode[]) {
const sortedNodeKeys = nodes.keys()

// assign based on id to dedupe across context
Expand Down

0 comments on commit da59e5d

Please sign in to comment.