@canopy-iiif/app 1.3.1 → 1.3.2
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/lib/build/sitemap.js +70 -10
- package/package.json +1 -1
package/lib/build/sitemap.js
CHANGED
|
@@ -3,6 +3,8 @@ const { logLine } = require('./log');
|
|
|
3
3
|
|
|
4
4
|
const DEFAULT_CHANGEFREQ = 'monthly';
|
|
5
5
|
const DEFAULT_PRIORITY = '0.5';
|
|
6
|
+
const MAX_URLS_PER_SITEMAP = 1000;
|
|
7
|
+
const SITEMAP_INDEX_BASENAME = 'sitemap.xml';
|
|
6
8
|
|
|
7
9
|
function escapeXml(value) {
|
|
8
10
|
return String(value || '')
|
|
@@ -44,12 +46,7 @@ function collectAbsoluteUrls(iiifRecords, pageRecords) {
|
|
|
44
46
|
return Array.from(urls.values()).sort((a, b) => a.localeCompare(b));
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
const urls = collectAbsoluteUrls(iiifRecords, pageRecords);
|
|
49
|
-
if (!urls.length) {
|
|
50
|
-
logLine('• No URLs to write to sitemap', 'yellow');
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
49
|
+
function buildUrlsetXml(urls) {
|
|
53
50
|
const rows = urls.map((loc) => {
|
|
54
51
|
const escapedLoc = escapeXml(loc);
|
|
55
52
|
return [
|
|
@@ -60,16 +57,79 @@ async function writeSitemap(iiifRecords, pageRecords) {
|
|
|
60
57
|
' </url>',
|
|
61
58
|
].join('\n');
|
|
62
59
|
});
|
|
63
|
-
|
|
60
|
+
return [
|
|
64
61
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
65
62
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
66
63
|
rows.join('\n'),
|
|
67
64
|
'</urlset>',
|
|
68
65
|
'',
|
|
69
66
|
].join('\n');
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function buildSitemapIndexXml(entries) {
|
|
70
|
+
const rows = entries.map((entry) => {
|
|
71
|
+
const escapedLoc = escapeXml(entry.loc);
|
|
72
|
+
return [' <sitemap>', ` <loc>${escapedLoc}</loc>`, ' </sitemap>'].join('\n');
|
|
73
|
+
});
|
|
74
|
+
return [
|
|
75
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
76
|
+
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
|
|
77
|
+
rows.join('\n'),
|
|
78
|
+
'</sitemapindex>',
|
|
79
|
+
'',
|
|
80
|
+
].join('\n');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function chunkList(list, chunkSize) {
|
|
84
|
+
const chunks = [];
|
|
85
|
+
if (!Array.isArray(list) || chunkSize <= 0) return chunks;
|
|
86
|
+
for (let i = 0; i < list.length; i += chunkSize) {
|
|
87
|
+
chunks.push(list.slice(i, i + chunkSize));
|
|
88
|
+
}
|
|
89
|
+
return chunks;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function cleanupLegacySitemaps() {
|
|
93
|
+
let entries;
|
|
94
|
+
try {
|
|
95
|
+
entries = await fsp.readdir(OUT_DIR);
|
|
96
|
+
} catch (_) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const deletions = entries
|
|
100
|
+
.filter((name) => /^sitemap-\d+\.xml$/i.test(name))
|
|
101
|
+
.map((name) =>
|
|
102
|
+
fsp
|
|
103
|
+
.unlink(path.join(OUT_DIR, name))
|
|
104
|
+
.catch(() => {})
|
|
105
|
+
);
|
|
106
|
+
await Promise.all(deletions);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function writeSitemap(iiifRecords, pageRecords) {
|
|
110
|
+
const urls = collectAbsoluteUrls(iiifRecords, pageRecords);
|
|
111
|
+
if (!urls.length) {
|
|
112
|
+
await cleanupLegacySitemaps();
|
|
113
|
+
logLine('• No URLs to write to sitemap', 'yellow');
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
await cleanupLegacySitemaps();
|
|
117
|
+
|
|
118
|
+
const chunks = chunkList(urls, MAX_URLS_PER_SITEMAP);
|
|
119
|
+
const indexEntries = [];
|
|
120
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
121
|
+
const fileName = `sitemap-${i + 1}.xml`;
|
|
122
|
+
const dest = path.join(OUT_DIR, fileName);
|
|
123
|
+
await fsp.writeFile(dest, buildUrlsetXml(chunks[i]), 'utf8');
|
|
124
|
+
indexEntries.push({ loc: absoluteUrl(fileName) });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const indexDest = path.join(OUT_DIR, SITEMAP_INDEX_BASENAME);
|
|
128
|
+
await fsp.writeFile(indexDest, buildSitemapIndexXml(indexEntries), 'utf8');
|
|
129
|
+
logLine(
|
|
130
|
+
`✓ Wrote sitemap index (${chunks.length} files, ${urls.length} urls total)`,
|
|
131
|
+
'cyan'
|
|
132
|
+
);
|
|
73
133
|
}
|
|
74
134
|
|
|
75
135
|
module.exports = { writeSitemap };
|