Created
June 23, 2016 20:50
-
-
Save oscarmorrison/1e6fa5f3648c4004f5398d192c0fc24f to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var filePath = 'js/data/data.json'; | |
var outputPath = 'sitemap.xml'; | |
var date = new Date().toISOString(); | |
var header = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n'; | |
var baseUrl = 'http://getfreestuff.co/'; | |
fs.readFile(filePath, 'utf8', function(err, data){ | |
var json = JSON.parse(data); | |
var urls = []; | |
for(var entry in json){ | |
urls.push(baseUrl+'#'+entry); | |
} | |
generateSiteMap(header, baseUrl, urls, outputPath); | |
}); | |
function generateSiteMap(header, baseUrl, urls, outputPath){ | |
var outputFile = "" ; | |
outputFile += header; | |
outputFile += tag('url',tag('loc',baseUrl)+tag('lastmod',date))+'\n'; | |
for(var url in urls){ | |
outputFile += tag('url',tag('loc',urls[url])+tag('changefreq','weekly'))+'\n'; | |
} | |
outputFile += '</urlset>'; | |
console.log(outputFile); | |
fs.writeFile(outputPath, outputFile); | |
} | |
function tag(tag, data){ | |
return '<'+tag+'>'+data+'</'+tag+'>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment