-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaste.js
59 lines (44 loc) · 2.72 KB
/
paste.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
// jshint strict: false
// Taken from medium editor: https://github.com/yabwe/medium-editor/blob/master/src/js/extensions/paste.js
/**
* pasteUtils -
* namespaced utility methods for cleaning paste data
* @access protected
*/
const pasteUtils = {
createReplacements () {
return [
// Remove anything but the contents within the BODY element
[new RegExp(/^[\s\S]*<body[^>]*>\s*|\s*<\/body[^>]*>[\s\S]*$/g), ''],
// cleanup comments added by Chrome when pasting html
[new RegExp(/<!--StartFragment-->|<!--EndFragment-->/g), ''],
// Trailing BR elements
[new RegExp(/<br>$/i), ''],
// replace two bogus tags that begin pastes from google docs
[new RegExp(/<[^>]*docs-internal-guid[^>]*>/gi), ''],
[new RegExp(/<\/b>(<br[^>]*>)?$/gi), ''],
// un-html spaces and newlines inserted by OS X
[new RegExp(/<span class="Apple-converted-space">\s+<\/span>/g), ' '],
[new RegExp(/<br class="Apple-interchange-newline">/g), '<br>'],
// replace google docs italics+bold with a span to be replaced once the html is inserted
[new RegExp(/<span[^>]*(font-style:italic;font-weight:(bold|700)|font-weight:(bold|700);font-style:italic)[^>]*>/gi), '<span class="replace-with italic bold">'],
// replace google docs italics with a span to be replaced once the html is inserted
[new RegExp(/<span[^>]*font-style:italic[^>]*>/gi), '<span class="replace-with italic">'],
//[replace google docs bolds with a span to be replaced once the html is inserted
[new RegExp(/<span[^>]*font-weight:(bold|700)[^>]*>/gi), '<span class="replace-with bold">'],
// replace manually entered b/i/a tags with real ones
[new RegExp(/<(\/?)(i|b|a)>/gi), '<$1$2>'],
// replace manually a tags with real ones, converting smart-quotes from google docs
[new RegExp(/<a(?:(?!href).)+href=(?:"|”|“|"|“|”)(((?!"|”|“|"|“|”).)*)(?:"|”|“|"|“|”)(?:(?!>).)*>/gi), '<a href="$1">'],
// Newlines between paragraphs in html have no syntactic value,
// but then have a tendency to accidentally become additional paragraphs down the line
[new RegExp(/<\/p>\n+/gi), '</p>'],
[new RegExp(/\n+<p/gi), '<p'],
// Microsoft Word makes these odd tags, like <o:p></o:p>
[new RegExp(/<\/?o:[a-z]*>/gi), ''],
// Microsoft Word adds some special elements around list items
[new RegExp(/<!\[if !supportLists\]>(((?!<!).)*)<!\[endif]>/gi), '$1']
];
}
};
export default pasteUtils;