This repository was archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport.js
More file actions
83 lines (79 loc) · 2.55 KB
/
import.js
File metadata and controls
83 lines (79 loc) · 2.55 KB
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
var mammoth = require('mammoth');
var TurndownService = require('turndown');
var detectUpload = function(){
document.getElementById("base64").value = "";
var x = document.getElementById("docupload");
if ('files' in x) {
if (x.files.length != 0) {
file = x.files[0];
if ('name' in file) {
var ext = file.name.split('.').pop();
if (ext == "doc" || ext == "docx") {
readFileInputEventAsArrayBuffer(function(arrayBuffer) {
mammoth.convertToHtml({arrayBuffer: arrayBuffer})
.then(function(result){
var html = result.value; // The generated HTML
var messages = result.messages; // Any messages, such as warnings during conversion)
/*if (messages != "") { //Sometimes causes weird alerts
alert(messages);
}*/
var count = html.split('base64,').length - 1;
var array = [];
var indexTracker = 0;
for (var i = 0; i < count; i++){
var startIndex = html.indexOf('base64,', indexTracker) + 7;
var endIndex = html.indexOf('"', startIndex);
var base64 = html.slice(startIndex, endIndex);
array.push(base64);
indexTracker = endIndex;
}
document.getElementById("base64").value = array.toString();
//HTML to Markdown
var count = 0;
var turndownService = new TurndownService({headingStyle: 'atx'});
turndownService.addRule('image', {
filter: 'img',
replacement: function (content) {
count += 1;
return '';
}
})
var markdown = turndownService.turndown(html);
editor.setMarkdown(markdown);
});
})
} else if (ext == "md") {
readFileInputEventAsText(function(text) {
editor.setMarkdown(text);
})
} else {
alert("Please select a valid file!");
}
}
}
}
else {
if (x.value == "") {
alert("Please select a valid file!");
} else {
alert("This function is not supported by your browser!")
}
}
}
function readFileInputEventAsArrayBuffer(callback) {
var reader = new FileReader();
reader.onload = function(loadEvent) {
var arrayBuffer = loadEvent.target.result;
callback(arrayBuffer);
};
reader.readAsArrayBuffer(file);
}
function readFileInputEventAsText(callback) {
var reader = new FileReader();
reader.onload = function(loadEvent) {
var text = loadEvent.target.result;
callback(text);
};
reader.readAsText(file);
}
module.exports = {detectUpload: detectUpload};