-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-sitemap.js
More file actions
28 lines (24 loc) · 888 Bytes
/
generate-sitemap.js
File metadata and controls
28 lines (24 loc) · 888 Bytes
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
const fs = require('fs'),
xml2js = require('xml2js'),
path = require('path'),
SitemapGenerator = require('advanced-sitemap-generator');
const sitemapPath = path.join(process.cwd(), 'static/sitemap.xml');
const parser = new xml2js.Parser();
const builder = new xml2js.Builder();
const generator = SitemapGenerator('http://localhost:3000', {
filepath: sitemapPath,
});
generator.on('done', () => {
fs.readFile(sitemapPath, function(err, data) {
parser.parseString(data, function(err, result) {
result.urlset.url.forEach(
(url) =>
(url.loc[0] = url.loc[0].replace('http://localhost:3000', 'https://codingforyou.net')) &&
url.loc[0].includes('fr') &&
(url.priority[0] = (parseFloat(url.priority[0]) + 0.1).toString())
);
fs.writeFileSync(sitemapPath, builder.buildObject(result));
});
});
});
generator.start();