|
1 | 1 | 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' |
37 | 4 |
|
38 | 5 | export function normaliseEntryTags<T extends {} = Head>(e: HeadEntry<T>) { |
39 | 6 | return Object.entries(e.input) |
40 | 7 | .filter(([k, v]) => typeof v !== 'undefined' && ValidHeadTags.includes(k)) |
41 | 8 | .map(([k, value]) => asArray(value) |
42 | 9 | // @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))), |
44 | 11 | ) |
45 | 12 | .flat(3) |
46 | 13 | .map((t, i) => { |
| 14 | + t._e = e._i |
47 | 15 | // support 256 tags per entry |
48 | 16 | t._p = (e._i << 8) + (i++) |
49 | 17 | return t |
|
0 commit comments