forked from euangoddard/clipboard2markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard2markdown.js
230 lines (205 loc) · 6.61 KB
/
clipboard2markdown.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
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
(function () {
'use strict';
// http://pandoc.org/README.html#pandocs-markdown
var pandoc = [
{
filter: 'h1',
replacement: function (content, node) {
return '\n\n# ' + content + '\n\n';
}
},
{
filter: 'h2',
replacement: function (content, node) {
return '\n\n## ' + content + '\n\n';
}
},
{
filter: 'h3',
replacement: function (content, node) {
return '\n\n### ' + content + '\n\n';
}
},
{
filter: 'sup',
replacement: function (content) {
return '^' + content + '^';
}
},
{
filter: 'sub',
replacement: function (content) {
return '~' + content + '~';
}
},
{
filter: 'br',
replacement: function () {
return '\\\n';
}
},
{
filter: 'hr',
replacement: function () {
return '\n\n* * *\n\n';
}
},
{
filter: ['em', 'i', 'cite', 'var'],
replacement: function (content) {
return '*' + content + '*';
}
},
{
filter: function (node) {
var hasSiblings = node.previousSibling || node.nextSibling;
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
var isCodeElem = node.nodeName === 'CODE' ||
node.nodeName === 'KBD' ||
node.nodeName === 'SAMP' ||
node.nodeName === 'TT';
return isCodeElem && !isCodeBlock;
},
replacement: function (content) {
return '`' + content + '`';
}
},
{
filter: function (node) {
return node.nodeName === 'A' && node.getAttribute('href');
},
replacement: function (content, node) {
var url = node.getAttribute('href');
var titlePart = node.title ? ' "' + node.title + '"' : '';
if (content === url) {
return '<' + url + '>';
} else if (url === ('mailto:' + content)) {
return '<' + content + '>';
} else {
return '[' + content + '](' + url + titlePart + ')';
}
}
},
{
filter: 'li',
replacement: function (content, node) {
content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ');
var prefix = '- ';
var parent = node.parentNode;
if (/ol/i.test(parent.nodeName)) {
var index = Array.prototype.indexOf.call(parent.children, node) + 1;
prefix = index + '. ';
while (prefix.length < 4) {
prefix += ' ';
}
}
return prefix + content;
}
}
];
// http://pandoc.org/README.html#smart-punctuation
var escape = function (str) {
return str.replace(/[\u2018\u2019\u00b4]/g, "'")
.replace(/[\u201c\u201d\u2033]/g, '"')
.replace(/[\u2212\u2022\u00b7\u25aa]/g, '-')
.replace(/[\u2013\u2015]/g, '--')
.replace(/\u2014/g, '---')
.replace(/\u2026/g, '...')
.replace(/[ ]+\n/g, '\n')
.replace(/\s*\\\n/g, '\\\n')
.replace(/\s*\\\n\s*\\\n/g, '\n\n')
.replace(/\s*\\\n\n/g, '\n\n')
.replace(/\n-\n/g, '\n')
.replace(/\n\n\s*\\\n/g, '\n\n')
.replace(/\n\n\n*/g, '\n\n')
.replace(/[ ]+$/gm, '')
.replace(/^\s+|[\s\\]+$/g, '');
};
var convert = function (str) {
var turndownService = new TurndownService({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
strongDelimiter: '**'
});
turndownService.addRule('HandleCiteAndVar', {
filter: ['em', 'i', 'cite', 'var'],
replacement: function (content) {
return '*' + content + '*';
}
})
/*
Copying text from a website often converts <strong> tags to span elements with
an inline style that contains the font-weight attribute:
https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Syntax
Treat as bold If the font-weight value is either "bold" or a number >= 600
*/
turndownService.addRule('HandleBoldTextWithoutProperTags', {
filter: function (node) {
if (node.nodeName === 'SPAN' && node.style["font-weight"]){
var fontWeight = node.style["font-weight"];
if (fontWeight === "bold") { return true; };
if (!isNaN(fontWeight) && parseInt(fontWeight) >= 600) { return true; };
};
return false;
},
replacement: function (content, node) {
return '**' + content + '**';
}
},)
var gfm = turndownPluginGfm.gfm;
turndownService.use(gfm);
console.log(str);
return escape(turndownService.turndown(str));
//return escape(toMarkdown(str, { converters: pandoc, gfm: true }));
}
var insert = function (myField, myValue) {
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
sel.select()
} else {
if (myField.selectionStart || myField.selectionStart == "0") {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var beforeValue = myField.value.substring(0, startPos);
var afterValue = myField.value.substring(endPos, myField.value.length);
myField.value = beforeValue + myValue + afterValue;
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.focus()
} else {
myField.value += myValue;
myField.focus()
}
}
};
// http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser
document.addEventListener('DOMContentLoaded', function () {
var info = document.querySelector('#info');
var pastebin = document.querySelector('#pastebin');
var output = document.querySelector('#output');
var wrapper = document.querySelector('#wrapper');
document.addEventListener('keydown', function (event) {
if (event.ctrlKey || event.metaKey) {
if (String.fromCharCode(event.which).toLowerCase() === 'v') {
pastebin.innerHTML = '';
pastebin.focus();
info.classList.add('hidden');
wrapper.classList.add('hidden');
}
}
});
pastebin.addEventListener('paste', function () {
setTimeout(function () {
var html = pastebin.innerHTML;
var markdown = convert(html);
// output.value = markdown;
insert(output, markdown);
wrapper.classList.remove('hidden');
output.focus();
output.select();
}, 200);
});
});
})();