-
-
Save vasanthv/b915685cad4350415f98176170be5fb0 to your computer and use it in GitHub Desktop.
Extract bookmarks from a netscape bookmark file with node.js
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 cheerio = require("cheerio"), | |
fs = require("fs"); | |
fs.readFile('bookmarks.html', "utf-8", function read(err, data) { | |
if (err) { | |
throw err; | |
} | |
var $ = cheerio.load(data); | |
$("a").each(function(index, a) { | |
var $a = $(a); | |
var title = $a.text(); | |
var url = $a.attr("href"); | |
var categories = getCategories($a); | |
console.log(title, url, categories); | |
}); | |
}); | |
function getCategories($a) { | |
var $node = $a.closest("DL").prev(); | |
var title = $node.text() | |
if ($node.length > 0 && title.length > 0) { | |
return [title].concat(getCategories($node)); | |
} else { | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment