-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
177 lines (152 loc) · 4.55 KB
/
Main.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
function getParsed() {
const value = document.getElementById('input').value;
return {
hasHeaders: document.querySelector('#hasHeader:checked'),
split: value.split(/\r?\n/),
value: value,
};
}
var htmlTimeout, mdTimeout;
function delayedGeneration() {
clearTimeout(htmlTimeout);
clearInterval(mdTimeout);
htmlTimeout = setTimeout(generateHtml, 100);
mdTimeout = setTimeout(generateMd, 1000);
}
function generateMd() {
function escapeMd(str) {
str = str.replace(/\*/g, '\\*');
str = str.replace(/}/g, '\\}');
str = str.replace(/{/g, '\\{');
str = str.replace(/\[/g, '\\[');
str = str.replace(/]/g, '\\]');
str = str.replace(/\(/g, '\\(');
str = str.replace(/\)/g, '\\)');
// str = str.replace(/_/g, '\\_');
// str = str.replace(/-/g, '\\-');
// str = str.replace(/\+/g, '\\+');
str = str.replace(/#/g, '\\#');
str = str.replace(/\./g, '\\.');
str = str.replace(/!/g, '\\!');
return str;
}
const mdPre = document.getElementById('mdContent');
mdPre.textContent = '';
const parsed = getParsed();
if (!parsed.value) {
return;
}
let output = '';
if (parsed.hasHeaders != undefined) {
const tds = parsed.split[0].replace(/\|/g, '').split(/\t/);
output += '| ';
var line = '| ';
tds.forEach(element => {
var escaped = escapeMd(element);
output += `${escaped} | `;
line += '-'.repeat(escaped.length) + ' | ';
});
output = output.trim();
output += `\r\n${line.trim()}`;
parsed.split.shift();
}
parsed.split.forEach(row => {
if (!row || row.trim().length < 1) return true;
var tds = row.split(/\t/);
output += '\r\n| ';
tds.forEach(element => {
output += `${escapeMd(element)} | `;
});
return true;
});
mdPre.textContent = output.trim();
}
function generateHtml() {
function convert(str) {
str = str.replace(/&/g, '&');
str = str.replace(/>/g, '>');
str = str.replace(/</g, '<');
str = str.replace(/"/g, '"');
str = str.replace(/'/g, ''');
return str;
}
const previewPanel = document.getElementById('previewPanel');
previewPanel.innerHTML = '';
const htmlPre = document.getElementById('htmlContent');
htmlPre.textContent = '';
const parsed = getParsed();
var output = '<table style="font-family: monospace;">';
if (parsed.hasHeaders != undefined) {
output += '\r\n <thead>\r\n <tr>';
const tds = parsed.split[0].split(/\t/);
tds.forEach(element => {
output += `\r\n\ <th>${convert(element)}</th>`;
});
output += '\r\n </tr>\r\n </thead>';
parsed.split.shift();
}
output += '\r\n <tbody>';
parsed.split.forEach(row => {
if (!row || row.trim().length < 1) return true;
var tds = row.split(/\t/);
output += '\r\n <tr>';
tds.forEach(element => {
output += `\r\n\ <td>${convert(element)}</td>`;
});
output += '\r\n </tr>';
return true;
});
output += '\r\n </tbody>';
output += '\r\n</table>';
previewPanel.innerHTML = output;
htmlPre.classList.remove('prettyprinted');
htmlPre.textContent = output;
// setTimeout(() => window.PR.prettyPrint(), 100);
}
function showCopied() {
if (showCopied.handler) {
clearTimeout(showCopied.handler);
}
const c = document.getElementById('copy-message');
c.style.opacity = 1;
showCopied.handler = setTimeout(() => (c.style.opacity = 0), 1000);
}
function copyHtmlCode() {
navigator.clipboard.writeText(document.getElementById('htmlContent').textContent).then(
function () {
showCopied();
},
function () {
alert('Failed to copy the text to clipboard!');
}
);
}
function copyMdCode() {
navigator.clipboard.writeText(document.getElementById('mdContent').textContent).then(
function () {
showCopied();
},
function () {
alert('Failed to copy the text to clipboard!');
}
);
}
function copyHtml() {
let element = document.getElementById('previewPanel');
window.getSelection().removeAllRanges();
let range = document.createRange();
range.selectNode(typeof element === 'string' ? document.getElementById(element) : element);
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
showCopied();
}
function clearAll() {
const previewPanel = document.getElementById('previewPanel');
previewPanel.innerHTML = '';
const htmlPre = document.getElementById('htmlContent');
htmlPre.textContent = '';
document.getElementById('input').value = '';
const mdPre = document.getElementById('mdContent');
mdPre.textContent = '';
}