@unhead/shared 1.0.22 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +26 -68
- package/dist/index.d.ts +6 -10
- package/dist/index.mjs +25 -68
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -17,6 +17,7 @@ const HasElementTags = [
|
|
|
17
17
|
const ValidHeadTags = [
|
|
18
18
|
"title",
|
|
19
19
|
"titleTemplate",
|
|
20
|
+
"templateParams",
|
|
20
21
|
"base",
|
|
21
22
|
"htmlAttrs",
|
|
22
23
|
"bodyAttrs",
|
|
@@ -27,7 +28,29 @@ const ValidHeadTags = [
|
|
|
27
28
|
"noscript"
|
|
28
29
|
];
|
|
29
30
|
const UniqueTags = ["base", "title", "titleTemplate", "bodyAttrs", "htmlAttrs"];
|
|
30
|
-
const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy"];
|
|
31
|
+
const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy", "innerHTML", "textContent"];
|
|
32
|
+
|
|
33
|
+
function defineHeadPlugin(plugin) {
|
|
34
|
+
return plugin;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hashCode(s) {
|
|
38
|
+
let h = 9;
|
|
39
|
+
for (let i = 0; i < s.length; )
|
|
40
|
+
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
41
|
+
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
function hashTag(tag) {
|
|
44
|
+
return hashCode(`${tag.tag}:${tag.textContent || tag.innerHTML || ""}:${Object.entries(tag.props).map(([key, value]) => `${key}:${String(value)}`).join(",")}`);
|
|
45
|
+
}
|
|
46
|
+
function computeHashes(hashes) {
|
|
47
|
+
let h = 9;
|
|
48
|
+
for (const s of hashes) {
|
|
49
|
+
for (let i = 0; i < s.length; )
|
|
50
|
+
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
51
|
+
}
|
|
52
|
+
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
53
|
+
}
|
|
31
54
|
|
|
32
55
|
function tagDedupeKey(tag, fn) {
|
|
33
56
|
const { props, tag: tagName } = tag;
|
|
@@ -50,74 +73,7 @@ function tagDedupeKey(tag, fn) {
|
|
|
50
73
|
}
|
|
51
74
|
return false;
|
|
52
75
|
}
|
|
53
|
-
const DedupesTagsPlugin = (options) => {
|
|
54
|
-
options = options || {};
|
|
55
|
-
const dedupeKeys = options.dedupeKeys || ["hid", "vmid", "key"];
|
|
56
|
-
return defineHeadPlugin({
|
|
57
|
-
hooks: {
|
|
58
|
-
"tag:normalise": function({ tag }) {
|
|
59
|
-
dedupeKeys.forEach((key) => {
|
|
60
|
-
if (tag.props[key]) {
|
|
61
|
-
tag.key = tag.props[key];
|
|
62
|
-
delete tag.props[key];
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
const dedupe = tag.key ? `${tag.tag}:${tag.key}` : tagDedupeKey(tag);
|
|
66
|
-
if (dedupe)
|
|
67
|
-
tag._d = dedupe;
|
|
68
|
-
},
|
|
69
|
-
"tags:resolve": function(ctx) {
|
|
70
|
-
const deduping = {};
|
|
71
|
-
ctx.tags.forEach((tag) => {
|
|
72
|
-
let dedupeKey = tag._d || tag._p;
|
|
73
|
-
const dupedTag = deduping[dedupeKey];
|
|
74
|
-
if (dupedTag) {
|
|
75
|
-
let strategy = tag?.tagDuplicateStrategy;
|
|
76
|
-
if (!strategy && (tag.tag === "htmlAttrs" || tag.tag === "bodyAttrs"))
|
|
77
|
-
strategy = "merge";
|
|
78
|
-
if (strategy === "merge") {
|
|
79
|
-
const oldProps = dupedTag.props;
|
|
80
|
-
["class", "style"].forEach((key) => {
|
|
81
|
-
if (tag.props[key] && oldProps[key]) {
|
|
82
|
-
if (key === "style" && !oldProps[key].endsWith(";"))
|
|
83
|
-
oldProps[key] += ";";
|
|
84
|
-
tag.props[key] = `${oldProps[key]} ${tag.props[key]}`;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
deduping[dedupeKey].props = {
|
|
88
|
-
...oldProps,
|
|
89
|
-
...tag.props
|
|
90
|
-
};
|
|
91
|
-
return;
|
|
92
|
-
} else if (tag._e === dupedTag._e) {
|
|
93
|
-
dedupeKey = tag._d = `${dedupeKey}:${tag._p}`;
|
|
94
|
-
}
|
|
95
|
-
const propCount = Object.keys(tag.props).length;
|
|
96
|
-
if ((propCount === 0 || propCount === 1 && typeof tag.props["data-h-key"] !== "undefined") && !tag.children) {
|
|
97
|
-
delete deduping[dedupeKey];
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
deduping[dedupeKey] = tag;
|
|
102
|
-
});
|
|
103
|
-
ctx.tags = Object.values(deduping);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
function defineHeadPlugin(plugin) {
|
|
110
|
-
return plugin;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function hashCode(s) {
|
|
114
|
-
let h = 9;
|
|
115
|
-
for (let i = 0; i < s.length; )
|
|
116
|
-
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
117
|
-
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
118
|
-
}
|
|
119
76
|
|
|
120
|
-
exports.DedupesTagsPlugin = DedupesTagsPlugin;
|
|
121
77
|
exports.HasElementTags = HasElementTags;
|
|
122
78
|
exports.SelfClosingTags = SelfClosingTags;
|
|
123
79
|
exports.TagConfigKeys = TagConfigKeys;
|
|
@@ -125,6 +81,8 @@ exports.TagsWithInnerContent = TagsWithInnerContent;
|
|
|
125
81
|
exports.UniqueTags = UniqueTags;
|
|
126
82
|
exports.ValidHeadTags = ValidHeadTags;
|
|
127
83
|
exports.asArray = asArray;
|
|
84
|
+
exports.computeHashes = computeHashes;
|
|
128
85
|
exports.defineHeadPlugin = defineHeadPlugin;
|
|
129
86
|
exports.hashCode = hashCode;
|
|
87
|
+
exports.hashTag = hashTag;
|
|
130
88
|
exports.tagDedupeKey = tagDedupeKey;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { HeadPlugin } from '@unhead/schema';
|
|
3
|
-
import { HeadTag } from '@unhead/schema/dist';
|
|
1
|
+
import { HeadPlugin, HeadTag } from '@unhead/schema';
|
|
4
2
|
|
|
5
3
|
type Arrayable<T> = T | Array<T>;
|
|
6
4
|
declare function asArray<T>(value: Arrayable<T>): T[];
|
|
@@ -12,14 +10,12 @@ declare const ValidHeadTags: string[];
|
|
|
12
10
|
declare const UniqueTags: string[];
|
|
13
11
|
declare const TagConfigKeys: string[];
|
|
14
12
|
|
|
15
|
-
interface DedupesTagsPluginOptions {
|
|
16
|
-
dedupeKeys?: string[];
|
|
17
|
-
}
|
|
18
|
-
declare function tagDedupeKey<T extends HeadTag>(tag: T, fn?: (key: string) => boolean): string | false;
|
|
19
|
-
declare const DedupesTagsPlugin: (options?: DedupesTagsPluginOptions) => _unhead_schema.HeadPlugin;
|
|
20
|
-
|
|
21
13
|
declare function defineHeadPlugin(plugin: HeadPlugin): HeadPlugin;
|
|
22
14
|
|
|
23
15
|
declare function hashCode(s: string): string;
|
|
16
|
+
declare function hashTag(tag: HeadTag): string;
|
|
17
|
+
declare function computeHashes(hashes: string[]): string;
|
|
18
|
+
|
|
19
|
+
declare function tagDedupeKey<T extends HeadTag>(tag: T, fn?: (key: string) => boolean): string | false;
|
|
24
20
|
|
|
25
|
-
export { Arrayable,
|
|
21
|
+
export { Arrayable, HasElementTags, SelfClosingTags, TagConfigKeys, TagsWithInnerContent, UniqueTags, ValidHeadTags, asArray, computeHashes, defineHeadPlugin, hashCode, hashTag, tagDedupeKey };
|
package/dist/index.mjs
CHANGED
|
@@ -15,6 +15,7 @@ const HasElementTags = [
|
|
|
15
15
|
const ValidHeadTags = [
|
|
16
16
|
"title",
|
|
17
17
|
"titleTemplate",
|
|
18
|
+
"templateParams",
|
|
18
19
|
"base",
|
|
19
20
|
"htmlAttrs",
|
|
20
21
|
"bodyAttrs",
|
|
@@ -25,7 +26,29 @@ const ValidHeadTags = [
|
|
|
25
26
|
"noscript"
|
|
26
27
|
];
|
|
27
28
|
const UniqueTags = ["base", "title", "titleTemplate", "bodyAttrs", "htmlAttrs"];
|
|
28
|
-
const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy"];
|
|
29
|
+
const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy", "innerHTML", "textContent"];
|
|
30
|
+
|
|
31
|
+
function defineHeadPlugin(plugin) {
|
|
32
|
+
return plugin;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hashCode(s) {
|
|
36
|
+
let h = 9;
|
|
37
|
+
for (let i = 0; i < s.length; )
|
|
38
|
+
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
39
|
+
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
40
|
+
}
|
|
41
|
+
function hashTag(tag) {
|
|
42
|
+
return hashCode(`${tag.tag}:${tag.textContent || tag.innerHTML || ""}:${Object.entries(tag.props).map(([key, value]) => `${key}:${String(value)}`).join(",")}`);
|
|
43
|
+
}
|
|
44
|
+
function computeHashes(hashes) {
|
|
45
|
+
let h = 9;
|
|
46
|
+
for (const s of hashes) {
|
|
47
|
+
for (let i = 0; i < s.length; )
|
|
48
|
+
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
49
|
+
}
|
|
50
|
+
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
51
|
+
}
|
|
29
52
|
|
|
30
53
|
function tagDedupeKey(tag, fn) {
|
|
31
54
|
const { props, tag: tagName } = tag;
|
|
@@ -48,71 +71,5 @@ function tagDedupeKey(tag, fn) {
|
|
|
48
71
|
}
|
|
49
72
|
return false;
|
|
50
73
|
}
|
|
51
|
-
const DedupesTagsPlugin = (options) => {
|
|
52
|
-
options = options || {};
|
|
53
|
-
const dedupeKeys = options.dedupeKeys || ["hid", "vmid", "key"];
|
|
54
|
-
return defineHeadPlugin({
|
|
55
|
-
hooks: {
|
|
56
|
-
"tag:normalise": function({ tag }) {
|
|
57
|
-
dedupeKeys.forEach((key) => {
|
|
58
|
-
if (tag.props[key]) {
|
|
59
|
-
tag.key = tag.props[key];
|
|
60
|
-
delete tag.props[key];
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
const dedupe = tag.key ? `${tag.tag}:${tag.key}` : tagDedupeKey(tag);
|
|
64
|
-
if (dedupe)
|
|
65
|
-
tag._d = dedupe;
|
|
66
|
-
},
|
|
67
|
-
"tags:resolve": function(ctx) {
|
|
68
|
-
const deduping = {};
|
|
69
|
-
ctx.tags.forEach((tag) => {
|
|
70
|
-
let dedupeKey = tag._d || tag._p;
|
|
71
|
-
const dupedTag = deduping[dedupeKey];
|
|
72
|
-
if (dupedTag) {
|
|
73
|
-
let strategy = tag?.tagDuplicateStrategy;
|
|
74
|
-
if (!strategy && (tag.tag === "htmlAttrs" || tag.tag === "bodyAttrs"))
|
|
75
|
-
strategy = "merge";
|
|
76
|
-
if (strategy === "merge") {
|
|
77
|
-
const oldProps = dupedTag.props;
|
|
78
|
-
["class", "style"].forEach((key) => {
|
|
79
|
-
if (tag.props[key] && oldProps[key]) {
|
|
80
|
-
if (key === "style" && !oldProps[key].endsWith(";"))
|
|
81
|
-
oldProps[key] += ";";
|
|
82
|
-
tag.props[key] = `${oldProps[key]} ${tag.props[key]}`;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
deduping[dedupeKey].props = {
|
|
86
|
-
...oldProps,
|
|
87
|
-
...tag.props
|
|
88
|
-
};
|
|
89
|
-
return;
|
|
90
|
-
} else if (tag._e === dupedTag._e) {
|
|
91
|
-
dedupeKey = tag._d = `${dedupeKey}:${tag._p}`;
|
|
92
|
-
}
|
|
93
|
-
const propCount = Object.keys(tag.props).length;
|
|
94
|
-
if ((propCount === 0 || propCount === 1 && typeof tag.props["data-h-key"] !== "undefined") && !tag.children) {
|
|
95
|
-
delete deduping[dedupeKey];
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
deduping[dedupeKey] = tag;
|
|
100
|
-
});
|
|
101
|
-
ctx.tags = Object.values(deduping);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
function defineHeadPlugin(plugin) {
|
|
108
|
-
return plugin;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function hashCode(s) {
|
|
112
|
-
let h = 9;
|
|
113
|
-
for (let i = 0; i < s.length; )
|
|
114
|
-
h = Math.imul(h ^ s.charCodeAt(i++), 9 ** 9);
|
|
115
|
-
return ((h ^ h >>> 9) + 65536).toString(16).substring(1, 8).toLowerCase();
|
|
116
|
-
}
|
|
117
74
|
|
|
118
|
-
export {
|
|
75
|
+
export { HasElementTags, SelfClosingTags, TagConfigKeys, TagsWithInnerContent, UniqueTags, ValidHeadTags, asArray, computeHashes, defineHeadPlugin, hashCode, hashTag, tagDedupeKey };
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unhead/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"packageManager": "pnpm@7.
|
|
4
|
+
"version": "1.1.1",
|
|
5
|
+
"packageManager": "pnpm@7.27.1",
|
|
6
6
|
"author": "Harlan Wilton <harlan@harlanzw.com>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"funding": "https://github.com/sponsors/harlan-zw",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@unhead/schema": "1.
|
|
38
|
+
"@unhead/schema": "1.1.1"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "unbuild .",
|