-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
142 lines (121 loc) · 4.15 KB
/
index.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as fs from "fs";
import * as cheerio from "cheerio";
type Entries = (Bookmark | Folder)[];
interface Bookmark {
type: "bookmark";
title: string;
url: string;
addDate: string | null;
lastModified: string | null;
icon: string | null;
}
interface Folder {
type: "folder";
title: string;
addDate: string | null;
lastModified: string | null;
contents: (Bookmark | Folder)[];
}
function isBookmark(node: cheerio.Cheerio, item: Bookmark | Folder): item is Bookmark {
return node.is("A") || item.type === "bookmark";
}
function isFolder(node: cheerio.Cheerio, item: Bookmark | Folder): item is Folder {
return node.is("H3") || node.children("A").length > 0 || item.type === "folder";
}
function parseNode($: cheerio.Root, element: cheerio.Cheerio): Entries {
let result: (Bookmark | Folder)[] = [];
element.children("DT").each((_, elem) => {
const node = $(elem);
const item = {} as Bookmark | Folder;
const titleElement = node.find("> A, > H3").first();
if (isBookmark(titleElement, item)) {
item.type = "bookmark";
item.title = titleElement.text();
item.url = titleElement.attr("href") || "";
item.addDate = titleElement.attr("add_date") || null;
item.lastModified = titleElement.attr("last_modified") || null;
item.icon = titleElement.attr("icon") || null;
} else if (isFolder(titleElement, item)) {
item.type = "folder";
item.title = titleElement.text();
item.addDate = titleElement.attr("add_date") || null;
item.lastModified = titleElement.attr("last_modified") || null;
}
const nextDl = node.children("DL");
if (isFolder(titleElement, item)) {
item.contents = parseNode($, nextDl);
}
if (Object.keys(item).length !== 0) {
result.push(item);
}
});
return result;
}
function removeFolderFromJSON(nodes: Entries, dirsToRemove: string[]): Entries {
const cp = structuredClone(nodes);
return cp.filter((node) => {
if (node.type === "folder") {
if (dirsToRemove.includes(node.title)) {
return false;
} else {
node.contents = removeFolderFromJSON(node.contents, dirsToRemove);
return node.contents.length > 0;
}
}
return true;
});
}
function pickUpFolderFromJSON(nodes: Entries, dirsToPickUp: string[]): Entries {
const cp = structuredClone(nodes);
return cp.filter((node) => {
if (node.type === "folder") {
if (dirsToPickUp.includes(node.title)) {
return true;
} else {
node.contents = pickUpFolderFromJSON(node.contents, dirsToPickUp);
return node.contents.length > 0;
}
}
return false;
});
}
function convertBookmarksToJSON(inputPath: string): Entries {
const rawBookmarks = fs.readFileSync(inputPath, "utf8");
const $ = cheerio.load(rawBookmarks);
const rootDl = $("DL").first();
const bookmarksJSON = parseNode($, rootDl);
return bookmarksJSON;
}
function writeJson(bookmarksJSON: Entries, outputPath: string): void {
fs.writeFile(outputPath, JSON.stringify(bookmarksJSON, null, 2), (err) => {
if (err) {
console.error("Error writing file:", err);
} else {
console.log(`Bookmarks converted to JSON successfully!: ${outputPath}`);
}
});
}
function grepUrls(bookmarksJSON: Entries): Set<string> {
const result = new Set<string>();
bookmarksJSON.forEach((node) => {
if (node.type === "bookmark") {
result.add(node.url);
} else if (node.type === "folder") {
const urls = grepUrls(node.contents);
urls.forEach((url) => result.add(url));
}
});
return result;
}
try {
const inputFile: string = "./imports/bookmarks_2024_07_28.html"; // HTML file to be converted
const outputFile: string = "./exports/bookmarks_2024_07_28.json"; // JSON file to be created
const bookmarkJson = convertBookmarksToJSON(inputFile);
const editedBookmark = removeFolderFromJSON(bookmarkJson, ["accounts"]);
writeJson(editedBookmark, outputFile);
const urlList = grepUrls(editedBookmark);
fs.writeFileSync("./exports/urls.txt", Array.from(urlList).join("\n"));
console.log("Bookmark JSON has been successfully created.");
} catch (error) {
console.error("Error processing the file:", error);
}