-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtf-worker.js
76 lines (63 loc) · 3.01 KB
/
rtf-worker.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
/*
rtf-worker.js
---------
Author(s) : AM Douglas, Lois Atwood
License : BSD 3-clause Clear
URL : https://github.com/seoscribe/editor
---------
Copyright (c) 2018, SEO Scribe Ltd
All rights reserved.
*/
'use strict';
// listen for the postMessage from the text editor
// no need for a dispatcher, only one event to listen for
self.addEventListener('message', respondWithRTF);
// respond to postMessage by checking event obj for message data
// and returning the results of the conversion if we get the green light
function respondWithRTF (e) {
if (typeof e !== 'undefined' && !!e.data) {
return self.postMessage(convertHTMLToRTF(e.data.html));
}
}
// Borrowing the work of a Mr. James Greene:
// reference: http://jsfiddle.net/JamesMGreene/2b6Lc/
function convertHTMLToRTF (html) {
if (!html || !(typeof html === 'string' && html)) return null;
var tmpRichText, hasHyperlinks;
var richText = html.replace(/^\s{8}|\s $/mg, "").replace(/^\s /, "").replace(/[ ]{4}/g, " ");
// Singleton tags
richText = richText.replace(/<(?:hr)(?:\s [^>]*)?\s*[\/]?>/ig, "{\\pard \\brdrb \\brdrs \\brdrw10 \\brsp20 \\par}\n{\\pard\\par}\n");
richText = richText.replace(/<(?:br)(?:\s [^>]*)?\s*[\/]?>/ig, "{\\pard\\par}\n");
// Empty tags
richText = richText.replace(/<(?:p|div|section|article)(?:\s [^>]*)?\s*[\/]>/ig, "{\\pard\\par}\n");
richText = richText.replace(/<(?:[^>] )\/>/g, "");
// Hyperlinks
richText = richText.replace(
/<a(?:\s [^>]*)?(?:\s href=(["'])(?:javascript:void\(0?\);?|#|return false;?|void\(0?\);?|)\1)(?:\s [^>]*)?>/ig,
"{{{\n");
tmpRichText = richText;
richText = richText.replace(
/<a(?:\s [^>]*)?(?:\s href=(["'])(. )\1)(?:\s [^>]*)?>/ig,
"{\\field{\\*\\fldinst{HYPERLINK\n \"$2\"\n}}{\\fldrslt{\\ul\\cf1\n");
hasHyperlinks = richText !== tmpRichText;
richText = richText.replace(/<a(?:\s [^>]*)?>/ig, "{{{\n");
richText = richText.replace(/<\/a(?:\s [^>]*)?>/ig, "\n}}}");
// Start tags
richText = richText.replace(/<(?:b|strong)(?:\s [^>]*)?>/ig, "{\\b\n");
richText = richText.replace(/<(?:i|em)(?:\s [^>]*)?>/ig, "{\\i\n");
richText = richText.replace(/<(?:u|ins)(?:\s [^>]*)?>/ig, "{\\ul\n");
richText = richText.replace(/<(?:strike|del)(?:\s [^>]*)?>/ig, "{\\strike\n");
richText = richText.replace(/<sup(?:\s [^>]*)?>/ig, "{\\super\n");
richText = richText.replace(/<sub(?:\s [^>]*)?>/ig, "{\\sub\n");
richText = richText.replace(/<(?:p|div|section|article)(?:\s [^>]*)?>/ig, "{\\pard\n");
// End tags
richText = richText.replace(/<\/(?:p|div|section|article)(?:\s [^>]*)?>/ig, "\n\\par}\n");
richText = richText.replace(/<\/(?:b|strong|i|em|u|ins|strike|del|sup|sub)(?:\s [^>]*)?>/ig, "\n}");
// Strip any other remaining HTML tags [but leave their contents]
richText = richText.replace(/<(?:[^>] )>/g, "");
// Prefix and suffix the rich text with the necessary syntax
richText =
"{\\rtf1\\ansi\n" (hasHyperlinks ? "{\\colortbl\n;\n\\red0\\green0\\blue255;\n}\n" : "") richText
"\n}";
return richText;
}