-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
91 lines (82 loc) · 2.75 KB
/
index.js
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
83
84
85
86
87
88
89
90
91
'use strict';
const chalk = require('chalk');
const defaults = require('lodash/defaults');
const log = require('fancy-log');
const path = require('path');
const PluginError = require('plugin-error');
const through = require('through2');
const Vinyl = require('vinyl');
const pluginName = 'gulp-sitemap';
const sitemap = require('./lib/sitemap');
module.exports = function (options = {}) {
const config = defaults({}, options, {
changefreq: undefined,
fileName: 'sitemap.xml',
lastmod: null,
mappings: [],
newLine: '\n',
priority: undefined,
spacing: ' ',
verbose: false,
noindex: false
});
const entries = [];
let firstFile;
let msg;
if (!config.siteUrl) {
msg = 'siteUrl is a required param';
throw new PluginError(pluginName, msg);
}
if (options.changeFreq) {
msg = chalk.magenta('changeFreq') + ' has been deprecated. Please use ' + chalk.cyan('changefreq');
throw new PluginError(pluginName, msg);
}
// site url should have a trailing slash
if (config.siteUrl.slice(-1) !== '/') {
config.siteUrl = config.siteUrl + '/';
}
return through.obj(function (file, enc, callback) {
//we handle null files (that have no contents), but not dirs
if (file.isDirectory()) {
return callback(null, file);
}
if (file.isStream()) {
msg = 'Streaming not supported';
return callback(new PluginError(pluginName), msg);
}
//skip 404 file
if (/404\.html?$/i.test(file.relative)) {
return callback();
}
if(options.noindex){
const contents = file.contents.toString();
if (/<meta [^>]*?noindex/i.test(contents)) {
return callback();
}
}
if (!firstFile) {
firstFile = file;
}
const entry = sitemap.getEntryConfig(file, config);
entries.push(entry);
callback();
},
function (callback) {
if (!firstFile) {
return callback();
}
const contents = sitemap.prepareSitemap(entries, config);
if (options.verbose) {
msg = 'Files in sitemap: ' + entries.length;
log(pluginName, msg);
}
//create and push new vinyl file for sitemap
this.push(new Vinyl({
cwd: firstFile.cwd,
base: firstFile.cwd,
path: path.join(firstFile.cwd, config.fileName),
contents: Buffer.from(contents)
}));
callback();
});
};