@cocreate/sitemap 1.2.1 → 1.2.3

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.3](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.2...v1.2.3) (2024-09-08)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * encodeValues for characters xml cant parse. Format date correctly ([e928405](https://github.com/CoCreate-app/CoCreate-sitemap/commit/e928405998d5f7492acdd1bb35c67f44c9bce4ad))
7
+
8
+ ## [1.2.2](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.1...v1.2.2) (2024-09-01)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * uncomment lastmod compare ([23c74cf](https://github.com/CoCreate-app/CoCreate-sitemap/commit/23c74cfeb20c7bb6dec07ae08a30daa2d8e0ab59))
14
+
1
15
  ## [1.2.1](https://github.com/CoCreate-app/CoCreate-sitemap/compare/v1.2.0...v1.2.1) (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.1",
3
+ "version": "1.2.3",
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
@@ -46,8 +46,8 @@ class CoCreateSitemap {
46
46
 
47
47
  // Compare the lastmod date in the sitemap with the modified.on date
48
48
  if (file.sitemap && file.sitemap.lastmod && file.modified.on) {
49
- // if (new Date(file.sitemap.lastmod) >= new Date(file.modified.on))
50
- // return;
49
+ if (new Date(file.sitemap.lastmod) >= new Date(file.modified.on))
50
+ return;
51
51
  }
52
52
 
53
53
  // Logic to update the sitemap
@@ -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
 
@@ -101,7 +100,7 @@ class CoCreateSitemap {
101
100
  this.parseHtml(file)
102
101
 
103
102
  if (file.sitemap.type !== 'news') {
104
- if (file.sitemap.changefreq)
103
+ if (!file.sitemap.changefreq)
105
104
  file.sitemap.changefreq = 'monthly';
106
105
 
107
106
  if (!file.sitemap.priority) {
@@ -116,17 +115,18 @@ class CoCreateSitemap {
116
115
  for (const key of Object.keys(file.sitemap)) {
117
116
  if (key === 'pathname' || key === 'type')
118
117
  continue
118
+
119
119
  let value = file.sitemap[key];
120
120
 
121
121
  if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
122
122
  if (!Array.isArray(value))
123
123
  value = [value]
124
+
124
125
  for (let i = 0; i < value.length; i++) {
125
126
  entry += `\t\t<${key}:${key}>\n`;
126
127
 
127
128
  for (const nestedKey of Object.keys(value[i])) {
128
- const nestedValue = value[i][nestedKey];
129
-
129
+ let nestedValue = value[i][nestedKey];
130
130
  // Handle nested objects
131
131
  if (typeof nestedValue === 'object' && nestedValue !== null && !(nestedValue instanceof Date)) {
132
132
  entry += `\t\t\t<${key}:${nestedKey}>\n`;
@@ -136,6 +136,16 @@ class CoCreateSitemap {
136
136
  }
137
137
  entry += `\t\t\t</${key}:${nestedKey}>\n`;
138
138
  } else {
139
+ if (nestedKey === 'loc') {
140
+ if (!nestedValue.startsWith('https://') && !nestedValue.startsWith('http://') && !nestedValue.startsWith('{{$host}}')) {
141
+ nestedValue = `{{$host}}${nestedValue}`;
142
+ }
143
+ } else if (nestedKey === 'publication_date') {
144
+ nestedValue = new Date(nestedValue).toISOString().split('.')[0] + "Z";
145
+ } else {
146
+ nestedValue = this.encodeXML(nestedValue)
147
+ }
148
+
139
149
  entry += `\t\t\t<${key}:${nestedKey}>${nestedValue}</${key}:${nestedKey}>\n`;
140
150
  }
141
151
  }
@@ -143,6 +153,16 @@ class CoCreateSitemap {
143
153
  entry += `\t\t</${key}:${key}>\n`;
144
154
  }
145
155
  } else {
156
+ if (key === 'loc') {
157
+ if (!value.startsWith('https://') && !value.startsWith('http://')) {
158
+ value = `{{$host}}${value}`;
159
+ }
160
+ } else if (key === 'lastmod') {
161
+ value = new Date(file.modified.on).toISOString().split('.')[0] + "Z";
162
+ } else {
163
+ value = this.encodeXML(value)
164
+ }
165
+
146
166
  entry += `\t\t<${key}>${value}</${key}>\n`;
147
167
  }
148
168
  }
@@ -440,6 +460,17 @@ class CoCreateSitemap {
440
460
  }
441
461
 
442
462
  }
463
+
464
+ encodeXML(str) {
465
+ if (str)
466
+ return str
467
+ .replace(/&/g, '&amp;')
468
+ .replace(/</g, '&lt;')
469
+ .replace(/>/g, '&gt;')
470
+ .replace(/"/g, '&quot;')
471
+ .replace(/'/g, '&apos;');
472
+ }
473
+
443
474
  }
444
475
 
445
476
  module.exports = CoCreateSitemap;