forked from netlify-labs/netlify-plugin-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sitemap.js
More file actions
57 lines (55 loc) · 1.73 KB
/
make_sitemap.js
File metadata and controls
57 lines (55 loc) · 1.73 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
const fs = require('fs')
const util = require('util')
const path = require('path')
const mkdirp = require('mkdirp')
const sm = require('sitemap')
const globby = require('globby')
module.exports = async function makeSitemap(opts = {}) {
const { distPath, fileName, homepage, exclude, prettyURLs, failPlugin } = opts
const htmlFiles = `${distPath}/**/**.html`
const excludeFiles = (exclude || []).map((filePath) => {
return `!${filePath.replace(/^!/, '')}`
})
const lookup = [ htmlFiles ].concat(excludeFiles)
const paths = await globby(lookup)
const urls = paths.map(file => {
let urlPath = file.startsWith(distPath) ? file.replace(distPath, '') : distPath
if (prettyURLs) {
urlPath = urlPath.replace(/\/index\.html$/, '').replace(/\.html$/, '')
}
return {
url: urlPath,
changefreq: 'weekly',
priority: 0.8,
lastmodrealtime: true,
lastmodfile: file,
}
})
const options = {
hostname: `${homepage.replace(/\/$/, '')}/`,
cacheTime: 600000, // 600 sec cache period
urls,
}
// Creates a sitemap object given the input configuration with URLs
const sitemap = sm.createSitemap(options)
// Generates XML
try {
await util.promisify(sitemap.toXML.bind(sitemap))()
} catch (error) {
failBuild('Could not generate XML sitemap', { error })
}
// Gives you a string containing the XML data
const xml = sitemap.toString()
// write sitemap to file
const sitemapFileName = fileName || 'sitemap.xml'
const sitemapFile = path.resolve(distPath, sitemapFileName)
// Ensure dist path
await mkdirp(distPath)
// Write sitemap
await util.promisify(fs.writeFile)(sitemapFile, xml)
// Return info
return {
sitemapPath: sitemapFileName,
sitemap: sitemap
}
}