Skip to content

Commit 9e98ba1

Browse files
committed
fix: tagPriority number sorting
1 parent 8d25ba4 commit 9e98ba1

File tree

9 files changed

+411
-174
lines changed

9 files changed

+411
-174
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
"lint": "eslint \"{packages,test}/**/*.{ts,vue,json,yml}\" --fix"
4545
},
4646
"devDependencies": {
47-
"@antfu/eslint-config": "^0.29.3",
47+
"@antfu/eslint-config": "^0.29.4",
4848
"@types/fs-extra": "^9.0.13",
49-
"@types/jsdom": "^20.0.0",
50-
"@vitest/ui": "^0.24.5",
49+
"@types/jsdom": "^20.0.1",
50+
"@vitest/ui": "^0.25.0",
5151
"@vue/server-renderer": "^3.2.41",
5252
"bumpp": "^8.2.1",
5353
"eslint": "^8.27.0",
@@ -56,7 +56,7 @@
5656
"typescript": "^4.8.4",
5757
"unbuild": "^0.9.4",
5858
"utility-types": "^3.10.0",
59-
"vitest": "^0.24.5",
59+
"vitest": "^0.25.0",
6060
"vue": "^3.2.41"
6161
},
6262
"dependencies": {

packages/dom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
"@unhead/schema": "workspace:*"
3939
},
4040
"devDependencies": {
41-
"zhead": "1.0.0-beta.4"
41+
"zhead": "1.0.0-beta.5"
4242
}
4343
}

packages/ssr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
"@unhead/schema": "workspace:*"
3939
},
4040
"devDependencies": {
41-
"zhead": "1.0.0-beta.4"
41+
"zhead": "1.0.0-beta.5"
4242
}
4343
}

packages/unhead/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@
3939
"hookable": "^5.4.1"
4040
},
4141
"devDependencies": {
42-
"zhead": "1.0.0-beta.4"
42+
"zhead": "1.0.0-beta.5"
4343
}
4444
}

packages/unhead/src/normalise.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,17 @@
11
import type { Head, HeadEntry, HeadTag } from '@unhead/schema'
2-
import { ValidHeadTags, normaliseTag as normaliseTagBase } from 'zhead'
3-
import { TagConfigKeys, asArray } from './util'
4-
5-
export function normaliseTag<T>(tagName: HeadTag['tag'], input: HeadTag['props'], entry: HeadEntry<T>): HeadTag | HeadTag[] {
6-
const tag = normaliseTagBase(tagName, input, { childrenKeys: ['innerHTML', 'textContent'] }) as HeadTag
7-
tag._e = entry._i
8-
9-
// keys with direct mapping
10-
Object.keys(tag.props)
11-
.filter(k => TagConfigKeys.includes(k))
12-
.forEach((k) => {
13-
// @ts-expect-error untyped
14-
tag[k] = tag.props[k]
15-
delete tag.props[k]
16-
})
17-
// class object boolean support
18-
if (typeof tag.props.class === 'object' && !Array.isArray(tag.props.class)) {
19-
tag.props.class = Object.keys(tag.props.class)
20-
.filter(k => tag.props.class[k])
21-
}
22-
// class array support
23-
if (Array.isArray(tag.props.class))
24-
tag.props.class = tag.props.class.join(' ')
25-
26-
// allow meta to be resolved into multiple tags if an array is provided on content
27-
if (tag.props.content && Array.isArray(tag.props.content)) {
28-
return tag.props.content.map((v, i) => {
29-
const newTag = { ...tag, props: { ...tag.props } }
30-
newTag.props.content = v
31-
newTag.key = `${tag.props.name || tag.props.property}:${i}`
32-
return newTag
33-
})
34-
}
35-
return tag
36-
}
2+
import { ValidHeadTags, normaliseTag } from 'zhead'
3+
import { asArray } from './util'
374

385
export function normaliseEntryTags<T extends {} = Head>(e: HeadEntry<T>) {
396
return Object.entries(e.input)
407
.filter(([k, v]) => typeof v !== 'undefined' && ValidHeadTags.includes(k))
418
.map(([k, value]) => asArray(value)
429
// @ts-expect-error untyped
43-
.map(props => asArray(normaliseTag(k as HeadTag['tag'], props, e))),
10+
.map(props => asArray(normaliseTag<HeadTag>(k as HeadTag['tag'], props, e))),
4411
)
4512
.flat(3)
4613
.map((t, i) => {
14+
t._e = e._i
4715
// support 256 tags per entry
4816
t._p = (e._i << 8) + (i++)
4917
return t

packages/unhead/src/plugin/sortTagsPlugin.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { sortCriticalTags } from 'zhead'
1+
import { sortTags } from 'zhead'
22
import { defineHeadPlugin } from '..'
33

44
export const SortTagsPlugin = () => {
55
return defineHeadPlugin({
66
hooks: {
77
'tags:resolve': (ctx) => {
88
const tagIndexForKey = (key: string) => ctx.tags.find(tag => tag._d === key)?._p
9+
910
// 2a. Sort based on priority
1011
// now we need to check render priority for each before: rule and use the dedupe key index
1112
for (const tag of ctx.tags) {
12-
if (!tag?.tagPriority)
13-
continue
14-
15-
if (typeof tag.tagPriority === 'number') {
16-
tag._p = tag.tagPriority
13+
if (!tag.tagPriority || typeof tag.tagPriority === 'number')
1714
continue
18-
}
19-
20-
const modifiers = [{ prefix: 'before:', offset: -1 }, { prefix: 'after:', offset: 1 }]
21-
for (const { prefix, offset } of modifiers) {
15+
const modifiers = [{prefix: 'before:', offset: -1}, {prefix: 'after:', offset: 1}]
16+
for (const {prefix, offset} of modifiers) {
2217
if (tag.tagPriority.startsWith(prefix)) {
2318
const key = tag.tagPriority.replace(prefix, '')
2419
const index = tagIndexForKey(key)
@@ -32,7 +27,7 @@ export const SortTagsPlugin = () => {
3227
// 2b. sort tags in their natural order
3328
.sort((a, b) => a._p! - b._p!)
3429
// 2c. sort based on critical tags
35-
.sort(sortCriticalTags)
30+
.sort(sortTags)
3631
},
3732
},
3833
})

packages/unhead/src/util.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ export type Arrayable<T> = T | Array<T>
33
export function asArray<T>(value: Arrayable<T>): T[] {
44
return Array.isArray(value) ? value : [value]
55
}
6-
7-
export const TagConfigKeys = ['tagPosition', 'tagPriority', 'tagDuplicateStrategy']

0 commit comments

Comments
 (0)