forked from eungjun-yi/copy-as-markdown-quot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
251 lines (202 loc) · 6.97 KB
/
background.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var get_selection = function() {
var selection = document.getSelection();
function domVisitor(node, functionName) {
functionName(node);
node = node.firstChild;
while (node) {
domVisitor(node, functionName);
node = node.nextSibling;
}
}
function getSelectionRepresentation() {
var textRepresentation = {
type: 'text',
value: selection.toString()
};
if (!selection.rangeCount) {
return textRepresentation;
}
// Grap the HTML
var container = document.createElement("div");
for (var i = 0, len = selection.rangeCount; i < len; ++i) {
container.appendChild(selection.getRangeAt(i).cloneContents());
}
// The HTML part is empty: but it could well be that the browser's
// HTML to text engine did a better job!
if (container.innerHTML.replace(/\s/g,"") === "") {
return textRepresentation;
}
// Make all links absolute
domVisitor(container, function (node) {
if (node.tagName !== 'A') {
return;
}
node.href = node.href;
});
return {
type: 'html',
value: container.innerHTML,
};
}
// Do the rest
var node = selection.getRangeAt(0).startContainer;
var uri = node.baseURI || document.documentURI;
var parent = node.parentElement;
var whiteSpace = (parent && window.getComputedStyle(parent)['white-space']);
var index;
var ext;
var is_code = function(elem) {
if (!elem) return false;
// Is the element monospace?
if (window.getComputedStyle(elem)['white-space'].toLowerCase() == 'pre') {
return true;
}
// Is the element generated by CodeMirror?
if (elem.className.toLowerCase().split(' ').indexOf('codemirror') >= 0) {
return true;
}
return false;
}
var pre = is_code(parent);
var get_frag = function(parent) {
var frag, sibling, nephew;
if (!parent) {
return null;
}
frag = parent.id || parent.name;
if (frag) {
return frag;
}
sibling = parent.previousSibling;
while(sibling) {
frag = sibling.id || sibling.name;
if (frag) {
return frag;
}
nephew = sibling.children && sibling.children[0];
frag = nephew && (nephew.id || nephew.name);
if (frag) {
return frag;
}
sibling = sibling.previousSibling;
}
}
var fileName;
var orig_frag;
var frag;
// Remove the fragment from the url and find the better one only if the
// original one was not semantically significant.
index = uri.lastIndexOf('#');
orig_frag = index >= 0 ? uri.substring(index + 1) : null;
if (!orig_frag || orig_frag.indexOf('/') < 0) { // Assume the fragment is siginificant if it contains '/'.
uri = index >= 0 ? uri.substring(0, index) : uri;
while(!frag && parent) {
frag = get_frag(parent);
parent = parent.parentElement;
}
}
// Get extension from the url
index = uri.lastIndexOf('/');
fileName = index >= 0 ? uri.substring(index + 1) : '';
index = fileName.lastIndexOf('.');
ext = index >= 0 ? fileName.substring(index + 1) : '';
if (frag) {
uri += '#' + frag;
}
return {
selection: getSelectionRepresentation(),
uri: uri,
pre: pre,
ext: ext
};
}
function toCleanMarkdown(html) {
var validTags = [
'p', 'br', 'hr', 'b', 'pre', 'img', 'code', 'blockquote', 'ol', '', 'h1', 'h2', 'h3',
'h4', 'h5', 'a', 'strong', 'em', 'a', 'i', 'em', 'ul', 'li',
];
return toMarkdown(html, {
converters: [
{
filter: 'div',
replacement: function (content) {
return '\n\n' + content + '\n\n';
}
},
{
filter: function (node) {
return validTags.indexOf(
node.nodeName.toLowerCase()
) === -1;
},
replacement: function (content) {
return content;
}
},
]
});
}
function toMarkdownQuote(markdown) {
return markdown.split('\n').map(function (line) {
return '> ' + line;
}).join('\n');
}
function toMarkdownCodeBlock(markdown, extension) {
return [
'> ```' + extension + '\n',
markdown,
'> ```\n',
].join('');
}
function dxClean(markdown) {
markdown = markdown.replace(/!\[Expanded]\(.*\)!\[Collapsed]\(.*\) /g, "");
markdown = markdown.replace(/DX\.Tabs\.init\('dxTab\d+'\);/g, "");
markdown = markdown.replace(/^Tip\n\n/gm, "> #### Tip\n> ");
markdown = markdown.replace(/^Note\n\n/gm, "> #### Note\n> ");
markdown = markdown.replace(/^Important\n\n/gm, "> #### Important\n> ");
markdown = markdown.replace(/^Show Me\n\n/gm, "> #### Show Me\n> ");
markdown = markdown.replace(/^C#\n\nVB\n\n<pre>[\S\s]*?<\/pre>\n\n<pre>[\S\s]*?<\/pre>\n\nC#/gm, "C#");
markdown = markdown.replace(/^C#\n\n/gm, "#### C#\n\n");
markdown = markdown.replace(/^VB\n\n/gm, "#### VB\n\n");
markdown = markdown.replace(/!\[]\(\/HelpResource\.ashx\?/g, "![Image](https://documentation.devexpress.com/HelpResource.ashx?");
markdown = markdown.replace(/<pre>/g, "```");
markdown = markdown.replace(/<\/pre>/g, "```");
markdown = markdown.replace(/^Xml\n\n```[\S\s]*?```\n\nXml/gm, "#### XML");
markdown = markdown.replace(/^#### XML\n\n```/gm, "#### XML\n\n```xml\n");
markdown = markdown.replace(/^#### C#\n\n```/gm, "#### C#\n\n```csharp\n");
markdown = markdown.replace(/^#### VB\n\n```/gm, "#### VB\n\n```vbnet\n");
return markdown;
}
var copy_as_markdown_quot = function (args) {
chrome.tabs.executeScript( {
code: "(" + get_selection + ")();"
}, function(selections) {
var selection = selections[0].selection;
var uri = selections[0].uri;
var pre = selections[0].pre;
var ext = selections[0].ext;
var result = selection.type === 'html'
? dxClean(toCleanMarkdown(selection.value))
: selection.value;
if (pre) {
result = toMarkdownCodeBlock(result, ext);
}
result = result + '\n>\n> -- ' + uri;
copyTextToClipboard(result);
});
};
function copyTextToClipboard(text) {
var copyFrom, body;
copyFrom = document.createElement("textarea");
copyFrom.textContent = text;
body = document.getElementsByTagName('body')[0];
body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
body.removeChild(copyFrom);
}
chrome.contextMenus.create({
title: "Copy DX Help as Markdown",
contexts: ['selection'],
onclick: copy_as_markdown_quot
});