Skip to content

Instantly share code, notes, and snippets.

@vasanthv
Forked from Sennahoi/extract.js
Created March 15, 2024 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasanthv/b915685cad4350415f98176170be5fb0 to your computer and use it in GitHub Desktop.
Save vasanthv/b915685cad4350415f98176170be5fb0 to your computer and use it in GitHub Desktop.
Extract bookmarks from a netscape bookmark file with node.js
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