@websolutespa/payload-plugin-seo 0.0.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/CHANGELOG.md +7 -0
- package/README.md +21 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +520 -0
- package/package.json +64 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @websolutespa/payload-plugin-seo
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40websolutespa%2Fpayload-plugin-seo)
|
|
4
|
+
|
|
5
|
+
[](https://shields.io/)
|
|
6
|
+
|
|
7
|
+
SEO plugin for PayloadCms.
|
|
8
|
+
|
|
9
|
+
# Payload SEO Plugin
|
|
10
|
+
|
|
11
|
+
### Requirements:
|
|
12
|
+
|
|
13
|
+
- Payload version 2.11.2 or higher is required.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @websolutespa/payload-plugin-seo
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
##### _this library is for internal usage and not production ready_
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Resource } from 'i18next';
|
|
2
|
+
import { Locale, Access, Plugin } from 'payload/config';
|
|
3
|
+
import { TextField, NumberField, EmailField, TextareaField, CheckboxField, DateField, BlockField, RadioField, RelationshipField, ArrayField, RichTextField, SelectField, UploadField, CodeField, PointField, PayloadRequest, CollectionConfig } from 'payload/types';
|
|
4
|
+
import { JSONField } from 'payload/dist/fields/config/types';
|
|
5
|
+
|
|
6
|
+
type DataField = JSONField | TextField | NumberField | EmailField | TextareaField | CheckboxField | DateField | BlockField | RadioField | RelationshipField | ArrayField | RichTextField | SelectField | UploadField | CodeField | PointField;
|
|
7
|
+
|
|
8
|
+
type SeoInitOptions = {
|
|
9
|
+
collections?: string[];
|
|
10
|
+
additionalFields?: DataField[];
|
|
11
|
+
customTokens?: SeoToken[];
|
|
12
|
+
qsMetatagsRules?: string;
|
|
13
|
+
metatagsRulesAccess?: MetatagsRuleAccess;
|
|
14
|
+
};
|
|
15
|
+
type SeoSlug = {
|
|
16
|
+
metatagsRule: string;
|
|
17
|
+
[key: string]: string;
|
|
18
|
+
};
|
|
19
|
+
type SeoGroup = {
|
|
20
|
+
seo: string;
|
|
21
|
+
[key: string]: string;
|
|
22
|
+
};
|
|
23
|
+
type SeoToken = {
|
|
24
|
+
name: string;
|
|
25
|
+
replacementFunction: (req: PayloadRequest, collection: CollectionConfig, doc: {
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}, locale: Locale | null) => Promise<string> | string;
|
|
28
|
+
};
|
|
29
|
+
type MetatagsRuleAccess = {
|
|
30
|
+
create?: Access;
|
|
31
|
+
read?: Access;
|
|
32
|
+
readVersions?: Access;
|
|
33
|
+
update?: Access;
|
|
34
|
+
delete?: Access;
|
|
35
|
+
admin?: (args?: any) => boolean | Promise<boolean>;
|
|
36
|
+
unlock?: Access;
|
|
37
|
+
};
|
|
38
|
+
type SeoOptions = {
|
|
39
|
+
collections: string[];
|
|
40
|
+
customTokens: SeoToken[];
|
|
41
|
+
additionalFields: DataField[];
|
|
42
|
+
qsMetatagsRules: string;
|
|
43
|
+
defaultLocale: string;
|
|
44
|
+
defaultMarket: string;
|
|
45
|
+
group: SeoGroup;
|
|
46
|
+
locales: string[] | Locale[];
|
|
47
|
+
slug: SeoSlug;
|
|
48
|
+
translations: Resource;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
declare const seo: (sourceOptions: SeoInitOptions) => Plugin;
|
|
52
|
+
|
|
53
|
+
export { MetatagsRuleAccess, SeoGroup, SeoInitOptions, SeoOptions, SeoSlug, SeoToken, seo as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
default: () => src_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
|
|
38
|
+
// src/api/tokens.service.ts
|
|
39
|
+
var import_payload = __toESM(require("payload"));
|
|
40
|
+
|
|
41
|
+
// src/translations.ts
|
|
42
|
+
var translations = {
|
|
43
|
+
en: {
|
|
44
|
+
group: {
|
|
45
|
+
cronJob: "Tasks"
|
|
46
|
+
},
|
|
47
|
+
collection: {
|
|
48
|
+
singular: {
|
|
49
|
+
cronJob: "Job",
|
|
50
|
+
cronLog: "Log"
|
|
51
|
+
},
|
|
52
|
+
plural: {
|
|
53
|
+
cronJob: "Jobs",
|
|
54
|
+
cronLog: "Logs"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
global: {},
|
|
58
|
+
field: {
|
|
59
|
+
cron: "Cron",
|
|
60
|
+
duration: "Duration",
|
|
61
|
+
job: "Job",
|
|
62
|
+
message: "Messagge",
|
|
63
|
+
status: "Status",
|
|
64
|
+
startedAt: "Started At",
|
|
65
|
+
completedAt: "Completed At",
|
|
66
|
+
executedAt: "Last Execution"
|
|
67
|
+
},
|
|
68
|
+
cronJob: {
|
|
69
|
+
runNow: "Run now"
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
it: {
|
|
73
|
+
group: {
|
|
74
|
+
cronJob: "Schedulazione"
|
|
75
|
+
},
|
|
76
|
+
collection: {
|
|
77
|
+
singular: {
|
|
78
|
+
cronJob: "Attivit\xE0",
|
|
79
|
+
cronLog: "Registro"
|
|
80
|
+
},
|
|
81
|
+
plural: {
|
|
82
|
+
cronJob: "Attivit\xE0",
|
|
83
|
+
cronLog: "Registro"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
global: {},
|
|
87
|
+
field: {
|
|
88
|
+
cron: "Cron",
|
|
89
|
+
duration: "Durata",
|
|
90
|
+
job: "Attivit\xE0",
|
|
91
|
+
message: "Messaggio",
|
|
92
|
+
status: "Stato",
|
|
93
|
+
startedAt: "Iniziato il",
|
|
94
|
+
completedAt: "Completato il",
|
|
95
|
+
executedAt: "Ultima esecuzione"
|
|
96
|
+
},
|
|
97
|
+
cronJob: {
|
|
98
|
+
runNow: "Esegui ora"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/options.ts
|
|
104
|
+
var defaultSlug = {
|
|
105
|
+
metatagsRule: "metatagsRule"
|
|
106
|
+
};
|
|
107
|
+
var defaultGroup = {
|
|
108
|
+
seo: "SEO"
|
|
109
|
+
};
|
|
110
|
+
var defaultLocale = process.env.DEFAULT_LOCALE || "en";
|
|
111
|
+
var defaultMarket = process.env.DEFAULT_MARKET || "ww";
|
|
112
|
+
var defaultLocales = ["en", "it"];
|
|
113
|
+
var options = {
|
|
114
|
+
additionalFields: [],
|
|
115
|
+
collections: [],
|
|
116
|
+
customTokens: [],
|
|
117
|
+
defaultLocale,
|
|
118
|
+
defaultMarket,
|
|
119
|
+
group: defaultGroup,
|
|
120
|
+
locales: defaultLocales,
|
|
121
|
+
slug: defaultSlug,
|
|
122
|
+
qsMetatagsRules: "seoMetatagsRules",
|
|
123
|
+
translations
|
|
124
|
+
};
|
|
125
|
+
var internalSlugs = Object.entries(defaultSlug).map((x) => x[1]);
|
|
126
|
+
|
|
127
|
+
// src/utils/eachField.ts
|
|
128
|
+
var PRESENTATION_FIELDS = ["collapsible", "row", "tabs", "ui"];
|
|
129
|
+
function isPresentationField(field) {
|
|
130
|
+
return PRESENTATION_FIELDS.includes(field.type);
|
|
131
|
+
}
|
|
132
|
+
__name(isPresentationField, "isPresentationField");
|
|
133
|
+
function isDataField(field) {
|
|
134
|
+
return !PRESENTATION_FIELDS.includes(field.type);
|
|
135
|
+
}
|
|
136
|
+
__name(isDataField, "isDataField");
|
|
137
|
+
function eachField(fields, callback, options2) {
|
|
138
|
+
const acc = typeof options2 === "number" ? {
|
|
139
|
+
paths: [],
|
|
140
|
+
depth: 0,
|
|
141
|
+
maxDepth: options2
|
|
142
|
+
} : typeof options2 === "object" ? options2 : {
|
|
143
|
+
paths: [],
|
|
144
|
+
depth: 0,
|
|
145
|
+
maxDepth: -1
|
|
146
|
+
};
|
|
147
|
+
for (const field of fields) {
|
|
148
|
+
let shouldBreak = callback(field, acc.paths, acc.parent);
|
|
149
|
+
if (shouldBreak === true) {
|
|
150
|
+
return shouldBreak;
|
|
151
|
+
}
|
|
152
|
+
let nextAcc = {
|
|
153
|
+
...acc,
|
|
154
|
+
paths: [...acc.paths],
|
|
155
|
+
parent: field
|
|
156
|
+
};
|
|
157
|
+
if (isDataField(field)) {
|
|
158
|
+
nextAcc.paths.push(field.name);
|
|
159
|
+
}
|
|
160
|
+
if (acc.maxDepth === -1 || acc.depth < acc.maxDepth || isPresentationField(field)) {
|
|
161
|
+
switch (field.type) {
|
|
162
|
+
case "array":
|
|
163
|
+
nextAcc.depth++;
|
|
164
|
+
shouldBreak = eachField(field.fields, callback, nextAcc);
|
|
165
|
+
break;
|
|
166
|
+
case "blocks":
|
|
167
|
+
nextAcc.depth++;
|
|
168
|
+
for (const block of field.blocks) {
|
|
169
|
+
nextAcc = {
|
|
170
|
+
...nextAcc,
|
|
171
|
+
parent: block
|
|
172
|
+
};
|
|
173
|
+
shouldBreak = eachField(block.fields, callback, nextAcc);
|
|
174
|
+
if (shouldBreak === true) {
|
|
175
|
+
return shouldBreak;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
case "collapsible":
|
|
180
|
+
case "group":
|
|
181
|
+
case "row":
|
|
182
|
+
shouldBreak = eachField(field.fields, callback, nextAcc);
|
|
183
|
+
break;
|
|
184
|
+
case "tabs":
|
|
185
|
+
for (const tab of field.tabs) {
|
|
186
|
+
shouldBreak = eachField(tab.fields, callback, nextAcc);
|
|
187
|
+
if (shouldBreak === true) {
|
|
188
|
+
return shouldBreak;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (shouldBreak === true) {
|
|
193
|
+
return shouldBreak;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
__name(eachField, "eachField");
|
|
199
|
+
var TOKENIZABLE_FIELDS = ["text", "textarea", "richText", "relationship"];
|
|
200
|
+
function isTokenizableField(field) {
|
|
201
|
+
return TOKENIZABLE_FIELDS.includes(field.type);
|
|
202
|
+
}
|
|
203
|
+
__name(isTokenizableField, "isTokenizableField");
|
|
204
|
+
function eachTokenizableField(fields, callback, maxDepth = -1) {
|
|
205
|
+
return eachField(fields, (field, paths, parent) => {
|
|
206
|
+
if (isTokenizableField(field)) {
|
|
207
|
+
return callback(field, paths, parent);
|
|
208
|
+
}
|
|
209
|
+
}, maxDepth);
|
|
210
|
+
}
|
|
211
|
+
__name(eachTokenizableField, "eachTokenizableField");
|
|
212
|
+
var getTokenizableFields = /* @__PURE__ */ __name((collection) => {
|
|
213
|
+
const fields = [];
|
|
214
|
+
eachTokenizableField(collection.fields, (field) => {
|
|
215
|
+
fields.push(field);
|
|
216
|
+
});
|
|
217
|
+
return fields;
|
|
218
|
+
}, "getTokenizableFields");
|
|
219
|
+
|
|
220
|
+
// src/api/tokens.service.ts
|
|
221
|
+
var getTokens = /* @__PURE__ */ __name((collections) => {
|
|
222
|
+
const collectionConfigs = collections.length ? import_payload.default.config.collections.filter((config) => collections.includes(config.slug)) : import_payload.default.config.collections.filter((config) => options.collections.includes(config.slug));
|
|
223
|
+
let tokens = collectionConfigs.reduce((acc, config) => {
|
|
224
|
+
const fields = [];
|
|
225
|
+
eachTokenizableField(config.fields, (field) => {
|
|
226
|
+
fields.push(`[${field.name}]`);
|
|
227
|
+
});
|
|
228
|
+
return acc.length ? acc.filter((field) => fields.includes(field)) : fields;
|
|
229
|
+
}, []);
|
|
230
|
+
tokens = [...new Set(tokens)];
|
|
231
|
+
tokens = [...tokens, ...options.customTokens.map((token) => `[${token.name}]`)];
|
|
232
|
+
return tokens;
|
|
233
|
+
}, "getTokens");
|
|
234
|
+
var tokensGet = /* @__PURE__ */ __name(() => ({
|
|
235
|
+
path: "/seo/tokens",
|
|
236
|
+
method: "get",
|
|
237
|
+
handler: async (req, res) => {
|
|
238
|
+
try {
|
|
239
|
+
const qsCollections = req.query.collections || "";
|
|
240
|
+
const collections = qsCollections !== "" ? qsCollections.split(",") : [];
|
|
241
|
+
const tokens = getTokens(collections);
|
|
242
|
+
res.status(200).send(tokens);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
res.status(400).send(error);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}), "tokensGet");
|
|
248
|
+
|
|
249
|
+
// src/components/Tokens.tsx
|
|
250
|
+
var import_forms = require("payload/components/forms");
|
|
251
|
+
var React = __toESM(require("react"));
|
|
252
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
253
|
+
var Tokens = /* @__PURE__ */ __name(({ path }) => {
|
|
254
|
+
const collectionsField = (0, import_forms.useFormFields)(([fields]) => fields["collections"]);
|
|
255
|
+
const [tokens, setTokens] = React.useState([]);
|
|
256
|
+
React.useEffect(() => {
|
|
257
|
+
const fetchOptions = /* @__PURE__ */ __name(async () => {
|
|
258
|
+
try {
|
|
259
|
+
const collections = collectionsField.value;
|
|
260
|
+
const url = (process.env.PAYLOAD_PUBLIC_BASE_PATH || "") + "/api/seo/tokens?collections=" + (collections?.join(",") || "");
|
|
261
|
+
const response = await fetch(url, {
|
|
262
|
+
method: "GET",
|
|
263
|
+
headers: {
|
|
264
|
+
"Content-Type": "application/json"
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
if (response.ok) {
|
|
268
|
+
const tokens2 = await response.json();
|
|
269
|
+
console.log(tokens2);
|
|
270
|
+
setTokens(tokens2);
|
|
271
|
+
} else {
|
|
272
|
+
setTokens([]);
|
|
273
|
+
}
|
|
274
|
+
} catch (error) {
|
|
275
|
+
console.error("Error fetching data:", error);
|
|
276
|
+
}
|
|
277
|
+
}, "fetchOptions");
|
|
278
|
+
fetchOptions();
|
|
279
|
+
}, [collectionsField]);
|
|
280
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "tokens-list", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("ul", { children: tokens.map((token) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: token }, token)) }) });
|
|
281
|
+
}, "Tokens");
|
|
282
|
+
|
|
283
|
+
// src/collections/MetatagsRule.ts
|
|
284
|
+
var MetatagsRule = {
|
|
285
|
+
slug: options.slug.metatagsRule,
|
|
286
|
+
admin: {
|
|
287
|
+
group: options.group.seo
|
|
288
|
+
},
|
|
289
|
+
fields: [
|
|
290
|
+
{
|
|
291
|
+
name: "collections",
|
|
292
|
+
type: "select",
|
|
293
|
+
options: options.collections,
|
|
294
|
+
hasMany: true
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
name: "title",
|
|
298
|
+
type: "text"
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
name: "description",
|
|
302
|
+
type: "textarea"
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
name: "keywords",
|
|
306
|
+
type: "textarea"
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
label: "Available Tokens",
|
|
310
|
+
type: "collapsible",
|
|
311
|
+
admin: {
|
|
312
|
+
initCollapsed: true,
|
|
313
|
+
position: "sidebar"
|
|
314
|
+
},
|
|
315
|
+
fields: [
|
|
316
|
+
{
|
|
317
|
+
name: "tokens",
|
|
318
|
+
type: "ui",
|
|
319
|
+
admin: {
|
|
320
|
+
components: {
|
|
321
|
+
Field: Tokens
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
]
|
|
326
|
+
}
|
|
327
|
+
]
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
// src/utils/findVal.ts
|
|
331
|
+
var findVal = /* @__PURE__ */ __name((obj, keyToFind) => {
|
|
332
|
+
let value;
|
|
333
|
+
Object.keys(obj).some((k) => {
|
|
334
|
+
if (k === keyToFind) {
|
|
335
|
+
value = obj[k];
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
if (obj[k] && typeof obj[k] === "object") {
|
|
339
|
+
value = findVal(obj[k], keyToFind);
|
|
340
|
+
return value !== void 0;
|
|
341
|
+
}
|
|
342
|
+
return false;
|
|
343
|
+
});
|
|
344
|
+
return value;
|
|
345
|
+
}, "findVal");
|
|
346
|
+
|
|
347
|
+
// src/utils/richTextSerialize.ts
|
|
348
|
+
var import_slate = require("slate");
|
|
349
|
+
var stripHtml = /* @__PURE__ */ __name((html) => {
|
|
350
|
+
return html.replace(/<[^>]*>/g, "");
|
|
351
|
+
}, "stripHtml");
|
|
352
|
+
function richTextSerialize(children) {
|
|
353
|
+
if (Array.isArray(children)) {
|
|
354
|
+
return children.map((node, i) => {
|
|
355
|
+
if (import_slate.Text.isText(node)) {
|
|
356
|
+
return node.text;
|
|
357
|
+
}
|
|
358
|
+
if (!node) {
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
return richTextSerialize(node.children);
|
|
362
|
+
}).join("\n");
|
|
363
|
+
}
|
|
364
|
+
if (typeof children === "string") {
|
|
365
|
+
return stripHtml(children);
|
|
366
|
+
}
|
|
367
|
+
return "";
|
|
368
|
+
}
|
|
369
|
+
__name(richTextSerialize, "richTextSerialize");
|
|
370
|
+
|
|
371
|
+
// src/seo.ts
|
|
372
|
+
var replaceTokens = /* @__PURE__ */ __name(async (req, collection, doc, str, locale) => {
|
|
373
|
+
const tokens = str.match(/\[(.*?)\]/g) || [];
|
|
374
|
+
const defaultLocale2 = req.payload.config.localization ? req.payload.config.localization.defaultLocale : "en";
|
|
375
|
+
const fields = getTokenizableFields(collection);
|
|
376
|
+
for (const token of tokens) {
|
|
377
|
+
const customToken = options.customTokens.find((customToken2) => `[${customToken2.name}]` === token);
|
|
378
|
+
if (customToken) {
|
|
379
|
+
try {
|
|
380
|
+
str = str.replace(token, await customToken.replacementFunction(req, collection, doc, locale));
|
|
381
|
+
} catch (error) {
|
|
382
|
+
console.error(`Error while replacing customtoken "${token}": ${error}`);
|
|
383
|
+
}
|
|
384
|
+
} else {
|
|
385
|
+
const fieldName = token.replace(/[[\]]/g, "");
|
|
386
|
+
const fieldConfig = fields.find((f) => f.name === fieldName);
|
|
387
|
+
if (!fieldConfig) {
|
|
388
|
+
str = str.replace(token, "");
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const fieldValue = findVal(doc, fieldName);
|
|
392
|
+
if (!fieldValue || Object.keys(fieldValue).length === 0) {
|
|
393
|
+
str = str.replace(token, "");
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
switch (fieldConfig?.type) {
|
|
397
|
+
case "text":
|
|
398
|
+
case "textarea": {
|
|
399
|
+
str = str.replace(token, locale && fieldConfig?.localized ? fieldValue[locale.code] ?? fieldValue[defaultLocale2] : fieldValue);
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case "richText": {
|
|
403
|
+
str = str.replace(token, locale && fieldConfig?.localized ? richTextSerialize(fieldValue[locale.code]) ?? richTextSerialize(fieldValue[defaultLocale2]) : richTextSerialize(fieldValue));
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case "relationship": {
|
|
407
|
+
const isPolymorphicRel = Array.isArray(fieldConfig.relationTo);
|
|
408
|
+
if (!isPolymorphicRel) {
|
|
409
|
+
if (locale && fieldConfig.hasMany) {
|
|
410
|
+
str = str.replace(token, fieldValue.map((id) => id).join(", "));
|
|
411
|
+
} else {
|
|
412
|
+
const relatedConfig = req.payload.config.collections.find((c) => c.slug === fieldConfig.relationTo);
|
|
413
|
+
if (relatedConfig) {
|
|
414
|
+
const relatedCollfields = getTokenizableFields(relatedConfig);
|
|
415
|
+
const titleField = relatedConfig?.admin?.useAsTitle ? relatedCollfields.find((f) => f.name === relatedConfig.admin.useAsTitle) : relatedCollfields.find((f) => f.name === "title");
|
|
416
|
+
if (titleField) {
|
|
417
|
+
if (fieldConfig.hasMany) {
|
|
418
|
+
const titleFieldValue = fieldValue.map((value) => findVal(value, titleField.name)).join(", ");
|
|
419
|
+
str = str.replace(token, titleFieldValue);
|
|
420
|
+
} else {
|
|
421
|
+
const titleFieldValue = findVal(fieldValue, titleField.name);
|
|
422
|
+
str = str.replace(token, locale && titleField?.localized ? titleFieldValue[locale.code] ?? titleFieldValue[defaultLocale2] : titleFieldValue);
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
str = str.replace(token, "");
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
} else {
|
|
430
|
+
str = str.replace(token, "");
|
|
431
|
+
}
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return str;
|
|
438
|
+
}, "replaceTokens");
|
|
439
|
+
var setCollectionMetatags = /* @__PURE__ */ __name(({ req, doc, query }) => async (collection) => {
|
|
440
|
+
try {
|
|
441
|
+
if (req.query[options.qsMetatagsRules] === void 0) {
|
|
442
|
+
return doc;
|
|
443
|
+
}
|
|
444
|
+
const result = await req.payload.find({
|
|
445
|
+
collection: options.slug.metatagsRule,
|
|
446
|
+
where: {
|
|
447
|
+
or: [
|
|
448
|
+
{ collections: { equals: "" } },
|
|
449
|
+
{ collections: { equals: [] } },
|
|
450
|
+
{ collections: { equals: null } },
|
|
451
|
+
{ collections: { exists: false } },
|
|
452
|
+
{ collections: { contains: collection.slug } }
|
|
453
|
+
]
|
|
454
|
+
},
|
|
455
|
+
pagination: false
|
|
456
|
+
});
|
|
457
|
+
let rules = result.docs;
|
|
458
|
+
rules = rules.sort((a, b) => a.collections.length === 0 ? 1 : b.collections.length === 0 ? -1 : 0);
|
|
459
|
+
const metaFields = ["title", "description", "keywords", ...options.additionalFields.map((field) => field.name)];
|
|
460
|
+
for (const rule of rules) {
|
|
461
|
+
for (const metafield of metaFields) {
|
|
462
|
+
doc.meta = doc.meta || {};
|
|
463
|
+
const ruleValue = rule[metafield];
|
|
464
|
+
if (!ruleValue || doc.meta[metafield]) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (req.locale === "all") {
|
|
468
|
+
const locales = req.payload?.config.localization ? req.payload.config.localization.locales : [];
|
|
469
|
+
for (const locale of locales) {
|
|
470
|
+
doc.meta[metafield] = doc.meta[metafield] || {};
|
|
471
|
+
doc.meta[metafield][locale.code] = await replaceTokens(req, collection, doc, ruleValue, locale);
|
|
472
|
+
}
|
|
473
|
+
} else {
|
|
474
|
+
doc.meta[metafield] = await replaceTokens(req, collection, doc, ruleValue, null);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
} catch (error) {
|
|
479
|
+
console.error(`Error while setting seo metatags: ${error}`);
|
|
480
|
+
}
|
|
481
|
+
return doc;
|
|
482
|
+
}, "setCollectionMetatags");
|
|
483
|
+
var seo = /* @__PURE__ */ __name((sourceOptions) => (sourceConfig) => {
|
|
484
|
+
options.collections = sourceOptions.collections ? Object.assign(options.collections, sourceOptions.collections) : Object.assign(options.collections, sourceConfig.collections?.map((x) => x.slug));
|
|
485
|
+
options.additionalFields = sourceOptions.additionalFields || [];
|
|
486
|
+
options.customTokens = sourceOptions.customTokens ? Object.assign(options.customTokens, sourceOptions.customTokens) : [];
|
|
487
|
+
options.qsMetatagsRules = sourceOptions.qsMetatagsRules || "seoMetatagsRules";
|
|
488
|
+
const collections = sourceConfig.collections?.filter((collection) => options.collections.includes(collection.slug));
|
|
489
|
+
collections?.forEach((collection) => {
|
|
490
|
+
collection.hooks = {
|
|
491
|
+
...collection.hooks,
|
|
492
|
+
afterRead: [...collection.hooks?.afterRead || [], (args) => setCollectionMetatags(args)(collection)]
|
|
493
|
+
};
|
|
494
|
+
});
|
|
495
|
+
const targetConfig = {
|
|
496
|
+
...sourceConfig,
|
|
497
|
+
collections: [
|
|
498
|
+
...sourceConfig.collections || [],
|
|
499
|
+
{
|
|
500
|
+
...MetatagsRule,
|
|
501
|
+
fields: [
|
|
502
|
+
...MetatagsRule.fields,
|
|
503
|
+
...options.additionalFields
|
|
504
|
+
],
|
|
505
|
+
access: {
|
|
506
|
+
...MetatagsRule.access,
|
|
507
|
+
...sourceOptions.metatagsRulesAccess
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
],
|
|
511
|
+
endpoints: [
|
|
512
|
+
...sourceConfig.endpoints || [],
|
|
513
|
+
tokensGet()
|
|
514
|
+
]
|
|
515
|
+
};
|
|
516
|
+
return targetConfig;
|
|
517
|
+
}, "seo");
|
|
518
|
+
|
|
519
|
+
// src/index.ts
|
|
520
|
+
var src_default = seo;
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@websolutespa/payload-plugin-seo",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "SEO plugin for PayloadCms",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"payload",
|
|
7
|
+
"seo"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git://github.com/websolutespa/bom.git"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"lint": "eslint *.ts*",
|
|
16
|
+
"test": "jest --forceExit --runInBand --workerIdleMemoryLimit='1000MB'",
|
|
17
|
+
"test-watch": "npm test -- --watch",
|
|
18
|
+
"coverage:integration": "npm run test -- --coverage",
|
|
19
|
+
"coverage": "run-s -c --silent coverage:*",
|
|
20
|
+
"compile": "tsup ./src/index.ts --format cjs --dts --external react",
|
|
21
|
+
"prepack": "bom prepack",
|
|
22
|
+
"postpack": "bom postpack"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"ts-deepmerge": "^2.0.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@payloadcms/db-mongodb": ">= 1.2.0",
|
|
29
|
+
"payload": ">= 2.11.2",
|
|
30
|
+
"react": ">= 18.2.0",
|
|
31
|
+
"uuid": ">= 9.0.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/express": "^4.17.15",
|
|
35
|
+
"@types/uuid": "^9.0.2",
|
|
36
|
+
"@websolutespa/bom-cli": "*",
|
|
37
|
+
"@websolutespa/bom-env": "*",
|
|
38
|
+
"@websolutespa/test": "*",
|
|
39
|
+
"@websolutespa/tsconfig": "*",
|
|
40
|
+
"esbuild-sass-plugin": "^2.10.0",
|
|
41
|
+
"eslint-config-websolute": "*",
|
|
42
|
+
"eslint": "^8.56.0",
|
|
43
|
+
"mongodb-memory-server": "^8.13.0",
|
|
44
|
+
"react": "^18.2.0",
|
|
45
|
+
"ts-node": "^10.9.2",
|
|
46
|
+
"tsup": "^6.7.0",
|
|
47
|
+
"typescript": "^5.4.5",
|
|
48
|
+
"uuid": "^9.0.1"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"sideEffects": false,
|
|
54
|
+
"prepack": {
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"main": "./dist/index.js"
|
|
57
|
+
},
|
|
58
|
+
"postpack": {
|
|
59
|
+
"types": "./src/index.ts",
|
|
60
|
+
"main": "./src/index.ts"
|
|
61
|
+
},
|
|
62
|
+
"types": "./dist/index.d.ts",
|
|
63
|
+
"main": "./dist/index.js"
|
|
64
|
+
}
|