Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created January 9, 2012 19:09
Show Gist options
  • Save wyattdanger/1584407 to your computer and use it in GitHub Desktop.
Save wyattdanger/1584407 to your computer and use it in GitHub Desktop.
quick utility
// bring in required modules
var fs = require('fs'),
util = require('util'),
jsdom = require('jsdom');
var output = "", texts;
// read sitemap.xml into string
fs.readFile('sitemap.xml', 'utf8', function(err, data) {
if (err) throw err;
// build DOM and bring in jQuery
jsdom.env(data, ['http://code.jquery.com/jquery-1.5.min.js'], function(err, window) {
if (err) throw err;
var $ = window.$;
// map over nodes for URLs
texts = $.map( $('url loc'), function(val, i) {
return $(val).text();
});
// alphabetize
texts.sort();
// build up output string
texts.forEach(function(text) {
output += text + "\n";
});
// write output string to file
var fname = util.format("url-list-%s.txt", Date.now());
fs.writeFile(fname, output, 'utf8', function() {
console.log(util.format("Wrote %s URLs to %s", texts.length, fname));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment