@lexingtonthemes/seo 0.1.0
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/README.md +225 -0
- package/index.ts +7 -0
- package/package.json +58 -0
- package/src/AstroSeo.astro +42 -0
- package/src/__test__/buildTags.test.ts +441 -0
- package/src/types.ts +455 -0
- package/src/utils/buildTags.ts +457 -0
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { buildTags } from "../utils/buildTags";
|
|
2
|
+
import { HtmlValidate as _HtmlValidate } from "html-validate/node";
|
|
3
|
+
|
|
4
|
+
const validate = new _HtmlValidate();
|
|
5
|
+
|
|
6
|
+
describe("buildTags function", () => {
|
|
7
|
+
it("should return an empty string if no config is provided", () => {
|
|
8
|
+
const result = buildTags({});
|
|
9
|
+
expect(result).toBe("");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should handle null or undefined values gracefully", () => {
|
|
13
|
+
const config = {
|
|
14
|
+
title: null,
|
|
15
|
+
description: undefined,
|
|
16
|
+
};
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
const result = buildTags(config);
|
|
19
|
+
expect(result).not.toContain("null");
|
|
20
|
+
expect(result).not.toContain("undefined");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should escape special characters correctly", async () => {
|
|
24
|
+
const config = {
|
|
25
|
+
title: "Title & Description",
|
|
26
|
+
};
|
|
27
|
+
const result = buildTags(config);
|
|
28
|
+
expect(result).toContain("Title & Description");
|
|
29
|
+
|
|
30
|
+
const htmlResult = await validate.validateString(result);
|
|
31
|
+
expect(htmlResult.valid).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should generate correct title tag", async () => {
|
|
35
|
+
const config = {
|
|
36
|
+
title: '<script>alert("hacked")</script>',
|
|
37
|
+
};
|
|
38
|
+
const result = buildTags(config);
|
|
39
|
+
expect(result).toContain(
|
|
40
|
+
"<title><script>alert("hacked")</script></title>"
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const htmlResult = await validate.validateString(result);
|
|
44
|
+
expect(htmlResult.valid).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should generate correct description tag", async () => {
|
|
48
|
+
const config = {
|
|
49
|
+
description: '<img src=x onerror=alert("hacked")>',
|
|
50
|
+
};
|
|
51
|
+
const result = buildTags(config);
|
|
52
|
+
expect(result).toContain(
|
|
53
|
+
'<meta name="description" content="<img src=x onerror=alert("hacked")>">'
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const htmlResult = await validate.validateString(result);
|
|
57
|
+
expect(htmlResult.valid).toBe(true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should escape URLs", async () => {
|
|
61
|
+
const config = {
|
|
62
|
+
openGraph: {
|
|
63
|
+
url: '<script>alert("hacked")</script>',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const result = buildTags(config);
|
|
67
|
+
expect(result).toContain(
|
|
68
|
+
'<meta property="og:url" content="<script>alert("hacked")</script>">'
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const htmlResult = await validate.validateString(result);
|
|
72
|
+
expect(htmlResult.valid).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should generate correct robots tag for noindex and nofollow", async () => {
|
|
76
|
+
const config = {
|
|
77
|
+
noindex: true,
|
|
78
|
+
nofollow: true,
|
|
79
|
+
};
|
|
80
|
+
const result = buildTags(config);
|
|
81
|
+
expect(result).toContain('<meta name="robots" content="noindex,nofollow">');
|
|
82
|
+
|
|
83
|
+
const htmlResult = await validate.validateString(result);
|
|
84
|
+
expect(htmlResult.valid).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should generate correct twitter card tag", async () => {
|
|
88
|
+
const config = {
|
|
89
|
+
twitter: {
|
|
90
|
+
cardType: "summary_large_image",
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
const result = buildTags(config);
|
|
94
|
+
expect(result).toContain(
|
|
95
|
+
'<meta name="twitter:card" content="summary_large_image">'
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const htmlResult = await validate.validateString(result);
|
|
99
|
+
expect(htmlResult.valid).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should generate correct facebook app id tag", async () => {
|
|
103
|
+
const config = {
|
|
104
|
+
facebook: {
|
|
105
|
+
appId: "1234567890",
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
const result = buildTags(config);
|
|
109
|
+
expect(result).toContain(
|
|
110
|
+
'<meta property="fb:app_id" content="1234567890">'
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const htmlResult = await validate.validateString(result);
|
|
114
|
+
expect(htmlResult.valid).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should generate correct openGraph title tag", async () => {
|
|
118
|
+
const config = {
|
|
119
|
+
openGraph: {
|
|
120
|
+
title: "<b>Test Title</b>",
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
const result = buildTags(config);
|
|
124
|
+
expect(result).toContain(
|
|
125
|
+
'<meta property="og:title" content="<b>Test Title</b>">'
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const htmlResult = await validate.validateString(result);
|
|
129
|
+
expect(htmlResult.valid).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should generate correct canonical link tag", async () => {
|
|
133
|
+
const config = {
|
|
134
|
+
canonical: "https://example.com/page",
|
|
135
|
+
};
|
|
136
|
+
const result = buildTags(config);
|
|
137
|
+
expect(result).toContain(
|
|
138
|
+
'<link rel="canonical" href="https://example.com/page">'
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const htmlResult = await validate.validateString(result);
|
|
142
|
+
expect(htmlResult.valid).toBe(true);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should generate correct alternate link tag for mobile", async () => {
|
|
146
|
+
const config = {
|
|
147
|
+
mobileAlternate: {
|
|
148
|
+
media: "only screen and (max-width: 640px)",
|
|
149
|
+
href: "https://m.example.com/page",
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
const result = buildTags(config);
|
|
153
|
+
expect(result).toContain(
|
|
154
|
+
'<link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.example.com/page">'
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const htmlResult = await validate.validateString(result);
|
|
158
|
+
expect(htmlResult.valid).toBe(true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("should generate correct openGraph description tag", async () => {
|
|
162
|
+
const config = {
|
|
163
|
+
openGraph: {
|
|
164
|
+
description: "<i>Test Description</i>",
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
const result = buildTags(config);
|
|
168
|
+
expect(result).toContain(
|
|
169
|
+
'<meta property="og:description" content="<i>Test Description</i>">'
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const htmlResult = await validate.validateString(result);
|
|
173
|
+
expect(htmlResult.valid).toBe(true);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should generate correct openGraph type tag", async () => {
|
|
177
|
+
const config = {
|
|
178
|
+
openGraph: {
|
|
179
|
+
type: "article",
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
const result = buildTags(config);
|
|
183
|
+
expect(result).toContain('<meta property="og:type" content="article">');
|
|
184
|
+
|
|
185
|
+
const htmlResult = await validate.validateString(result);
|
|
186
|
+
expect(htmlResult.valid).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should generate correct openGraph locale tag", async () => {
|
|
190
|
+
const config = {
|
|
191
|
+
openGraph: {
|
|
192
|
+
locale: "en_US",
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
const result = buildTags(config);
|
|
196
|
+
expect(result).toContain('<meta property="og:locale" content="en_US">');
|
|
197
|
+
|
|
198
|
+
const htmlResult = await validate.validateString(result);
|
|
199
|
+
expect(htmlResult.valid).toBe(true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should generate correct openGraph site_name tag", async () => {
|
|
203
|
+
const config = {
|
|
204
|
+
openGraph: {
|
|
205
|
+
site_name: "Test Site",
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
const result = buildTags(config);
|
|
209
|
+
expect(result).toContain(
|
|
210
|
+
'<meta property="og:site_name" content="Test Site">'
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const htmlResult = await validate.validateString(result);
|
|
214
|
+
expect(htmlResult.valid).toBe(true);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("should handle multiple OpenGraph media tags correctly", async () => {
|
|
218
|
+
const config = {
|
|
219
|
+
openGraph: {
|
|
220
|
+
images: [
|
|
221
|
+
{ url: "https://example.com/image1.jpg" },
|
|
222
|
+
{ url: "https://example.com/image2.jpg" },
|
|
223
|
+
],
|
|
224
|
+
videos: [
|
|
225
|
+
{ url: "https://example.com/video1.mp4" },
|
|
226
|
+
{ url: "https://example.com/video2.mp4" },
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
const result = buildTags(config);
|
|
231
|
+
expect(result).toContain(
|
|
232
|
+
'<meta property="og:image" content="https://example.com/image1.jpg">'
|
|
233
|
+
);
|
|
234
|
+
expect(result).toContain(
|
|
235
|
+
'<meta property="og:image" content="https://example.com/image2.jpg">'
|
|
236
|
+
);
|
|
237
|
+
expect(result).toContain(
|
|
238
|
+
'<meta property="og:video" content="https://example.com/video1.mp4">'
|
|
239
|
+
);
|
|
240
|
+
expect(result).toContain(
|
|
241
|
+
'<meta property="og:video" content="https://example.com/video2.mp4">'
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const htmlResult = await validate.validateString(result);
|
|
245
|
+
expect(htmlResult.valid).toBe(true);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("should generate correct languageAlternates link tags", async () => {
|
|
249
|
+
const config = {
|
|
250
|
+
languageAlternates: [
|
|
251
|
+
{ hreflang: "es", href: "https://example.com/es" },
|
|
252
|
+
{ hreflang: "fr", href: "https://example.com/fr" },
|
|
253
|
+
],
|
|
254
|
+
};
|
|
255
|
+
const result = buildTags(config);
|
|
256
|
+
expect(result).toContain(
|
|
257
|
+
'<link rel="alternate" hreflang="es" href="https://example.com/es">'
|
|
258
|
+
);
|
|
259
|
+
expect(result).toContain(
|
|
260
|
+
'<link rel="alternate" hreflang="fr" href="https://example.com/fr">'
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const htmlResult = await validate.validateString(result);
|
|
264
|
+
expect(htmlResult.valid).toBe(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("should generate correct twitter site and creator tags", async () => {
|
|
268
|
+
const config = {
|
|
269
|
+
twitter: {
|
|
270
|
+
site: "@testsite",
|
|
271
|
+
handle: "@testhandle",
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
const result = buildTags(config);
|
|
275
|
+
expect(result).toContain('<meta name="twitter:site" content="@testsite">');
|
|
276
|
+
expect(result).toContain(
|
|
277
|
+
'<meta name="twitter:creator" content="@testhandle">'
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
const htmlResult = await validate.validateString(result);
|
|
281
|
+
expect(htmlResult.valid).toBe(true);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("should generate correct additionalMetaTags", async () => {
|
|
285
|
+
const config = {
|
|
286
|
+
additionalMetaTags: [
|
|
287
|
+
{ name: "viewport", content: "width=device-width, initial-scale=1" },
|
|
288
|
+
],
|
|
289
|
+
};
|
|
290
|
+
const result = buildTags(config);
|
|
291
|
+
expect(result).toContain(
|
|
292
|
+
'<meta content="width=device-width, initial-scale=1" name="viewport">'
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const htmlResult = await validate.validateString(result);
|
|
296
|
+
expect(htmlResult.valid).toBe(true);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("should generate correct additionalLinkTags", async () => {
|
|
300
|
+
const config = {
|
|
301
|
+
additionalLinkTags: [
|
|
302
|
+
{ rel: "stylesheet", href: "https://example.com/styles.css" },
|
|
303
|
+
],
|
|
304
|
+
};
|
|
305
|
+
const result = buildTags(config);
|
|
306
|
+
expect(result).toContain(
|
|
307
|
+
'<link rel="stylesheet" href="https://example.com/styles.css">'
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
const htmlResult = await validate.validateString(result);
|
|
311
|
+
expect(htmlResult.valid).toBe(true);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("should generate og:title and og:description from title and description if not explicitly set", async () => {
|
|
315
|
+
const config = {
|
|
316
|
+
title: "Test Title",
|
|
317
|
+
description: "Test Description",
|
|
318
|
+
openGraph: {},
|
|
319
|
+
};
|
|
320
|
+
const result = buildTags(config);
|
|
321
|
+
expect(result).toContain('<meta property="og:title" content="Test Title">');
|
|
322
|
+
expect(result).toContain(
|
|
323
|
+
'<meta property="og:description" content="Test Description">'
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
const htmlResult = await validate.validateString(result);
|
|
327
|
+
expect(htmlResult.valid).toBe(true);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("should not generate og:title and og:description if openGraph is not defined", async () => {
|
|
331
|
+
const config = {
|
|
332
|
+
title: "Test Title",
|
|
333
|
+
description: "Test Description",
|
|
334
|
+
};
|
|
335
|
+
const result = buildTags(config);
|
|
336
|
+
expect(result).not.toContain(
|
|
337
|
+
'<meta property="og:title" content="Test Title">'
|
|
338
|
+
);
|
|
339
|
+
expect(result).not.toContain(
|
|
340
|
+
'<meta property="og:description" content="Test Description">'
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
const htmlResult = await validate.validateString(result);
|
|
344
|
+
expect(htmlResult.valid).toBe(true);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Casos de prueba realistas
|
|
348
|
+
it("should generate a complete set of tags for a blog post", async () => {
|
|
349
|
+
const config = {
|
|
350
|
+
title: "My Blog Post",
|
|
351
|
+
description: "A detailed description of my blog post.",
|
|
352
|
+
canonical: "https://example.com/blog/my-blog-post",
|
|
353
|
+
openGraph: {
|
|
354
|
+
type: "article",
|
|
355
|
+
url: "https://example.com/blog/my-blog-post",
|
|
356
|
+
title: "My Blog Post",
|
|
357
|
+
description: "A detailed description of my blog post.",
|
|
358
|
+
images: [{ url: "https://example.com/images/blog-image.jpg" }],
|
|
359
|
+
},
|
|
360
|
+
twitter: {
|
|
361
|
+
cardType: "summary_large_image",
|
|
362
|
+
site: "@example",
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
const result = buildTags(config);
|
|
366
|
+
expect(result).toContain("<title>My Blog Post</title>");
|
|
367
|
+
expect(result).toContain(
|
|
368
|
+
'<meta name="description" content="A detailed description of my blog post.">'
|
|
369
|
+
);
|
|
370
|
+
expect(result).toContain(
|
|
371
|
+
'<link rel="canonical" href="https://example.com/blog/my-blog-post">'
|
|
372
|
+
);
|
|
373
|
+
expect(result).toContain('<meta property="og:type" content="article">');
|
|
374
|
+
expect(result).toContain(
|
|
375
|
+
'<meta property="og:url" content="https://example.com/blog/my-blog-post">'
|
|
376
|
+
);
|
|
377
|
+
expect(result).toContain(
|
|
378
|
+
'<meta property="og:title" content="My Blog Post">'
|
|
379
|
+
);
|
|
380
|
+
expect(result).toContain(
|
|
381
|
+
'<meta property="og:description" content="A detailed description of my blog post.">'
|
|
382
|
+
);
|
|
383
|
+
expect(result).toContain(
|
|
384
|
+
'<meta property="og:image" content="https://example.com/images/blog-image.jpg">'
|
|
385
|
+
);
|
|
386
|
+
expect(result).toContain(
|
|
387
|
+
'<meta name="twitter:card" content="summary_large_image">'
|
|
388
|
+
);
|
|
389
|
+
expect(result).toContain('<meta name="twitter:site" content="@example">');
|
|
390
|
+
|
|
391
|
+
const htmlResult = await validate.validateString(result);
|
|
392
|
+
expect(htmlResult.valid).toBe(true);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it("should generate a complete set of tags for a product page", async () => {
|
|
396
|
+
const config = {
|
|
397
|
+
title: "Product Name",
|
|
398
|
+
description: "Description of the product.",
|
|
399
|
+
canonical: "https://example.com/products/product-name",
|
|
400
|
+
openGraph: {
|
|
401
|
+
type: "product",
|
|
402
|
+
url: "https://example.com/products/product-name",
|
|
403
|
+
title: "Product Name",
|
|
404
|
+
description: "Description of the product.",
|
|
405
|
+
images: [{ url: "https://example.com/images/product-image.jpg" }],
|
|
406
|
+
},
|
|
407
|
+
twitter: {
|
|
408
|
+
cardType: "summary",
|
|
409
|
+
site: "@examplestore",
|
|
410
|
+
},
|
|
411
|
+
};
|
|
412
|
+
const result = buildTags(config);
|
|
413
|
+
expect(result).toContain("<title>Product Name</title>");
|
|
414
|
+
expect(result).toContain(
|
|
415
|
+
'<meta name="description" content="Description of the product.">'
|
|
416
|
+
);
|
|
417
|
+
expect(result).toContain(
|
|
418
|
+
'<link rel="canonical" href="https://example.com/products/product-name">'
|
|
419
|
+
);
|
|
420
|
+
expect(result).toContain('<meta property="og:type" content="product">');
|
|
421
|
+
expect(result).toContain(
|
|
422
|
+
'<meta property="og:url" content="https://example.com/products/product-name">'
|
|
423
|
+
);
|
|
424
|
+
expect(result).toContain(
|
|
425
|
+
'<meta property="og:title" content="Product Name">'
|
|
426
|
+
);
|
|
427
|
+
expect(result).toContain(
|
|
428
|
+
'<meta property="og:description" content="Description of the product.">'
|
|
429
|
+
);
|
|
430
|
+
expect(result).toContain(
|
|
431
|
+
'<meta property="og:image" content="https://example.com/images/product-image.jpg">'
|
|
432
|
+
);
|
|
433
|
+
expect(result).toContain('<meta name="twitter:card" content="summary">');
|
|
434
|
+
expect(result).toContain(
|
|
435
|
+
'<meta name="twitter:site" content="@examplestore">'
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
const htmlResult = await validate.validateString(result);
|
|
439
|
+
expect(htmlResult.valid).toBe(true);
|
|
440
|
+
});
|
|
441
|
+
});
|