@cocreate/sitemap 1.2.2 → 1.2.4

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 CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.2.4](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.3...v1.2.4) (2024-09-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * undefined changefreq ([a41333f](https://github.com/CoCreate-app/CoCreate-sitemap/commit/a41333f4fa65b18a531a0889211c4a9a04d040ab))
7
+
8
+ ## [1.2.3](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.2...v1.2.3) (2024-09-08)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * encodeValues for characters xml cant parse. Format date correctly ([e928405](https://github.com/CoCreate-app/CoCreate-sitemap/commit/e928405998d5f7492acdd1bb35c67f44c9bce4ad))
14
+
1
15
  ## [1.2.2](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.1...v1.2.2) (2024-09-01)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/sitemap",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "A simple sitemap component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "sitemap",
package/src/index.js CHANGED
@@ -59,8 +59,7 @@ class CoCreateSitemap {
59
59
  if (!file.sitemap)
60
60
  file.sitemap = {}
61
61
 
62
- // TODO: need to get info such as host
63
- const entry = this.createEntry(file);
62
+ const entry = this.createEntry(file, host);
64
63
 
65
64
  let { mainSitemap, sitemap } = await this.getSitemap(file, host);
66
65
 
@@ -100,14 +99,17 @@ class CoCreateSitemap {
100
99
  if (file['content-type'] === 'text/html') {
101
100
  this.parseHtml(file)
102
101
 
103
- if (file.sitemap.type !== 'news') {
104
- if (file.sitemap.changefreq)
102
+ if (file.sitemap.type !== 'news' && file.sitemap.type !== 'image' && file.sitemap.type !== 'video') {
103
+ if (!file.sitemap.changefreq)
105
104
  file.sitemap.changefreq = 'monthly';
106
105
 
107
106
  if (!file.sitemap.priority) {
108
107
  const depth = (file.pathname.match(/\//g) || []).length;
109
108
  file.sitemap.priority = Math.max(0.1, 1.0 - (depth - 1) * 0.1).toFixed(1);
110
109
  }
110
+ } else {
111
+ delete file.sitemap.changefreq
112
+ delete file.sitemap.priority
111
113
  }
112
114
  }
113
115
 
@@ -116,17 +118,18 @@ class CoCreateSitemap {
116
118
  for (const key of Object.keys(file.sitemap)) {
117
119
  if (key === 'pathname' || key === 'type')
118
120
  continue
121
+
119
122
  let value = file.sitemap[key];
120
123
 
121
124
  if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
122
125
  if (!Array.isArray(value))
123
126
  value = [value]
127
+
124
128
  for (let i = 0; i < value.length; i++) {
125
129
  entry += `\t\t<${key}:${key}>\n`;
126
130
 
127
131
  for (const nestedKey of Object.keys(value[i])) {
128
- const nestedValue = value[i][nestedKey];
129
-
132
+ let nestedValue = value[i][nestedKey];
130
133
  // Handle nested objects
131
134
  if (typeof nestedValue === 'object' && nestedValue !== null && !(nestedValue instanceof Date)) {
132
135
  entry += `\t\t\t<${key}:${nestedKey}>\n`;
@@ -136,6 +139,16 @@ class CoCreateSitemap {
136
139
  }
137
140
  entry += `\t\t\t</${key}:${nestedKey}>\n`;
138
141
  } else {
142
+ if (nestedKey === 'loc') {
143
+ if (!nestedValue.startsWith('https://') && !nestedValue.startsWith('http://') && !nestedValue.startsWith('{{$host}}')) {
144
+ nestedValue = `{{$host}}${nestedValue}`;
145
+ }
146
+ } else if (nestedKey === 'publication_date') {
147
+ nestedValue = new Date(nestedValue).toISOString().split('.')[0] + "Z";
148
+ } else {
149
+ nestedValue = this.encodeXML(nestedValue)
150
+ }
151
+
139
152
  entry += `\t\t\t<${key}:${nestedKey}>${nestedValue}</${key}:${nestedKey}>\n`;
140
153
  }
141
154
  }
@@ -143,6 +156,16 @@ class CoCreateSitemap {
143
156
  entry += `\t\t</${key}:${key}>\n`;
144
157
  }
145
158
  } else {
159
+ if (key === 'loc') {
160
+ if (!value.startsWith('https://') && !value.startsWith('http://')) {
161
+ value = `{{$host}}${value}`;
162
+ }
163
+ } else if (key === 'lastmod') {
164
+ value = new Date(file.modified.on).toISOString().split('.')[0] + "Z";
165
+ } else {
166
+ value = this.encodeXML(value)
167
+ }
168
+
146
169
  entry += `\t\t<${key}>${value}</${key}>\n`;
147
170
  }
148
171
  }
@@ -432,14 +455,25 @@ class CoCreateSitemap {
432
455
 
433
456
  }
434
457
 
435
- if (file.sitemap.type !== 'news') {
458
+ if (file.sitemap.type !== 'news' && file.sitemap.type !== 'image' && file.sitemap.type !== 'video') {
436
459
  const priorityMeta = dom.querySelector('meta[name="sitemap-priority"]');
437
460
  const changefreqMeta = dom.querySelector('meta[name="sitemap-changefreq"]');
438
461
  file.sitemap.priority = priorityMeta ? priorityMeta.getAttribute('content') : file.sitemap.priority; // Default priority if not specified
439
- file.sitemap.changefreq = changefreqMeta ? changefreqMeta.getAttribute('content') : file.sitemap.changefreq; // Default changefreq if not specified
462
+ file.sitemap.changefreq = changefreqMeta ? changefreqMeta.getAttribute('content') : file.sitemap.changefreq || 'monthly'; // Default changefreq if not specified
440
463
  }
441
464
 
442
465
  }
466
+
467
+ encodeXML(str) {
468
+ if (str)
469
+ return str
470
+ .replace(/&/g, '&amp;')
471
+ .replace(/</g, '&lt;')
472
+ .replace(/>/g, '&gt;')
473
+ .replace(/"/g, '&quot;')
474
+ .replace(/'/g, '&apos;');
475
+ }
476
+
443
477
  }
444
478
 
445
479
  module.exports = CoCreateSitemap;