Skip to content

Instantly share code, notes, and snippets.

@aronanda
Created September 16, 2020 01:54
Show Gist options
  • Save aronanda/0150e9729465dc96bcecd628bbd0adac to your computer and use it in GitHub Desktop.
Save aronanda/0150e9729465dc96bcecd628bbd0adac to your computer and use it in GitHub Desktop.
Convert Netscape bookmark export files from HTML to JSON using Node.js
const fs = require('fs')
const path = require('path')
const cheerio = require('cheerio')
const inputFile = process.env.INPUT || process.argv[2] || 'bookmarks.html'
const outputFile = process.env.OUTPUT || process.argv[3] || 'bookmarks.json'
const inputFilePath = path.resolve(inputFile)
const outputFilePath = path.resolve(outputFile)
fs.readFile(inputFilePath, { encoding: 'utf8' }, (error, data) => {
if (error)
return console.error(error)
const $ = cheerio.load(data)
function parseTerm(element, out) {
const item = {}
if (element.name === 'dt') {
parseTerm($(element).children(':not(p)').first().get()[0], out)
} else if (element.name === 'h3') {
item.title = $(element).text()
item.type = 'folder'
item.updated = $(element).attr('last_modified')
item.children = []
out.push(item)
parseList($(element).next(), item.children)
} else if (element.name === 'a') {
item.title = $(element).text()
item.type = 'link'
item.added = $(element).attr('add_date')
item.href = $(element).attr('href')
item.icon = $(element).attr('icon')
out.push(item)
}
}
function parseList(list, out) {
list.children(':not(p)').each(function (index) {
parseTerm(this, out)
})
}
const out = []
parseList($('dl').first(), out)
fs.writeFile(outputFilePath, JSON.stringify(out, null, 2), error => {
if (error)
return console.error(error)
console.log('Success!')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment