-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-to-json.js
66 lines (57 loc) · 1.62 KB
/
html-to-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require("fs");
const path = require("path");
function getList(dl) {
let retArray = [];
dl.childNodes.forEach((cn) => {
if (cn.tagName.toLowerCase() != "dt") {
return;
}
if (
cn.childNodes.length > 0 &&
cn.childNodes[0].tagName.toLowerCase() == "a"
) {
let a = cn.childNodes[0];
retArray.push({ url: a.href, title: a.innerHTML });
} else if (cn.childNodes.length > 2) {
retArray.push(getLinkFolder(cn));
}
});
return retArray;
}
function getLinkFolder(dt) {
let folder = { title: "undefined", list: [] };
dt.childNodes.forEach((li) => {
if (li.tagName && li.tagName.toLowerCase() == "h3") {
folder.title = li.innerHTML;
} else if (li.tagName && li.tagName.toLowerCase() == "dl") {
folder.list = getList(li);
}
});
return folder;
}
function parseIt(inputDoc = "undefined") {
if (inputDoc == "undefined") {
inputDoc = "./test/Safari Bookmarks.html";
}
if (!fs.existsSync(inputDoc)) {
console.log(`${inputDoc} does not exist.`);
inputDoc = "./test/Safari Bookmarks.html";
console.log("switching to test bookmarks");
}
if (!fs.existsSync(inputDoc)) {
console.log(`${inputDoc} does not exist.`);
return;
}
let srcDocPath = path.resolve(__dirname, inputDoc);
let dom = new JSDOM(fs.readFileSync(srcDocPath, "utf8"));
let document = dom.window.document;
let dts = document.querySelectorAll("body > dt");
let categories = [];
dts.forEach((dt) => {
categories.push(getLinkFolder(dt));
});
return categories;
}
module.exports = parseIt;