-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (160 loc) · 4.92 KB
/
index.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
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
let input_obj = document.querySelector("[paste]");
input_obj.addEventListener("input",function(){
process(null);
})
let output = document.querySelector("[op]");
let reset = document.querySelector("[reset]");
reset.addEventListener("click",resetPaste);
function openFileUpload() {
var fileInput = document.getElementById('fileInput');
fileInput.click();
}
let uploadButton = document.querySelector("[uploadedFile]");
uploadButton.addEventListener("click",extractText);
function resetPaste(){
input_obj.value="";
output.value="";
copy.innerHTML="Copy Result"
}
function extractTextSub() {
return new Promise(function(resolve, reject) {
var fileInput = document.getElementById("fileInput");
fileInput.onchange = function(event) {
var file = fileInput.files[0];
if (!file) {
reject("No file selected.");
return;
}
var reader = new FileReader();
reader.onload = function(event) {
var text = event.target.result;
resolve(text);
};
reader.onerror = function(event) {
reject(event.target.error);
};
reader.readAsText(file);
};
});
}
function extractText(){
extractTextSub().then(function(text) {
process(text);
input_obj.value = text;
}).catch(function(error) {
console.error(error);
});
}
let copy = document.querySelector("[copy]")
copy.addEventListener("click",copyContent);
async function copyContent(){
const text = output.value;
await navigator.clipboard.writeText(text);
copy.innerHTML="Copied!"
setTimeout(()=>{copy.innerHTML="Copy Result"},1000);
}
let spaces =[[1,1]];
function process(fileInput){
copy.innerHTML="Copy Result"
pass_len = fileInput==null?document.querySelector("[paste]").value.split('\n'):fileInput.split('\n');
// console.log(pass_len.length);
let res = "";
let v = [];
for(let i=0; i<pass_len.length; i++){
let s = pass_len[i];
let k = "";
let j = 0;
for(; j<s.length-1; j++){
if(s[j+1]==' '){
k += s[j];
break;
}
k += s[j];
}
let isHeading = false;
if(/^#+$/.test(k)){
v.push(s);
isHeading = true;
}
res += s+'\n';
if(isHeading == true){
res += "[Index](#index)\n \n";
}
}
let op = ``;
op+= `# Index\n`;
for(let i=0; i<v.length; i++){
let isFirst = false;
let filt = ``;
for(let k=0; k<v[i].length; k++){
if(v[i][k]!='#' && v[i][k]!=' '){
isFirst = true;
}
if(isFirst == true){
filt += v[i][k];
}
}
let cnt_Hash = 0;
for(let u=0; u<v[i].length; u++){
if(v[i][u]=='#'){
cnt_Hash++;
}
else{
break;
}
}
if(cnt_Hash<spaces[spaces.length-1][1]){
while(cnt_Hash<spaces[spaces.length-1][1]){
spaces.pop();
}
}
let last_index_hashes = spaces[spaces.length-1][1];
let last_index_length = spaces[spaces.length-1][0];
if(cnt_Hash>last_index_hashes){
spaces.push([last_index_length=last_index_length+2,cnt_Hash]);
}
let lengths_to_be_added_before = last_index_length;
for(let i=0; i<lengths_to_be_added_before; i++){
op+= ' ';
}
op += `- [`+filt+`]`;
let n=``;
n+=`#`;
let index = 0;
// console.log(v[i]);
for(;index<v[i].length;index++){
if(v[i][index]!='#' && v[i][index]!=' ') break;
}
while(index<v[i].length){
while(v[i][index]!=' '){
if(index>=v[i].length){
break;
}
if(v[i][index]>='A' && v[i][index]<='Z'){
n += String.fromCharCode(v[i][index].charCodeAt(0)+('a'.charCodeAt(0)-'A'.charCodeAt(0)));
} else if(v[i][index]>='a' && v[i][index]<='z'){
if(index>=v[i].length){
break;
}
n += v[i][index];
} else if(v[i][index]=='-' || !v[i][index].match(/[^\w\s]/)){
n += v[i][index];
}
index++;
}
while(index<v[i].length && !(v[i][index]>='A' && v[i][index]<='Z') && !(v[i][index]>='a' && v[i][index]<='z') && (!v[i][index].match(/[^\w\s]/))){
index++;
}
if(index<v[i].length){
n += '-';
}
}
op += `(`+n+`)\n`;
}
op += `\n`+res;
// console.log(1111);
// console.log(`result is `+res);
const parser = new DOMParser();
const html = parser.parseFromString(op, 'text/html');
output.value=op;
}