forked from casetext/quill-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
209 lines (172 loc) · 3.8 KB
/
index.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
var cheerio = require('cheerio'),
escapeHtml = require('escape-html');
exports = module.exports = function(ops) {
return convert(ops).html();
};
exports.asDOM = convert;
var format = exports.format = {
block: {
image: function($, src) {
var img = $('<img>');
img.attr('src', src);
this.append(img);
}
},
inline: {
italic: function($) {
return $('<i>');
},
bold: function($) {
return $('<b>');
},
link: function($, href) {
return $('<a>').attr('href', href);
}
},
lineify: {
h1: function() {
this[0].name = 'h1';
},
h2: function() {
this[0].name = 'h2';
},
h3: function() {
this[0].name = 'h3';
},
bullet: {
group: function($) {
return $('<ul>');
},
line: function() {
this[0].name = 'li';
}
},
list: {
group: function($) {
return $('<ol>');
},
line: function() {
this[0].name = 'li';
}
}
}
};
function convert(ops) {
var $ = cheerio.load(''), group, line, el, activeInline, beginningOfLine;
function newLine() {
el = line = $('<p>');
$.root().append(line);
activeInline = {};
}
newLine();
for (var i = 0; i < ops.length; i++) {
var op = ops[i];
if (op.insert === 1) {
for (var k in op.attributes) {
if (format.block[k]) {
newLine();
applyStyles(op.attributes);
format.block[k].call(el, $, op.attributes[k]);
newLine();
}
}
} else {
var lines = escapeHtml(op.insert).split('\n');
if (isLinifyable(op.attributes)) {
// Some line-level styling (ie headings) is applied by inserting a \n
// with the style; the style applies back to the previous \n.
// There *should* only be one style in an insert operation.
for (var j = 1; j < lines.length; j++) {
for (var k in op.attributes) {
if (format.lineify[k]) {
var fn = format.lineify[k];
if (typeof fn == 'object') {
if (group && group.type != k) {
group = null;
}
if (!group && fn.group) {
group = {
el: fn.group($),
type: k,
distance: 0
};
$.root().append(group.el);
}
if (group) {
group.el.append(line);
group.distance = 0;
}
fn = fn.line;
}
fn.call(line, $, op.attributes[k]);
newLine();
break;
}
}
}
beginningOfLine = true;
} else {
for (var j = 0; j < lines.length; j++) {
if ((j > 0 || beginningOfLine) && group && ++group.distance >= 2) {
group = null;
}
applyStyles(op.attributes, ops[i+1] && ops[i+1].attributes);
el.append(lines[j]);
if (j < lines.length-1) {
newLine();
}
}
beginningOfLine = false;
}
}
}
return $;
function applyStyles(attrs, next) {
var first = [], then = [];
attrs = attrs || {};
var tag = el, seen = {};
while (tag[0]._format) {
seen[tag[0]._format] = true;
if (!attrs[tag[0]._format]) {
for (var k in seen) {
delete activeInline[k];
}
el = tag.parent();
}
tag = tag.parent();
}
for (var k in attrs) {
if (format.inline[k]) {
if (activeInline[k]) {
if (activeInline[k] != attrs[k]) {
// ie when two links abut
} else {
continue; // do nothing -- we should already be inside this style's tag
}
}
if (next && attrs[k] == next[k]) {
first.push(k); // if the next operation has the same style, this should be the outermost tag
} else {
then.push(k);
}
activeInline[k] = attrs[k];
}
}
first.forEach(apply);
then.forEach(apply);
function apply(fmt) {
var newEl = format.inline[fmt].call(null, $, attrs[fmt]);
newEl[0]._format = fmt;
el.append(newEl);
el = newEl;
}
}
}
function isLinifyable(attrs) {
for (var k in attrs) {
if (format.lineify[k]) {
return true;
}
}
return false;
}