-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnext-sitemap.config.js
More file actions
58 lines (56 loc) · 1.77 KB
/
next-sitemap.config.js
File metadata and controls
58 lines (56 loc) · 1.77 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
/** @type {import('next-sitemap').IConfig} */
import fs from 'fs';
import path from 'path';
const config = {
siteUrl: process.env.SITE_URL || 'https://muetab.com',
generateRobotsTxt: true,
generateIndexSitemap: false,
exclude: ['/server-sitemap.xml'],
robotsTxtOptions: {
policies: [
{
userAgent: '*',
allow: '/',
},
],
},
additionalPaths: async (config) => {
const result = [];
// Blog posts for deriving tags & pagination counts
const blogDir = path.join(process.cwd(), 'content', 'blog');
let files = [];
try {
files = fs.readdirSync(blogDir).filter((f) => f.endsWith('.md') || f.endsWith('.mdx'));
} catch {}
const posts = [];
const tags = new Set();
for (const file of files) {
const raw = fs.readFileSync(path.join(blogDir, file), 'utf8');
const fmMatch = /---([\s\S]*?)---/.exec(raw);
if (fmMatch) {
const front = fmMatch[1];
const tagLineMatches = front.match(/tags:\s*\[[^\]]*\]/i);
if (tagLineMatches) {
const inner = tagLineMatches[0].replace(/tags:\s*\[/i, '').replace(/\]$/, '');
inner
.split(',')
.map((t) => t.trim().replace(/^['\"]|['\"]$/g, ''))
.filter(Boolean)
.forEach((t) => tags.add(t.toLowerCase()));
}
}
const slug = file.replace(/\.(md|mdx)$/i, '');
posts.push({ slug });
}
const PAGE_SIZE = 9;
const totalPages = Math.max(1, Math.ceil(posts.length / PAGE_SIZE));
for (let p = 2; p <= totalPages; p++) {
result.push(await config.transform(config, `/blog/page/${p}`));
}
for (const tag of tags) {
result.push(await config.transform(config, `/blog/tag/${tag}`));
}
return result;
},
};
export default config;