-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookmarks2md.html
395 lines (350 loc) · 12.3 KB
/
bookmarks2md.html
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!--
Version 1.0.2 (26/02/2024)
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bookmarks to Markdown</title>
<meta name="description" content="Bookmarks to MD">
<meta name="author" content="Daniel BP">
<style>
/* The colors of the page were adjusted to display "hidden" white objects */
body {
font: 10pt Sans-Serif;
}
p {
font: 9pt Serif;
}
.outputs {
font: 11pt Serif;
}
table{
font: 9pt Serif;
}
textarea{
font: 10pt Monospace;
width: 100%;
height: 512px;
}
.right {
text-align: right;
}
.left {
width: 100%;
text-align: left;
}
.highlight {
background-color: yellow;
}
.green {
color: green;
font-weight: bold;
}
.red {
color: red;
font-weight: bold;
}
.bold {
font-weight: bold;
}
.columns {
column-count: 2;
column-rule-style: solid;
column-rule-width: 1px;
-webkit-column-width: 500px; /* Chrome, Safari, Opera */
-moz-column-width: 500px; /* Firefox */
column-width: 500px;
background-color: #f0f0e1;
padding: 20px;
}
.columnBlock {
-webkit-column-break-inside: avoid; /* Chrome, Safari, Opera */
page-break-inside: avoid; /* Firefox */
break-inside: avoid; /* IE 10+ */
}
.hidden {
display: none;
}
</style>
<!-- Your style-sheets go here -->
<!-- End of your page style-sheets-->
</head>
<body>
<div>
<h1>Bookmarks to Markdown</h1>
This tool converts exported bookmarks HTML file from Browsers (Chrome, Firefox and others) to Markdown (.md) format.<BR><BR>
It runs from a plain HTML file, from any browser:<BR>
<UL>
<LI>no installation</LI>
<LI>no data sent to any server - local processing</LI>
<LI>no extensions</LI>
<LI>no command line</LI>
<LI>mobile friendly</LI>
<LI>no Python, Node, PHP or runtimes required</LI>
</UL>
The tool is not working as expected? Contact the developer <a href="https://www.danbp.org">Daniel BP</a>.<BR><BR>
<div class="columns">
<div class="columnBlock"><h3>1. Select and load one or more HTML bookmark files</h3>
<input type="file" id="fileinput" multiple/>
<input type="button" value="Reload the files" onClick="document.getElementById('fileinput').dispatchEvent(eVent);"/>
</div>
<div class="columnBlock"><h3>2. Convert the bookmarks</h3>
<input type="button" value="Convert" onClick="processHTML();"/>
<UL>
<li>Source files: <span id="fileCount" class="bold">N/A</span></li>
<li>Folders: <span id="folderCount" class="bold">N/A</span></li>
<li>Bookmarks: <span id="bookmarkCount" class="bold">N/A</span></li>
</UL>
</div>
<div class="columnBlock"><h3>3. Export the Markdown</h3>
<input type="button" value="Save as a file" onClick="exportMD();"/>
<input type="button" value="Copy to clipboard" onClick="copyMD();"/>
</div>
</div>
</div>
<BR><BR>
<div class="hidden" id="b2mdMDTitle">
<h1 id="b2mdMDHead">Final MD Markup</h1>
<HR>
<textarea id="b2mdMD" class="outputs"></textarea>
<HR>
</div>
<div class="hidden" id="b2mdOutputsTitle">
<h1 id="b2mdOutputHead">Source HTML bookmarks</h1>
<HR>
<div id="b2mdOutputs" class="outputs"></div>
</div>
<script>
// JavaScript extra debug code part 1
//--------------------------------------------------------------------------------------------------------
"use strict"; //Prevents the use of undeclared variables
console.time("Runtime"); //Start the script timer
//--------------------------------------------------------------------------------------------------------
</script>
<script >
//
//-------------------------------------------------------------------------------------------------------------
// BOOKMARKS TO MD CONVERSION ENGINE
//-------------------------------------------------------------------------------------------------------------
// This Software was Developed by Daniel Brooke Peig
// In case of any doubts please contact the developer at www.danbp.org
// Copyright (c) 2023 Daniel Brooke Peig
//-------------------------------------------------------------------------------------------------------------
//
//
//
//Turn on for verbose output for debug purposes
let debug = false;
/**
* Converts the Bookmarks Notation to MarkDown
* Please note that this function requires a DOM object and functions to work
* @param {object} source - The source DOM element containing the DT/DL tags
* @param {int} level - The depth level of the block
* @returns {string} - Formatted MD
*/
function b2md(source, level = 0) {
dest = "";
//Get links and titles first
for (let index in source.children) {
//Title
if (source.children[index].tagName == "H1") {
if (debug) console.log("Title: " + source.children[index].innerHTML);
dest += "# " + source.children[index].innerHTML + "\n";
//dest += "----\n";
}
//Links
else if (source.children[index].tagName == "DT") {
if (source.children[index].childElementCount > 0) {
if (source.children[index].children[0].tagName == "A") {
if (debug) console.log("Link: " + source.children[index].children[0].innerHTML);
dest +=
indent(level) +
"[" +
source.children[index].children[0].innerHTML +
"](" +
source.children[index].children[0].href +
")\n";
}
}
}
}
//Get sub-folders
let foundHeader = false;
for (let index in source.children) {
if (source.children[index].tagName == "DT") {
//This is a sub-folder folder header
if (source.children[index].children[0].tagName == "H3") {
if (debug)
console.log("Sub-folder level " + (level + 1) + " header: " + source.children[index].children[0].innerHTML);
dest += indentHeader(level) + source.children[index].children[0].innerHTML + "\n";
foundHeader = true;
}
//This sub-folder has contents
if (source.children[index].childElementCount > 1)
if (source.children[index].children[1].tagName == "DL") {
if (foundHeader == true) {
level++;
foundHeader = false;
}
if (debug) console.log("Getting internal content from level " + level);
// Let's get the content from the sub-folder
dest += b2md(source.children[index].children[1], level);
level--;
}
}
//This is just a container, move down
if (source.children[index].tagName == "DL") {
if (debug) console.log("Nothing to do in this level, let's go to the next...");
dest += b2md(source.children[index], level);
}
}
return dest;
}
/**
* Creates indentation for links
* @param {int} level - The depth level of the block
* @returns {string} - Formatted indentation markup
*/
function indent(level) {
indentation = "";
for (i = 0; i < level; i++) indentation += "";
return indentation + "- ";
}
/**
* Creates indentation for headers
* @param {int} level - The depth level of the block
* @returns {string} - Formatted indentation markup
*/
function indentHeader(level) {
indentation = "##";
for (i = 0; i < level; i++) indentation += "#";
return indentation + " ";
}
</script>
<script>
//
//-------------------------------------------------------------------------------------------------------------
// USER INTERFACE FOR THE PAGE
//-------------------------------------------------------------------------------------------------------------
// This Software was Developed by Daniel Brooke Peig
// In case of any doubts please contact the developer at www.danbp.org
// Copyright (c) 2023 Daniel Brooke Peig
//-------------------------------------------------------------------------------------------------------------
//
//
//
var originalTitle = document.title;
var fileCount = 0; //Keep track of the amount of files
var processedReports = false; //Check if the report was processed or not
var exportTitle = "";
function readMultiFile(evt) {
//Clear the statistics
document.getElementById('fileCount').innerHTML= "N/A";
document.getElementById('folderCount').innerHTML= "N/A";
document.getElementById('bookmarkCount').innerHTML= "N/A";
//Clear the HTML output placeholder
document.getElementById('b2mdOutputs').innerHTML= "";
document.getElementById('b2mdOutputsTitle').className = "hidden";
document.getElementById('b2mdOutputHead').innerHTML = "Source HTML bookmarks";
//Clear the output placeholder
document.getElementById('b2mdMD').innerHTML= "";
document.getElementById('b2mdMDTitle').className = "hidden";
document.getElementById('b2mdMDHead').innerHTML = "Final Markdown";
//Count the files
fileCount = evt.target.files.length;
//Tracks the file index
var fileIndex = 0;
//Files object
var files = evt.target.files;
if(fileCount > 16) alert("Maximum 16 files at once.")
else {
if (files) {
//Display the output box
document.getElementById('b2mdOutputsTitle').className = "";
for (var i=0, f; f=files[i]; i++) {
var r = new FileReader();
if(exportTitle == "") exportTitle = f.name;
r.onload = (function(f) {
return function(e) {
var contents = e.target.result;
fileIndex++;
//Create an object holder for the file and then append to the main div object
var outputDiv = document.createElement('div');
outputDiv.id = 'b2mdOutput'+fileIndex;
outputDiv.innerHTML = contents;
outputDiv.className = "reportBlock";
document.getElementById('b2mdOutputs').appendChild(outputDiv);
var outputHR = document.createElement('hr');
outputHR.className = "noprint";
document.getElementById('b2mdOutputs').appendChild(outputHR);
};
})(f);
r.readAsText(f);
}
} else {
alert("Failed to load files.");
fileCount = 0;
}
}
processedSources = false;
}
// Load the file
document.getElementById('fileinput').addEventListener('change', readMultiFile, false);
// Create the change event
var eVent = new Event('change');
function processHTML(){
//Process each block(file)
for(var bIndex=1; bIndex<=fileCount; bIndex++) {
//Set the processed reports flag
processedSources = false;
if(document.getElementById('b2mdOutput'+bIndex).innerHTML.length < 200) {alert("No file contents loaded. Please reload the file.");}
else if(document.getElementById('b2mdOutput'+bIndex).innerHTML.indexOf("Bookmarks")<0){
alert("Sorry, this is not a valid bookmark export file.");
}
else if(document.getElementById('b2mdOutput'+bIndex).innerHTML.indexOf("b2md") > 0){
if(bIndex == 1) alert("This document was already converted. Please reload the file if you want to convert it again.");
}
//Performs the conversions
else {
document.getElementById('b2mdMD').innerHTML += b2md(document.getElementById('b2mdOutput'+bIndex))
//Display the output box
document.getElementById('b2mdMDTitle').className = "";
//Calculates statistics
document.getElementById('fileCount').innerHTML= fileCount;
let folderCount = 0;
let bookmarkCount = 0;
let lines = document.getElementById('b2mdMD').innerHTML.split("\n");
for (line of lines){
if (line.search(/- \[/gm) != -1) bookmarkCount++;
if (line.search(/\#\#/gm) != -1) folderCount++;
}
document.getElementById('folderCount').innerHTML= folderCount;
document.getElementById('bookmarkCount').innerHTML= bookmarkCount;
} //End of else block (after error check)
} //End of block loop
} //End of processHTML
function exportMD(){
const link = document.createElement("a");
const content = document.getElementById("b2mdMD").innerHTML;
const file = new Blob([content], { type: 'text/plain' });
link.href = URL.createObjectURL(file);
link.download = exportTitle.replace(".html",".md").replace(".htm",".md");
link.click();
URL.revokeObjectURL(link.href);
}
function copyMD() {
// Copy the text inside the text field
navigator.clipboard.writeText(document.getElementById("b2mdMD").innerHTML);
}
</script>
<!-- End of your JavaScript code -->
<script>
// JavaScript extra debug code part 2
//--------------------------------------------------------------------------------------------------------
console.log("Developed by Daniel Brooke Peig (www.danbp.org)"); //Add final notice
console.timeEnd("Runtime"); //Stop the script timer
//--------------------------------------------------------------------------------------------------------
</script>
</body>
</html>