Created
February 8, 2016 10:13
-
-
Save osv/9266c5d98d1c95b111bd to your computer and use it in GitHub Desktop.
gulp sitemap task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ISO_3166 language map. http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html | |
var LANGUAGES = { | |
en: 'en', | |
ru: 'ru', | |
sv: 'si', | |
fi: 'fi', | |
tr: 'tr', | |
el: 'gr' | |
}; | |
var URLS = { | |
'/': { | |
changefreq: 'daily', // default weekly | |
priority: 0.9, | |
}, | |
'/about': {}, | |
'/affiliate': {}, | |
'/affiliate_terms': {}, | |
'/bitcoin': {}, | |
'/bonus': {}, | |
'/faq': {}, | |
'/gamelist': {}, | |
'/gamelist/1': {}, // hits | |
'/gamelist/allgames': {}, | |
'/payment-methods': {}, | |
'/play-safe': {}, | |
'/promo': { | |
priority: 0.9, | |
changefreq: 'daily' | |
}, | |
'/provability-fair-games': {}, | |
'/support': {}, | |
'/terms-and-conditions': {}, | |
}; | |
module.exports = { | |
languages: LANGUAGES, | |
urls: URLS, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build sitemap for 7bit | |
// See https://support.google.com/webmasters/answer/2620865?hl=ru | |
'use strict'; | |
var gulp = require('gulp'), | |
plugins = require('gulp-load-plugins')({ | |
pattern: ['*'], | |
lazy: false | |
}), | |
fs = require('fs'), | |
_ = require('underscore'), | |
builder = require('xmlbuilder'); | |
var BASE = 'https://EXAMPLE.com', | |
buildDir = plugins.path.resolve('./build'), | |
SITEMAPXML = buildDir + '/sitemap.xml', | |
config = require('../sitemap.config.js'), | |
URLS = config.urls, // sitemap urls | |
LANGUAGES = config.languages; // ISO3166 languages map | |
gulp.task('sitemap', [], function() { | |
try {fs.mkdirSync(buildDir);} catch (e) {} | |
var xml = builder.create('urlset', { version: '1.0', encoding: 'UTF-8' }) | |
.att({ | |
'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9', | |
'xmlns:xhtml': 'http://www.w3.org/1999/xhtml' | |
}); | |
xmlUrls(xml, URLS); | |
// per language urls | |
_.each(LANGUAGES, function(lang) { | |
xmlUrls(xml, URLS, lang); | |
}); | |
var sitemap = xml.end({ pretty: true, indent: ' ', newline: '\n' }); | |
fs.writeFileSync(SITEMAPXML, sitemap); | |
}); | |
function xmlUrls(xml, urls, language) { | |
_.each(urls, function(prop, url) { | |
var languagePart = (language ? '/' + language : ''), | |
fullUrl = BASE + languagePart + url, | |
xUrl = xml.ele('url'); | |
xUrl.ele('loc', fullUrl); | |
xUrl.ele('changefreq', prop.changefreq || 'weekly'); | |
if (prop.priority) { | |
xUrl.ele('priority', prop.priority || 0.5); | |
} | |
_.each(LANGUAGES, function(ISO3166, nextLang) { | |
if (nextLang !== language) { | |
var langUrl = BASE + '/' + nextLang + url; | |
xUrl.ele('xhtml:link') | |
.att({ | |
rel: 'alternate', | |
hreflang: ISO3166, | |
href: langUrl, | |
}); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It produce: