-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-sitemap.config.cjs
More file actions
82 lines (68 loc) · 1.9 KB
/
Copy pathnext-sitemap.config.cjs
File metadata and controls
82 lines (68 loc) · 1.9 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
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const path = require('path');
/* eslint-enable @typescript-eslint/no-require-imports */
const pathLevelToPriority = {
0: 1,
1: 0.9,
2: 0.8,
3: 0.7,
};
const EXCLUDED_PATHS = ['/feed.xml'];
const getPathDepthLevel = (urlPath) => {
if (urlPath === '/') {
return 0;
}
return urlPath.split('/').length - 1;
};
function getContentDate(urlPath) {
const blogMatch = urlPath.match(/^\/blog\/([^/]+)$/);
const snippetMatch = urlPath.match(/^\/snippets\/([^/]+)$/);
let dir;
let slug;
if (blogMatch) {
dir = '_posts';
slug = blogMatch[1];
} else if (snippetMatch) {
dir = '_snippets';
slug = snippetMatch[1];
} else {
return null;
}
const filePath = path.join(process.cwd(), dir, `${slug}.md`);
try {
const content = fs.readFileSync(filePath, 'utf-8');
const frontmatter =
content.match(/^---\r?\n([\s\S]*?)\r?\n---/m)?.[1] ?? '';
const updateMatch = frontmatter.match(/^updateDate:\s*(.+)$/m);
const createMatch = frontmatter.match(/^createDate:\s*(.+)$/m);
const dateStr = updateMatch
? updateMatch[1].trim()
: createMatch
? createMatch[1].trim()
: null;
if (dateStr) {
return new Date(dateStr).toISOString();
}
} catch (error) {
if (error?.code !== 'ENOENT') {
console.error(`Error reading content date for ${urlPath}:`, error);
}
}
return null;
}
/** @type {import("next-sitemap").IConfig} */
module.exports = {
siteUrl: 'https://shramko.dev',
generateIndexSitemap: false,
exclude: EXCLUDED_PATHS,
transform: async (config, urlPath) => {
const contentDate = getContentDate(urlPath);
return {
loc: urlPath,
priority: pathLevelToPriority[getPathDepthLevel(urlPath)],
...(contentDate && { lastmod: contentDate }),
alternateRefs: config.alternateRefs ?? [],
};
},
};