-
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathformatter.js
More file actions
252 lines (223 loc) · 6.39 KB
/
formatter.js
File metadata and controls
252 lines (223 loc) · 6.39 KB
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
252
var _ = require('underscore');
var _s = require('underscore.string');
var he = require('he');
var helper = require('./helper');
function formatText(elem, options) {
var text = elem.raw;
text = he.decode(text, options.decodeOptions);
if (options.isInPre) {
return text;
} else {
return helper.wordwrap(elem.trimLeadingSpace ? _s.lstrip(text) : text, options);
}
}
function formatImage(elem, options) {
if (options.ignoreImage) {
return '';
}
var result = '', attribs = elem.attribs || {};
if (attribs.alt) {
result += he.decode(attribs.alt, options.decodeOptions);
if (attribs.src) {
result += ' ';
}
}
if (attribs.src) {
result += '[' + attribs.src + ']';
}
return (result);
}
function formatVideo(elem, options) {
if (options.ignoreVideo) {
return '';
}
var result = '', attribs = elem.attribs || {};
if (attribs.src) {
result += '[' + attribs.src + ']';
} else if (elem.children.length > 0) {
_.each(elem.children, function(el) {
var elAttribs = el.attribs || {};
if (el.type === 'tag' && el.name.toLowerCase() === 'source') {
result += '[' + el.attribs.src + ']';
}
});
}
return (result);
}
function formatLineBreak(elem, fn, options) {
return '\n' + fn(elem.children, options);
}
function formatParagraph(elem, fn, options) {
return fn(elem.children, options) + '\n\n';
}
function formatHeading(elem, fn, options) {
var heading = fn(elem.children, options);
if (options.uppercaseHeadings) {
heading = heading.toUpperCase();
}
return heading + '\n';
}
// If we have both href and anchor text, format it in a useful manner:
// - "anchor text [href]"
// Otherwise if we have only anchor text or an href, we return the part we have:
// - "anchor text" or
// - "href"
function formatAnchor(elem, fn, options) {
var href = '';
// Always get the anchor text
var storedCharCount = options.lineCharCount;
var text = fn(elem.children || [], options);
if (!text) {
text = '';
}
var result = elem.trimLeadingSpace ? _s.lstrip(text) : text;
if (!options.ignoreHref) {
// Get the href, if present
if (elem.attribs && elem.attribs.href) {
href = elem.attribs.href.replace(/^mailto\:/, '');
}
if (href) {
if (options.linkHrefBaseUrl && href.indexOf('/') == 0) {
href = options.linkHrefBaseUrl + href;
}
if (!options.hideLinkHrefIfSameAsText || href != _s.replaceAll(result, '\n', '')) {
result += ' [' + href + ']';
}
}
}
options.lineCharCount = storedCharCount;
return formatText({ raw: result || href, trimLeadingSpace: elem.trimLeadingSpace }, options);
}
function formatHorizontalLine(elem, fn, options) {
return '\n' + _s.repeat('-', options.wordwrap) + '\n\n';
}
function formatListItem(prefix, elem, fn, options) {
options = _.clone(options);
// Reduce the wordwrap for sub elements.
if (options.wordwrap) {
options.wordwrap -= prefix.length;
}
// Process sub elements.
var text = fn(elem.children, options);
// Replace all line breaks with line break + prefix spacing.
text = text.replace(/\n/g, '\n' + _s.repeat(' ', prefix.length));
// Add first prefix and line break at the end.
return prefix + text + '\n';
}
var whiteSpaceRegex = /^\s*$/;
function formatUnorderedList(elem, fn, options) {
var result = '';
var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) {
return child.type !== 'text' || !whiteSpaceRegex.test(child.raw);
});
_.each(nonWhiteSpaceChildren, function(elem) {
result += formatListItem(' * ', elem, fn, options);
});
return result + '\n';
}
function formatOrderedList(elem, fn, options) {
var result = '';
var nonWhiteSpaceChildren = (elem.children || []).filter(function(child) {
return child.type !== 'text' || !whiteSpaceRegex.test(child.raw);
});
// Make sure there are list items present
if (nonWhiteSpaceChildren.length) {
// Calculate the maximum length to i.
var maxLength = nonWhiteSpaceChildren.length.toString().length;
_.each(nonWhiteSpaceChildren, function(elem, i) {
var index = i + 1;
// Calculate the needed spacing for nice indentation.
var spacing = maxLength - index.toString().length;
var prefix = ' ' + index + '. ' + _s.repeat(' ', spacing);
result += formatListItem(prefix, elem, fn, options);
});
}
return result + '\n';
}
function tableToString(table) {
// Determine space width per column
// Convert all rows to lengths
var widths = _.map(table, function(row) {
return _.map(row, function(col) {
return col.length;
});
});
// Invert rows with colums
widths = helper.arrayZip(widths);
// Determine the max values for each column
widths = _.map(widths, function(col) {
return _.max(col);
});
// Build the table
var text = '';
_.each(table, function(row) {
var i = 0;
_.each(row, function(col) {
text += _s.rpad(_s.strip(col), widths[i++], ' ') + ' ';
});
text += '\n';
});
return text + '\n';
}
function formatTable(elem, fn, options) {
var table = [];
_.each(elem.children, tryParseRows);
return tableToString(table);
function tryParseRows(elem) {
if (elem.type !== 'tag') {
return;
}
switch (elem.name.toLowerCase()) {
case "thead":
case "tbody":
case "tfoot":
case "center":
_.each(elem.children, tryParseRows);
return;
case 'tr':
var rows = [];
_.each(elem.children, function(elem) {
var tokens, times;
if (elem.type === 'tag') {
switch (elem.name.toLowerCase()) {
case 'th':
tokens = formatHeading(elem, fn, options).split('\n');
rows.push(_.compact(tokens));
break;
case 'td':
tokens = fn(elem.children, options).split('\n');
rows.push(_.compact(tokens));
// Fill colspans with empty values
if (elem.attribs && elem.attribs.colspan) {
times = elem.attribs.colspan - 1;
_.times(times, function() {
rows.push(['']);
});
}
break;
}
}
});
rows = helper.arrayZip(rows);
_.each(rows, function(row) {
row = _.map(row, function(col) {
return col || '';
});
table.push(row);
});
break;
}
}
}
exports.text = formatText;
exports.image = formatImage;
exports.video = formatVideo;
exports.lineBreak = formatLineBreak;
exports.paragraph = formatParagraph;
exports.anchor = formatAnchor;
exports.heading = formatHeading;
exports.table = formatTable;
exports.orderedList = formatOrderedList;
exports.unorderedList = formatUnorderedList;
exports.listItem = formatListItem;
exports.horizontalLine = formatHorizontalLine;