@unhead/addons 1.1.17 → 1.1.20
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 +38 -38
- package/dist/index.d.ts +2 -8
- package/dist/index.mjs +39 -39
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -2,60 +2,60 @@
|
|
|
2
2
|
|
|
3
3
|
const shared = require('@unhead/shared');
|
|
4
4
|
|
|
5
|
-
const InferSeoMetaPlugin = (options) => {
|
|
6
|
-
options = options || {};
|
|
7
|
-
const ogTitleTemplate = options.ogTitle || "%s";
|
|
8
|
-
const ogDescriptionTemplate = options.ogDescription || "%s";
|
|
5
|
+
const InferSeoMetaPlugin = (options = {}) => {
|
|
9
6
|
return shared.defineHeadPlugin({
|
|
10
7
|
hooks: {
|
|
11
8
|
entries: {
|
|
12
9
|
resolve({ entries }) {
|
|
13
|
-
let
|
|
10
|
+
let titleTemplate = null;
|
|
11
|
+
for (const entry of entries) {
|
|
12
|
+
const inputKey = entry.resolvedInput ? "resolvedInput" : "input";
|
|
13
|
+
const input = entry[inputKey];
|
|
14
|
+
if (typeof input.titleTemplate !== "undefined")
|
|
15
|
+
titleTemplate = input.titleTemplate;
|
|
16
|
+
}
|
|
14
17
|
for (const entry of entries) {
|
|
15
18
|
const inputKey = entry.resolvedInput ? "resolvedInput" : "input";
|
|
16
19
|
const input = entry[inputKey];
|
|
17
20
|
const resolvedMeta = input.meta || [];
|
|
21
|
+
titleTemplate = shared.resolveTitleTemplate(titleTemplate, input.title);
|
|
18
22
|
const title = input.title;
|
|
19
|
-
const ogTitle = resolvedMeta.find((meta) => meta.property === "og:title");
|
|
20
|
-
hasOgImage = hasOgImage || !!resolvedMeta.find((meta) => meta.property === "og:image");
|
|
21
23
|
const description = resolvedMeta.find((meta) => meta.name === "description")?.content;
|
|
22
|
-
const
|
|
24
|
+
const hasOgTitle = !!resolvedMeta.find((meta) => meta.property === "og:title");
|
|
25
|
+
const hasOgImage = !!resolvedMeta.find((meta) => meta.property === "og:image");
|
|
26
|
+
const hasTwitterCard = !!resolvedMeta.find((meta) => meta.property === "twitter:card");
|
|
27
|
+
const hasOgDescription = !!resolvedMeta.find((meta) => meta.property === "og:description");
|
|
23
28
|
entry[inputKey].meta = input.meta || [];
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
if (titleTemplate && !hasOgTitle) {
|
|
30
|
+
let newOgTitle = options?.ogTitle || titleTemplate;
|
|
31
|
+
if (typeof newOgTitle === "function")
|
|
32
|
+
newOgTitle = newOgTitle(title);
|
|
33
|
+
if (newOgTitle) {
|
|
34
|
+
entry[inputKey].meta.push({
|
|
35
|
+
property: "og:title",
|
|
36
|
+
// have the og:title be removed if we don't have a title
|
|
37
|
+
content: String(newOgTitle)
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (description && !hasOgDescription) {
|
|
42
|
+
let newOgDescription = options?.ogDescription || description;
|
|
43
|
+
if (typeof newOgDescription === "function")
|
|
44
|
+
newOgDescription = newOgDescription(title);
|
|
45
|
+
if (newOgDescription) {
|
|
46
|
+
entry[inputKey].meta.push({
|
|
47
|
+
property: "og:description",
|
|
48
|
+
content: String(newOgDescription)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
29
51
|
}
|
|
30
|
-
if (
|
|
31
|
-
const desc = String(description);
|
|
52
|
+
if (hasOgImage && !hasTwitterCard) {
|
|
32
53
|
entry[inputKey].meta.push({
|
|
33
|
-
|
|
34
|
-
content:
|
|
54
|
+
name: "twitter:card",
|
|
55
|
+
content: options?.twitterCard || "summary_large_image"
|
|
35
56
|
});
|
|
36
57
|
}
|
|
37
58
|
}
|
|
38
|
-
const metas = [];
|
|
39
|
-
if (options?.robots !== false) {
|
|
40
|
-
metas.push({
|
|
41
|
-
name: "robots",
|
|
42
|
-
content: options?.robots || "max-snippet: -1; max-image-preview: large; max-video-preview: -1"
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
if (hasOgImage && options?.twitterCard !== false) {
|
|
46
|
-
metas.push({
|
|
47
|
-
property: "twitter:card",
|
|
48
|
-
content: options?.twitterCard || "summary_large_image"
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
const rootEntry = {
|
|
52
|
-
_i: -1,
|
|
53
|
-
_sde: {},
|
|
54
|
-
input: {
|
|
55
|
-
meta: metas
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
entries.unshift(rootEntry);
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,19 +6,13 @@ interface InferSeoMetaPluginOptions {
|
|
|
6
6
|
*
|
|
7
7
|
* @param title
|
|
8
8
|
*/
|
|
9
|
-
ogTitle?:
|
|
9
|
+
ogTitle?: ((title: string) => string);
|
|
10
10
|
/**
|
|
11
11
|
* Transform the og description.
|
|
12
12
|
*
|
|
13
13
|
* @param title
|
|
14
14
|
*/
|
|
15
|
-
ogDescription?:
|
|
16
|
-
/**
|
|
17
|
-
* Whether robot meta should be infered.
|
|
18
|
-
*
|
|
19
|
-
* @default true
|
|
20
|
-
*/
|
|
21
|
-
robots?: false | string;
|
|
15
|
+
ogDescription?: ((description: string) => string);
|
|
22
16
|
/**
|
|
23
17
|
* The twitter card to use.
|
|
24
18
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import { defineHeadPlugin } from '@unhead/shared';
|
|
1
|
+
import { defineHeadPlugin, resolveTitleTemplate } from '@unhead/shared';
|
|
2
2
|
|
|
3
|
-
const InferSeoMetaPlugin = (options) => {
|
|
4
|
-
options = options || {};
|
|
5
|
-
const ogTitleTemplate = options.ogTitle || "%s";
|
|
6
|
-
const ogDescriptionTemplate = options.ogDescription || "%s";
|
|
3
|
+
const InferSeoMetaPlugin = (options = {}) => {
|
|
7
4
|
return defineHeadPlugin({
|
|
8
5
|
hooks: {
|
|
9
6
|
entries: {
|
|
10
7
|
resolve({ entries }) {
|
|
11
|
-
let
|
|
8
|
+
let titleTemplate = null;
|
|
9
|
+
for (const entry of entries) {
|
|
10
|
+
const inputKey = entry.resolvedInput ? "resolvedInput" : "input";
|
|
11
|
+
const input = entry[inputKey];
|
|
12
|
+
if (typeof input.titleTemplate !== "undefined")
|
|
13
|
+
titleTemplate = input.titleTemplate;
|
|
14
|
+
}
|
|
12
15
|
for (const entry of entries) {
|
|
13
16
|
const inputKey = entry.resolvedInput ? "resolvedInput" : "input";
|
|
14
17
|
const input = entry[inputKey];
|
|
15
18
|
const resolvedMeta = input.meta || [];
|
|
19
|
+
titleTemplate = resolveTitleTemplate(titleTemplate, input.title);
|
|
16
20
|
const title = input.title;
|
|
17
|
-
const ogTitle = resolvedMeta.find((meta) => meta.property === "og:title");
|
|
18
|
-
hasOgImage = hasOgImage || !!resolvedMeta.find((meta) => meta.property === "og:image");
|
|
19
21
|
const description = resolvedMeta.find((meta) => meta.name === "description")?.content;
|
|
20
|
-
const
|
|
22
|
+
const hasOgTitle = !!resolvedMeta.find((meta) => meta.property === "og:title");
|
|
23
|
+
const hasOgImage = !!resolvedMeta.find((meta) => meta.property === "og:image");
|
|
24
|
+
const hasTwitterCard = !!resolvedMeta.find((meta) => meta.property === "twitter:card");
|
|
25
|
+
const hasOgDescription = !!resolvedMeta.find((meta) => meta.property === "og:description");
|
|
21
26
|
entry[inputKey].meta = input.meta || [];
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
if (titleTemplate && !hasOgTitle) {
|
|
28
|
+
let newOgTitle = options?.ogTitle || titleTemplate;
|
|
29
|
+
if (typeof newOgTitle === "function")
|
|
30
|
+
newOgTitle = newOgTitle(title);
|
|
31
|
+
if (newOgTitle) {
|
|
32
|
+
entry[inputKey].meta.push({
|
|
33
|
+
property: "og:title",
|
|
34
|
+
// have the og:title be removed if we don't have a title
|
|
35
|
+
content: String(newOgTitle)
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (description && !hasOgDescription) {
|
|
40
|
+
let newOgDescription = options?.ogDescription || description;
|
|
41
|
+
if (typeof newOgDescription === "function")
|
|
42
|
+
newOgDescription = newOgDescription(title);
|
|
43
|
+
if (newOgDescription) {
|
|
44
|
+
entry[inputKey].meta.push({
|
|
45
|
+
property: "og:description",
|
|
46
|
+
content: String(newOgDescription)
|
|
47
|
+
});
|
|
48
|
+
}
|
|
27
49
|
}
|
|
28
|
-
if (
|
|
29
|
-
const desc = String(description);
|
|
50
|
+
if (hasOgImage && !hasTwitterCard) {
|
|
30
51
|
entry[inputKey].meta.push({
|
|
31
|
-
|
|
32
|
-
content:
|
|
52
|
+
name: "twitter:card",
|
|
53
|
+
content: options?.twitterCard || "summary_large_image"
|
|
33
54
|
});
|
|
34
55
|
}
|
|
35
56
|
}
|
|
36
|
-
const metas = [];
|
|
37
|
-
if (options?.robots !== false) {
|
|
38
|
-
metas.push({
|
|
39
|
-
name: "robots",
|
|
40
|
-
content: options?.robots || "max-snippet: -1; max-image-preview: large; max-video-preview: -1"
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
if (hasOgImage && options?.twitterCard !== false) {
|
|
44
|
-
metas.push({
|
|
45
|
-
property: "twitter:card",
|
|
46
|
-
content: options?.twitterCard || "summary_large_image"
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
const rootEntry = {
|
|
50
|
-
_i: -1,
|
|
51
|
-
_sde: {},
|
|
52
|
-
input: {
|
|
53
|
-
meta: metas
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
entries.unshift(rootEntry);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unhead/addons",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.20",
|
|
5
5
|
"packageManager": "pnpm@7.28.0",
|
|
6
6
|
"author": "Harlan Wilton <harlan@harlanzw.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -56,9 +56,9 @@
|
|
|
56
56
|
"ufo": "^1.1.1",
|
|
57
57
|
"unplugin": "^1.1.0",
|
|
58
58
|
"unplugin-ast": "^0.7.0",
|
|
59
|
-
"@unhead/schema": "1.1.
|
|
60
|
-
"@unhead/shared": "1.1.
|
|
61
|
-
"unhead": "1.1.
|
|
59
|
+
"@unhead/schema": "1.1.20",
|
|
60
|
+
"@unhead/shared": "1.1.20",
|
|
61
|
+
"unhead": "1.1.20"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@babel/types": "^7.21.2",
|