Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
sudo: false
language: node_js
node_js:
- '0.12'
- 'node'
- 'stable'
- 6
- 8
- 9
- 10
cache:
directories:
- node_modules
notifications:
email:
on_success: never
on_failure: always
email: false
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';
var chalk = require('chalk');
var defaults = require('lodash/defaults');
var log = require('fancy-log');
var path = require('path');
var PluginError = require('plugin-error');
var through = require('through2');
var Vinyl = require('vinyl');
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');

var pluginName = 'gulp-sitemap';
var sitemap = require('./lib/sitemap');
const pluginName = 'gulp-sitemap';
const sitemap = require('./lib/sitemap');

module.exports = function (options) {
var config = defaults(options || {}, {
module.exports = function (options = {}) {
const config = defaults({}, options, {
changefreq: undefined,
fileName: 'sitemap.xml',
lastmod: null,
Expand All @@ -21,9 +21,9 @@ module.exports = function (options) {
spacing: ' ',
verbose: false
});
var entries = [];
var firstFile;
var msg;
const entries = [];
let firstFile;
let msg;

if (!config.siteUrl) {
msg = 'siteUrl is a required param';
Expand Down Expand Up @@ -58,15 +58,15 @@ module.exports = function (options) {
firstFile = file;
}

var entry = sitemap.getEntryConfig(file, config);
const entry = sitemap.getEntryConfig(file, config);
entries.push(entry);
callback();
},
function (callback) {
if (!firstFile) {
return callback();
}
var contents = sitemap.prepareSitemap(entries, config);
const contents = sitemap.prepareSitemap(entries, config);
if (options.verbose) {
msg = 'Files in sitemap: ' + entries.length;
log(pluginName, msg);
Expand All @@ -76,7 +76,7 @@ module.exports = function (options) {
cwd: firstFile.cwd,
base: firstFile.cwd,
path: path.join(firstFile.cwd, config.fileName),
contents: new Buffer(contents)
contents: Buffer.from(contents)
}));
callback();
});
Expand Down
62 changes: 32 additions & 30 deletions lib/sitemap.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
'use strict';
var defaults = require('lodash/defaults');
var find = require('lodash/find');
var includes = require('lodash/includes');
var multimatch = require('multimatch');
var slash = require('slash');
var pick = require('lodash/pick');

var header = [
const defaults = require('lodash/defaults');
const find = require('lodash/find');
const includes = require('lodash/includes');
const multimatch = require('multimatch');
const slash = require('slash');
const pick = require('lodash/pick');

const header = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
];

var headerHref = [
const headerHref = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
];

var footer = ['</urlset>'];
var allowedProperties = ['getLoc', 'lastmod', 'priority', 'changefreq', 'hreflang'];
const footer = ['</urlset>'];
const allowedProperties = ['getLoc', 'lastmod', 'priority', 'changefreq', 'hreflang'];

function getEntryConfig(file, siteConfig) {
var relativePath = file.relative;
var mappingsForFile = find(siteConfig.mappings, function(item) {
const relativePath = file.relative;
const mappingsForFile = find(siteConfig.mappings, function(item) {
return multimatch(relativePath, item.pages).length > 0;
}) || {};

var entry = defaults(
const entry = defaults(
{},
pick(mappingsForFile, allowedProperties),
pick(siteConfig, allowedProperties)
Expand All @@ -39,10 +39,12 @@ function getEntryConfig(file, siteConfig) {
}

//turn index.html into -> /
var relativeFile = relativePath.replace(/(\/|\\|^)index\.html?$/, '/');
if (relativeFile === '/') relativeFile = ''
let relativeFile = relativePath.replace(/(\/|\\|^)index\.html?$/, '/');
if (relativeFile === '/') {
relativeFile = '';
}
//url location. Use slash to convert windows \\ or \ to /
var adjustedFile = slash(relativeFile);
const adjustedFile = slash(relativeFile);
entry.loc = siteConfig.siteUrl + adjustedFile;
entry.file = adjustedFile;

Expand All @@ -58,37 +60,37 @@ function wrapTag(tag, value) {
*
* @param entry
* @param siteConfig
* @return {Array}
* @return {string}
*/
function processEntry(entry, siteConfig) {
var returnArr = [siteConfig.spacing + '<url>'];
const returnArr = [siteConfig.spacing + '<url>'];

var loc = entry.getLoc ? entry.getLoc(siteConfig.siteUrl, entry.loc, entry) : entry.loc;
const loc = entry.getLoc ? entry.getLoc(siteConfig.siteUrl, entry.loc, entry) : entry.loc;
returnArr.push(siteConfig.spacing + siteConfig.spacing + wrapTag('loc', loc));

var lastmod = entry.lastmod;
let lastmod = entry.lastmod;
if (lastmod) {
//format mtime to ISO (same as +00:00)
lastmod = new Date(lastmod).toISOString();
returnArr.push(siteConfig.spacing + siteConfig.spacing + wrapTag('lastmod', lastmod));
}

var changefreq = entry.changefreq;
const changefreq = entry.changefreq;
if (changefreq) {
returnArr.push(siteConfig.spacing + siteConfig.spacing + wrapTag('changefreq', changefreq));
}

var priority = entry.priority;
const priority = entry.priority;
if (priority || priority === 0) {
returnArr.push(siteConfig.spacing + siteConfig.spacing + wrapTag('priority', priority));
}

var hreflang = entry.hreflang;
const hreflang = entry.hreflang;
if (hreflang && hreflang.length > 0) {
var file = entry.file;
const file = entry.file;
hreflang.forEach(function(item) {
var href = item.getHref(siteConfig.siteUrl, file, item.lang, loc);
var tag = '<xhtml:link rel="alternate" hreflang="' + item.lang + '" href="' + href + '" />';
const href = item.getHref(siteConfig.siteUrl, file, item.lang, loc);
const tag = '<xhtml:link rel="alternate" hreflang="' + item.lang + '" href="' + href + '" />';
returnArr.push(siteConfig.spacing + siteConfig.spacing + tag);
});
}
Expand All @@ -98,7 +100,7 @@ function processEntry(entry, siteConfig) {
}

function prepareSitemap(entries, config) {
var entriesHref = entries.some(function(entry) {
const entriesHref = entries.some(function(entry) {
return entry && entry.hreflang && entry.hreflang.length;
});
return (entriesHref ? headerHref : header)
Expand All @@ -110,7 +112,7 @@ function prepareSitemap(entries, config) {
.toString();
}

var validChangefreqs = [
const VALID_CHANGE_FREQUENCIES = [
'always',
'hourly',
'daily',
Expand All @@ -125,7 +127,7 @@ function isChangefreqValid(changefreq) {
if (!changefreq) {
return true;
}
return includes(validChangefreqs, changefreq.toLowerCase());
return includes(VALID_CHANGE_FREQUENCIES, changefreq.toLowerCase());
}

exports.getEntryConfig = getEntryConfig;
Expand Down
Loading