-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnext-sitemap.config.js
More file actions
73 lines (65 loc) · 2.97 KB
/
next-sitemap.config.js
File metadata and controls
73 lines (65 loc) · 2.97 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
const fs = require('fs')
const path = require('path')
const { spawnSync } = require('child_process')
const grayMatter = require('gray-matter')
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.NEXT_PUBLIC_SITE_URL || 'https://docs.devguard.org',
generateRobotsTxt: true,
robotsTxtOptions: {
additionalSitemaps: [
`${process.env.NEXT_PUBLIC_SITE_URL || 'https://docs.devguard.org'}/api/sitemap.xml`,
],
},
transform: (_, p) => {
// check if the frontmatter of the page has a seo.robots of "noindex"
console.log(`Processing ${p} for sitemap...`)
try {
// read the file
const filePath = p === '/' ? 'index.mdx' : `${p.replace(/^\//, '')}.mdx`
let fullPath = path.join(process.cwd(), 'src', 'pages', filePath)
// check if directory
const stat = fs.statSync(fullPath)
if (stat.isDirectory()) {
// we need to read the index.mdx file in the directory
const indexPath = path.join(fullPath, 'index.mdx')
if (fs.existsSync(indexPath)) {
fullPath = indexPath
} else if (fs.existsSync(path.join(fullPath, 'index.md'))) {
fullPath = path.join(fullPath, 'index.md')
}
else if (fs.existsSync(path.join(process.cwd(), 'src', 'pages', `${filePath.replace(/\/$/, '')}.mdx`))) {
fullPath = path.join(process.cwd(), 'src', 'pages', `${filePath.replace(/\/$/, '')}.mdx`)
} else if (fs.existsSync(path.join(process.cwd(), 'src', 'pages', `${filePath.replace(/\/$/, '')}.md`))) {
fullPath = path.join(process.cwd(), 'src', 'pages', `${filePath.replace(/\/$/, '')}.md`)
} else {
console.warn(`No index.mdx or index.md found for directory ${fullPath}, skipping...`)
return null
}
}
if (!fs.existsSync(fullPath)) {
console.warn(`File ${fullPath} does not exist, skipping...`)
return null
}
const fileContent = fs.readFileSync(fullPath, 'utf-8')
const { data } = grayMatter(fileContent)
if (data.seo && data.seo.robots && data.seo.robots.includes('noindex')) {
console.log(`Page ${p} has noindex in frontmatter, skipping...`)
return null
}
let lastmod
try {
const result = spawnSync('git', ['log', '-1', '--format=%cI', '--', fullPath], { encoding: 'utf-8' })
const gitDate = result.stdout.trim()
if (gitDate) lastmod = gitDate
} catch (_) { }
return {
loc: p,
lastmod,
}
} catch (e) {
console.warn(`Error processing ${p} for sitemap: ${e.message}, skipping...`)
return null
}
},
}