-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
362 lines (307 loc) · 11.1 KB
/
main.js
File metadata and controls
362 lines (307 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { i18n, plugin, format } from "@shevky/base";
const PLUGIN_NAME = "shevky-sitemap";
const PLUGIN_VERSION = "0.0.4";
const SITEMAP_FILENAME = "sitemap.xml";
const escape = (value) => format.escape(value ?? "");
const lastmod = (value) => format.lastMod(value) ?? new Date().toISOString();
const parseDate = (value) => (value ? Date.parse(String(value)) || 0 : 0);
const sortByLastmodDesc = (a, b) => {
const aDate = a?.lastmod ? Date.parse(String(a.lastmod)) : NaN;
const bDate = b?.lastmod ? Date.parse(String(b.lastmod)) : NaN;
if (!Number.isNaN(aDate) && !Number.isNaN(bDate) && aDate !== bDate) {
return bDate - aDate;
}
return (a?.loc || "").localeCompare(b?.loc || "");
};
const resolvePaginationSegment = (lang, config) => {
const segmentConfig = config?.content?.pagination?.segment ?? {};
if (typeof segmentConfig[lang] === "string" && segmentConfig[lang].trim()) {
return segmentConfig[lang].trim();
}
if (typeof segmentConfig[i18n.default] === "string") {
return segmentConfig[i18n.default].trim();
}
return "page";
};
const normalizeCollectionTypeValue = (value) =>
typeof value === "string" ? value.trim().toLowerCase() : "";
const dedupeCollectionItems = (items) => {
if (!Array.isArray(items) || items.length === 0) return items;
const seen = new Map();
const order = [];
items.forEach((item) => {
const id = item?.id;
if (!id) {
order.push(item);
return;
}
const existingIndex = seen.get(id);
const hasSeriesTitle = Boolean(item?.seriesTitle);
if (existingIndex == null) {
seen.set(id, order.length);
order.push(item);
return;
}
const existing = order[existingIndex];
const existingHasSeries = Boolean(existing?.seriesTitle);
if (hasSeriesTitle && !existingHasSeries) {
order[existingIndex] = item;
}
});
return order;
};
const buildContentUrl = (canonical, lang, slug) => {
if (typeof canonical === "string" && canonical.trim()) {
return format.ensureDirectoryTrailingSlash(canonical.trim());
}
const normalizedLang = lang ?? i18n.default;
const slugSegment = slug ? `/${slug}` : "/";
if (normalizedLang !== i18n.default) {
const langPath = `/${normalizedLang}${slugSegment}`.replace(/\/+/, "/");
return format.ensureDirectoryTrailingSlash(langPath);
}
const normalizedSlug = slugSegment.replace(/\/+/, "/");
return format.ensureDirectoryTrailingSlash(normalizedSlug);
};
const resolveUrl = (value, ctx) =>
format.resolveUrl(value, ctx?.config?.identity?.url ?? "");
class SitemapBuilder {
async build(ctx) {
const pluginConfig = ctx.config.get(PLUGIN_NAME) ?? {};
const sitemapFilename =
typeof pluginConfig.sitemapFilename === "string" &&
pluginConfig.sitemapFilename.trim().length > 0
? pluginConfig.sitemapFilename.trim()
: SITEMAP_FILENAME;
const includeCollections = Boolean(ctx?.config?.seo?.includeCollections);
const entries = await this._collectContentEntries(ctx);
const collectionEntries = includeCollections
? this._collectCollectionEntries(ctx)
: [];
const combined = [...entries, ...collectionEntries];
if (!combined.length) {
return;
}
const entryByLoc = this._mergeEntries(combined);
const xml = this._renderSitemap(entryByLoc);
await this._writeSitemap(ctx, xml, entryByLoc.length, sitemapFilename);
}
async _collectContentEntries(ctx) {
const contentFiles = ctx?.contentFiles ?? [];
if (!contentFiles.length) {
return [];
}
const pages = ctx?.pages ?? {};
const includePaging = Boolean(ctx?.config?.seo?.includePaging);
const urls = [];
for (const file of contentFiles) {
if (!file?.isValid || file?.isDraft || !file?.isPublished) {
continue;
}
const canonical =
file.canonical ?? buildContentUrl(null, file.lang, file.slug);
const absoluteLoc = resolveUrl(canonical, ctx);
const updated = file.updated ?? file.date;
const baseLastmod = lastmod(updated);
urls.push({ loc: absoluteLoc, lastmod: baseLastmod });
if (
includePaging &&
(file.template === "collection" || file.template === "home")
) {
const langCollections = pages[file.lang] ?? {};
const key =
typeof file?.header?.listKey === "string"
? file.header.listKey
: file?.header?.slug;
const allItems =
key && Array.isArray(langCollections[key])
? langCollections[key]
: [];
const pageSizeSetting = ctx?.config?.content?.pagination?.pageSize ?? 5;
const pageSize = pageSizeSetting > 0 ? pageSizeSetting : 5;
const totalPages = Math.max(
1,
pageSize > 0 ? Math.ceil(allItems.length / pageSize) : 1,
);
if (totalPages > 1) {
const segment = resolvePaginationSegment(file.lang, ctx?.config);
const baseSlug = (file.slug ?? "").replace(/\/+$/, "");
let latestTimestamp = null;
if (Array.isArray(allItems)) {
allItems.forEach((item) => {
if (!item) return;
const ts = parseDate(item.updated ?? item.date);
if (ts && (latestTimestamp == null || ts > latestTimestamp)) {
latestTimestamp = ts;
}
});
}
const listingLastmod =
latestTimestamp != null
? lastmod(new Date(latestTimestamp))
: baseLastmod;
for (let pageIndex = 2; pageIndex <= totalPages; pageIndex += 1) {
const pageSlug = baseSlug
? `${baseSlug}/${segment}-${pageIndex}`
: `${segment}-${pageIndex}`;
let canonicalOverride;
const canonicalSource =
typeof file?.header?.canonical === "string"
? file.header.canonical
: file.canonical;
if (typeof canonicalSource === "string" && canonicalSource.trim()) {
const trimmed = canonicalSource.trim().replace(/\/+$/, "");
canonicalOverride = `${trimmed}/${segment}-${pageIndex}/`;
}
const pageCanonical = buildContentUrl(
canonicalOverride,
file.lang,
pageSlug,
);
const pageAbsoluteLoc = resolveUrl(pageCanonical, ctx);
urls.push({ loc: pageAbsoluteLoc, lastmod: listingLastmod });
}
}
}
}
return urls;
}
_collectCollectionEntries(ctx) {
const urls = [];
const collectionsConfig = ctx?.config?.content?.collections ?? {};
const pages = ctx?.pages ?? {};
const configKeys = Object.keys(collectionsConfig).filter(
(key) => key !== "includeContentFile",
);
for (const configKey of configKeys) {
const entry = collectionsConfig[configKey];
if (!entry || typeof entry !== "object") {
continue;
}
const slugPattern =
entry.slugPattern && typeof entry.slugPattern === "object"
? entry.slugPattern
: {};
const rawTypes = Array.isArray(entry.types) ? entry.types : null;
const types = rawTypes
? rawTypes
.map((value) =>
normalizeCollectionTypeValue(
typeof value === "string" ? value : "",
),
)
.filter(Boolean)
: null;
if (!types || !types.length) {
continue;
}
const languages = Object.keys(pages);
for (const lang of languages) {
const langCollections = pages[lang] ?? {};
const langSlugPattern =
typeof slugPattern[lang] === "string" ? slugPattern[lang] : null;
const collectionKeys = Object.keys(langCollections);
for (const key of collectionKeys) {
const sourceItems = langCollections[key] ?? [];
if (!Array.isArray(sourceItems) || sourceItems.length === 0) {
continue;
}
const items = dedupeCollectionItems(sourceItems);
if (items.length === 0) {
continue;
}
const hasMatchingType = items.some((entryItem) =>
types.includes(normalizeCollectionTypeValue(entryItem.type)),
);
if (!hasMatchingType) {
continue;
}
const slug =
langSlugPattern && langSlugPattern.includes("{{key}}")
? langSlugPattern.replace("{{key}}", key)
: (langSlugPattern ?? key);
const canonical = buildContentUrl(null, lang, slug);
const absoluteLoc = resolveUrl(canonical, ctx);
let latestTimestamp = null;
items.forEach((item) => {
if (!item) return;
const ts = parseDate(item.updated ?? item.date);
if (ts && (latestTimestamp == null || ts > latestTimestamp)) {
latestTimestamp = ts;
}
});
const entryLastmod =
latestTimestamp != null
? lastmod(new Date(latestTimestamp))
: lastmod();
urls.push({ loc: absoluteLoc, lastmod: entryLastmod });
}
}
}
return urls;
}
_mergeEntries(entries) {
const entryByLoc = new Map();
entries.forEach((entry) => {
if (!entry || !entry.loc) return;
const key = entry.loc;
const existing = entryByLoc.get(key);
if (!existing) {
entryByLoc.set(key, entry);
return;
}
const existingDate = existing.lastmod
? Date.parse(String(existing.lastmod))
: null;
const incomingDate = entry.lastmod
? Date.parse(String(entry.lastmod))
: null;
if (
incomingDate != null &&
!Number.isNaN(incomingDate) &&
(existingDate == null ||
Number.isNaN(existingDate) ||
incomingDate > existingDate)
) {
entryByLoc.set(key, entry);
}
});
return Array.from(entryByLoc.values()).sort(sortByLastmodDesc);
}
_renderSitemap(entries) {
const urlset = entries
.map((entry) => {
const parts = [" <url>", ` <loc>${escape(entry.loc)}</loc>`];
if (entry.lastmod) {
parts.push(` <lastmod>${escape(entry.lastmod)}</lastmod>`);
}
parts.push(" </url>");
return parts.join("\n");
})
.join("\n");
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<?xml-stylesheet type="text/xsl" href="/assets/sitemap.xsl"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
urlset,
"</urlset>",
"",
].join("\n");
}
async _writeSitemap(ctx, xml, count, sitemapFilename) {
const relativePath = sitemapFilename;
const targetPath = ctx.path.combine(ctx.paths.dist, relativePath);
await ctx.directory.create(ctx.path.name(targetPath));
await ctx.file.write(targetPath, xml);
ctx.log.debug(`[${PLUGIN_NAME}] Sitemap has been created.`);
}
}
const sitemapBuilder = new SitemapBuilder();
/** @type {import("@shevky/base").PluginHooks} */
const hooks = {
[plugin.hooks.CONTENT_READY]: async function (ctx) {
await sitemapBuilder.build(ctx);
},
};
const PLUGIN = { name: PLUGIN_NAME, version: PLUGIN_VERSION, hooks };
export default PLUGIN;