@cloudrac3r/turndown 7.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,946 @@
1
+ 'use strict';
2
+
3
+ function extend (destination) {
4
+ for (var i = 1; i < arguments.length; i++) {
5
+ var source = arguments[i];
6
+ for (var key in source) {
7
+ if (source.hasOwnProperty(key)) destination[key] = source[key];
8
+ }
9
+ }
10
+ return destination
11
+ }
12
+
13
+ function repeat (character, count) {
14
+ return Array(count + 1).join(character)
15
+ }
16
+
17
+ function trimLeadingNewlines (string) {
18
+ return string.replace(/^\n*/, '')
19
+ }
20
+
21
+ function trimTrailingNewlines (string) {
22
+ // avoid match-at-end regexp bottleneck, see #370
23
+ var indexEnd = string.length;
24
+ while (indexEnd > 0 && string[indexEnd - 1] === '\n') indexEnd--;
25
+ return string.substring(0, indexEnd)
26
+ }
27
+
28
+ var blockElements = [
29
+ 'ADDRESS', 'ARTICLE', 'ASIDE', 'AUDIO', 'BLOCKQUOTE', 'BODY', 'CANVAS',
30
+ 'CENTER', 'DD', 'DIR', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE',
31
+ 'FOOTER', 'FORM', 'FRAMESET', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEADER',
32
+ 'HGROUP', 'HR', 'HTML', 'ISINDEX', 'LI', 'MAIN', 'MENU', 'NAV', 'NOFRAMES',
33
+ 'NOSCRIPT', 'OL', 'OUTPUT', 'P', 'PRE', 'SECTION', 'TABLE', 'TBODY', 'TD',
34
+ 'TFOOT', 'TH', 'THEAD', 'TR', 'UL'
35
+ ];
36
+
37
+ function isBlock (node) {
38
+ return is(node, blockElements)
39
+ }
40
+
41
+ var voidElements = [
42
+ 'AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT',
43
+ 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'
44
+ ];
45
+
46
+ function isVoid (node) {
47
+ return is(node, voidElements)
48
+ }
49
+
50
+ function hasVoid (node) {
51
+ return has(node, voidElements)
52
+ }
53
+
54
+ var meaningfulWhenBlankElements = [
55
+ 'A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT',
56
+ 'AUDIO', 'VIDEO'
57
+ ];
58
+
59
+ function isMeaningfulWhenBlank (node) {
60
+ return is(node, meaningfulWhenBlankElements)
61
+ }
62
+
63
+ function hasMeaningfulWhenBlank (node) {
64
+ return has(node, meaningfulWhenBlankElements)
65
+ }
66
+
67
+ function is (node, tagNames) {
68
+ return tagNames.indexOf(node.nodeName) >= 0
69
+ }
70
+
71
+ function has (node, tagNames) {
72
+ return (
73
+ node.getElementsByTagName &&
74
+ tagNames.some(function (tagName) {
75
+ return node.getElementsByTagName(tagName).length
76
+ })
77
+ )
78
+ }
79
+
80
+ var rules = {};
81
+
82
+ rules.paragraph = {
83
+ filter: 'p',
84
+
85
+ replacement: function (content) {
86
+ return '\n\n' + content + '\n\n'
87
+ }
88
+ };
89
+
90
+ rules.lineBreak = {
91
+ filter: 'br',
92
+
93
+ replacement: function (content, node, options) {
94
+ return options.br + '\n'
95
+ }
96
+ };
97
+
98
+ rules.heading = {
99
+ filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
100
+
101
+ replacement: function (content, node, options) {
102
+ var hLevel = Number(node.nodeName.charAt(1));
103
+
104
+ if (options.headingStyle === 'setext' && hLevel < 3) {
105
+ var underline = repeat((hLevel === 1 ? '=' : '-'), content.length);
106
+ return (
107
+ '\n\n' + content + '\n' + underline + '\n\n'
108
+ )
109
+ } else {
110
+ return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n'
111
+ }
112
+ }
113
+ };
114
+
115
+ rules.blockquote = {
116
+ filter: 'blockquote',
117
+
118
+ replacement: function (content) {
119
+ content = content.replace(/^\n+|\n+$/g, '');
120
+ content = content.replace(/^/gm, '> ');
121
+ return '\n\n' + content + '\n\n'
122
+ }
123
+ };
124
+
125
+ rules.list = {
126
+ filter: ['ul', 'ol'],
127
+
128
+ replacement: function (content, node) {
129
+ var parent = node.parentNode;
130
+ if (parent.nodeName === 'LI' && parent.lastElementChild === node) {
131
+ return '\n' + content
132
+ } else {
133
+ return '\n\n' + content + '\n\n'
134
+ }
135
+ }
136
+ };
137
+
138
+ rules.listItem = {
139
+ filter: 'li',
140
+
141
+ replacement: function (content, node, options) {
142
+ content = content
143
+ .replace(/^\n+/, '') // remove leading newlines
144
+ .replace(/\n+$/, '\n') // replace trailing newlines with just a single one
145
+ .replace(/\n/gm, '\n '); // indent
146
+ var prefix = options.bulletListMarker + ' ';
147
+ var parent = node.parentNode;
148
+ if (parent.nodeName === 'OL') {
149
+ var start = parent.getAttribute('start');
150
+ var index = Array.prototype.indexOf.call(parent.children, node);
151
+ prefix = (start ? Number(start) + index : index + 1) + '. ';
152
+ }
153
+ return (
154
+ prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
155
+ )
156
+ }
157
+ };
158
+
159
+ rules.indentedCodeBlock = {
160
+ filter: function (node, options) {
161
+ return (
162
+ options.codeBlockStyle === 'indented' &&
163
+ node.nodeName === 'PRE' &&
164
+ node.firstChild &&
165
+ node.firstChild.nodeName === 'CODE'
166
+ )
167
+ },
168
+
169
+ replacement: function (content, node, options) {
170
+ return (
171
+ '\n\n ' +
172
+ node.firstChild.textContent.replace(/\n/g, '\n ') +
173
+ '\n\n'
174
+ )
175
+ }
176
+ };
177
+
178
+ rules.fencedCodeBlock = {
179
+ filter: function (node, options) {
180
+ return (
181
+ options.codeBlockStyle === 'fenced' &&
182
+ node.nodeName === 'PRE' &&
183
+ node.firstChild &&
184
+ node.firstChild.nodeName === 'CODE'
185
+ )
186
+ },
187
+
188
+ replacement: function (content, node, options) {
189
+ var className = node.firstChild.getAttribute('class') || '';
190
+ var language = (className.match(/language-(\S+)/) || [null, ''])[1];
191
+ var code = node.firstChild.textContent;
192
+
193
+ var fenceChar = options.fence.charAt(0);
194
+ var fenceSize = 3;
195
+ var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');
196
+
197
+ var match;
198
+ while ((match = fenceInCodeRegex.exec(code))) {
199
+ if (match[0].length >= fenceSize) {
200
+ fenceSize = match[0].length + 1;
201
+ }
202
+ }
203
+
204
+ var fence = repeat(fenceChar, fenceSize);
205
+
206
+ return (
207
+ '\n\n' + fence + language + '\n' +
208
+ code.replace(/\n$/, '') +
209
+ '\n' + fence + '\n\n'
210
+ )
211
+ }
212
+ };
213
+
214
+ rules.horizontalRule = {
215
+ filter: 'hr',
216
+
217
+ replacement: function (content, node, options) {
218
+ return '\n\n' + options.hr + '\n\n'
219
+ }
220
+ };
221
+
222
+ rules.inlineLink = {
223
+ filter: function (node, options) {
224
+ return (
225
+ options.linkStyle === 'inlined' &&
226
+ node.nodeName === 'A' &&
227
+ node.getAttribute('href')
228
+ )
229
+ },
230
+
231
+ replacement: function (content, node) {
232
+ var href = node.getAttribute('href');
233
+ var title = cleanAttribute(node.getAttribute('title'));
234
+ if (title) title = ' "' + title + '"';
235
+ return '[' + content + '](' + href + title + ')'
236
+ }
237
+ };
238
+
239
+ rules.referenceLink = {
240
+ filter: function (node, options) {
241
+ return (
242
+ options.linkStyle === 'referenced' &&
243
+ node.nodeName === 'A' &&
244
+ node.getAttribute('href')
245
+ )
246
+ },
247
+
248
+ replacement: function (content, node, options) {
249
+ var href = node.getAttribute('href');
250
+ var title = cleanAttribute(node.getAttribute('title'));
251
+ if (title) title = ' "' + title + '"';
252
+ var replacement;
253
+ var reference;
254
+
255
+ switch (options.linkReferenceStyle) {
256
+ case 'collapsed':
257
+ replacement = '[' + content + '][]';
258
+ reference = '[' + content + ']: ' + href + title;
259
+ break
260
+ case 'shortcut':
261
+ replacement = '[' + content + ']';
262
+ reference = '[' + content + ']: ' + href + title;
263
+ break
264
+ default:
265
+ var id = this.references.length + 1;
266
+ replacement = '[' + content + '][' + id + ']';
267
+ reference = '[' + id + ']: ' + href + title;
268
+ }
269
+
270
+ this.references.push(reference);
271
+ return replacement
272
+ },
273
+
274
+ references: [],
275
+
276
+ append: function (options) {
277
+ var references = '';
278
+ if (this.references.length) {
279
+ references = '\n\n' + this.references.join('\n') + '\n\n';
280
+ this.references = []; // Reset references
281
+ }
282
+ return references
283
+ }
284
+ };
285
+
286
+ rules.emphasis = {
287
+ filter: ['em', 'i'],
288
+
289
+ replacement: function (content, node, options) {
290
+ if (!content.trim()) return ''
291
+ return options.emDelimiter + content + options.emDelimiter
292
+ }
293
+ };
294
+
295
+ rules.strong = {
296
+ filter: ['strong', 'b'],
297
+
298
+ replacement: function (content, node, options) {
299
+ if (!content.trim()) return ''
300
+ return options.strongDelimiter + content + options.strongDelimiter
301
+ }
302
+ };
303
+
304
+ rules.code = {
305
+ filter: function (node) {
306
+ var hasSiblings = node.previousSibling || node.nextSibling;
307
+ var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
308
+
309
+ return node.nodeName === 'CODE' && !isCodeBlock
310
+ },
311
+
312
+ replacement: function (content) {
313
+ if (!content) return ''
314
+ content = content.replace(/\r?\n|\r/g, ' ');
315
+
316
+ var extraSpace = /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : '';
317
+ var delimiter = '`';
318
+ var matches = content.match(/`+/gm) || [];
319
+ while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`';
320
+
321
+ return delimiter + extraSpace + content + extraSpace + delimiter
322
+ }
323
+ };
324
+
325
+ rules.image = {
326
+ filter: 'img',
327
+
328
+ replacement: function (content, node) {
329
+ var alt = cleanAttribute(node.getAttribute('alt'));
330
+ var src = node.getAttribute('src') || '';
331
+ var title = cleanAttribute(node.getAttribute('title'));
332
+ var titlePart = title ? ' "' + title + '"' : '';
333
+ return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
334
+ }
335
+ };
336
+
337
+ function cleanAttribute (attribute) {
338
+ return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
339
+ }
340
+
341
+ /**
342
+ * Manages a collection of rules used to convert HTML to Markdown
343
+ */
344
+
345
+ function Rules (options) {
346
+ this.options = options;
347
+ this._keep = [];
348
+ this._remove = [];
349
+
350
+ this.blankRule = {
351
+ replacement: options.blankReplacement
352
+ };
353
+
354
+ this.keepReplacement = options.keepReplacement;
355
+
356
+ this.defaultRule = {
357
+ replacement: options.defaultReplacement
358
+ };
359
+
360
+ this.array = [];
361
+ for (var key in options.rules) this.array.push(options.rules[key]);
362
+ }
363
+
364
+ Rules.prototype = {
365
+ add: function (key, rule) {
366
+ this.array.unshift(rule);
367
+ },
368
+
369
+ keep: function (filter) {
370
+ this._keep.unshift({
371
+ filter: filter,
372
+ replacement: this.keepReplacement
373
+ });
374
+ },
375
+
376
+ remove: function (filter) {
377
+ this._remove.unshift({
378
+ filter: filter,
379
+ replacement: function () {
380
+ return ''
381
+ }
382
+ });
383
+ },
384
+
385
+ forNode: function (node) {
386
+ if (node.isBlank) return this.blankRule
387
+ var rule;
388
+
389
+ if ((rule = findRule(this.array, node, this.options))) return rule
390
+ if ((rule = findRule(this._keep, node, this.options))) return rule
391
+ if ((rule = findRule(this._remove, node, this.options))) return rule
392
+
393
+ return this.defaultRule
394
+ },
395
+
396
+ forEach: function (fn) {
397
+ for (var i = 0; i < this.array.length; i++) fn(this.array[i], i);
398
+ }
399
+ };
400
+
401
+ function findRule (rules, node, options) {
402
+ for (var i = 0; i < rules.length; i++) {
403
+ var rule = rules[i];
404
+ if (filterValue(rule, node, options)) return rule
405
+ }
406
+ return void 0
407
+ }
408
+
409
+ function filterValue (rule, node, options) {
410
+ var filter = rule.filter;
411
+ if (typeof filter === 'string') {
412
+ if (filter === node.nodeName.toLowerCase()) return true
413
+ } else if (Array.isArray(filter)) {
414
+ if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true
415
+ } else if (typeof filter === 'function') {
416
+ if (filter.call(rule, node, options)) return true
417
+ } else {
418
+ throw new TypeError('`filter` needs to be a string, array, or function')
419
+ }
420
+ }
421
+
422
+ /**
423
+ * The collapseWhitespace function is adapted from collapse-whitespace
424
+ * by Luc Thevenard.
425
+ *
426
+ * The MIT License (MIT)
427
+ *
428
+ * Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>
429
+ *
430
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
431
+ * of this software and associated documentation files (the "Software"), to deal
432
+ * in the Software without restriction, including without limitation the rights
433
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
434
+ * copies of the Software, and to permit persons to whom the Software is
435
+ * furnished to do so, subject to the following conditions:
436
+ *
437
+ * The above copyright notice and this permission notice shall be included in
438
+ * all copies or substantial portions of the Software.
439
+ *
440
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
441
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
442
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
443
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
444
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
445
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
446
+ * THE SOFTWARE.
447
+ */
448
+
449
+ /**
450
+ * collapseWhitespace(options) removes extraneous whitespace from an the given element.
451
+ *
452
+ * @param {Object} options
453
+ */
454
+ function collapseWhitespace (options) {
455
+ var element = options.element;
456
+ var isBlock = options.isBlock;
457
+ var isVoid = options.isVoid;
458
+ var isPre = options.isPre || function (node) {
459
+ return node.nodeName === 'PRE'
460
+ };
461
+
462
+ if (!element.firstChild || isPre(element)) return
463
+
464
+ var prevText = null;
465
+ var keepLeadingWs = false;
466
+
467
+ var prev = null;
468
+ var node = next(prev, element, isPre);
469
+
470
+ while (node !== element) {
471
+ if (node.nodeType === 3 || node.nodeType === 4) { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE
472
+ var text = node.data.replace(/[ \r\n\t]+/g, ' ');
473
+
474
+ if ((!prevText || / $/.test(prevText.data)) &&
475
+ !keepLeadingWs && text[0] === ' ') {
476
+ text = text.substr(1);
477
+ }
478
+
479
+ // `text` might be empty at this point.
480
+ if (!text) {
481
+ node = remove(node);
482
+ continue
483
+ }
484
+
485
+ node.data = text;
486
+
487
+ prevText = node;
488
+ } else if (node.nodeType === 1) { // Node.ELEMENT_NODE
489
+ if (isBlock(node) || node.nodeName === 'BR') {
490
+ if (prevText) {
491
+ prevText.data = prevText.data.replace(/ $/, '');
492
+ }
493
+
494
+ prevText = null;
495
+ keepLeadingWs = false;
496
+ } else if (isVoid(node) || isPre(node)) {
497
+ // Avoid trimming space around non-block, non-BR void elements and inline PRE.
498
+ prevText = null;
499
+ keepLeadingWs = true;
500
+ } else if (prevText) {
501
+ // Drop protection if set previously.
502
+ keepLeadingWs = false;
503
+ }
504
+ } else {
505
+ node = remove(node);
506
+ continue
507
+ }
508
+
509
+ var nextNode = next(prev, node, isPre);
510
+ prev = node;
511
+ node = nextNode;
512
+ }
513
+
514
+ if (prevText) {
515
+ prevText.data = prevText.data.replace(/ $/, '');
516
+ if (!prevText.data) {
517
+ remove(prevText);
518
+ }
519
+ }
520
+ }
521
+
522
+ /**
523
+ * remove(node) removes the given node from the DOM and returns the
524
+ * next node in the sequence.
525
+ *
526
+ * @param {Node} node
527
+ * @return {Node} node
528
+ */
529
+ function remove (node) {
530
+ var next = node.nextSibling || node.parentNode;
531
+
532
+ node.parentNode.removeChild(node);
533
+
534
+ return next
535
+ }
536
+
537
+ /**
538
+ * next(prev, current, isPre) returns the next node in the sequence, given the
539
+ * current and previous nodes.
540
+ *
541
+ * @param {Node} prev
542
+ * @param {Node} current
543
+ * @param {Function} isPre
544
+ * @return {Node}
545
+ */
546
+ function next (prev, current, isPre) {
547
+ if ((prev && prev.parentNode === current) || isPre(current)) {
548
+ return current.nextSibling || current.parentNode
549
+ }
550
+
551
+ return current.firstChild || current.nextSibling || current.parentNode
552
+ }
553
+
554
+ /*
555
+ * Set up window for Node.js
556
+ */
557
+
558
+ var root = (typeof window !== 'undefined' ? window : {});
559
+
560
+ /*
561
+ * Parsing HTML strings
562
+ */
563
+
564
+ function canParseHTMLNatively () {
565
+ var Parser = root.DOMParser;
566
+ var canParse = false;
567
+
568
+ // Adapted from https://gist.github.com/1129031
569
+ // Firefox/Opera/IE throw errors on unsupported types
570
+ try {
571
+ // WebKit returns null on unsupported types
572
+ if (new Parser().parseFromString('', 'text/html')) {
573
+ canParse = true;
574
+ }
575
+ } catch (e) {}
576
+
577
+ return canParse
578
+ }
579
+
580
+ function createHTMLParser () {
581
+ var Parser = function () {};
582
+
583
+ {
584
+ var domino = require('domino');
585
+ Parser.prototype.parseFromString = function (string) {
586
+ return domino.createDocument(string)
587
+ };
588
+ }
589
+ return Parser
590
+ }
591
+
592
+ var HTMLParser = canParseHTMLNatively() ? root.DOMParser : createHTMLParser();
593
+
594
+ function RootNode (input, options) {
595
+ var root;
596
+ if (typeof input === 'string') {
597
+ var doc = htmlParser().parseFromString(
598
+ // DOM parsers arrange elements in the <head> and <body>.
599
+ // Wrapping in a custom element ensures elements are reliably arranged in
600
+ // a single element.
601
+ '<x-turndown id="turndown-root">' + input + '</x-turndown>',
602
+ 'text/html'
603
+ );
604
+ root = doc.getElementById('turndown-root');
605
+ } else {
606
+ root = input.cloneNode(true);
607
+ }
608
+ collapseWhitespace({
609
+ element: root,
610
+ isBlock: isBlock,
611
+ isVoid: isVoid,
612
+ isPre: options.preformattedCode ? isPreOrCode : null
613
+ });
614
+
615
+ return root
616
+ }
617
+
618
+ var _htmlParser;
619
+ function htmlParser () {
620
+ _htmlParser = _htmlParser || new HTMLParser();
621
+ return _htmlParser
622
+ }
623
+
624
+ function isPreOrCode (node) {
625
+ return node.nodeName === 'PRE' || node.nodeName === 'CODE'
626
+ }
627
+
628
+ function Node (node, options) {
629
+ node.isBlock = isBlock(node);
630
+ node.isCode = node.nodeName === 'CODE' || node.nodeName === 'A' || node.parentNode.isCode;
631
+ node.isBlank = isBlank(node);
632
+ node.flankingWhitespace = flankingWhitespace(node, options);
633
+ return node
634
+ }
635
+
636
+ function isBlank (node) {
637
+ return (
638
+ !isVoid(node) &&
639
+ !isMeaningfulWhenBlank(node) &&
640
+ /^\s*$/i.test(node.textContent) &&
641
+ !hasVoid(node) &&
642
+ !hasMeaningfulWhenBlank(node)
643
+ )
644
+ }
645
+
646
+ function flankingWhitespace (node, options) {
647
+ if (node.isBlock || (options.preformattedCode && node.isCode)) {
648
+ return { leading: '', trailing: '' }
649
+ }
650
+
651
+ var edges = edgeWhitespace(node.textContent);
652
+
653
+ // abandon leading ASCII WS if left-flanked by ASCII WS
654
+ if (edges.leadingAscii && isFlankedByWhitespace('left', node, options)) {
655
+ edges.leading = edges.leadingNonAscii;
656
+ }
657
+
658
+ // abandon trailing ASCII WS if right-flanked by ASCII WS
659
+ if (edges.trailingAscii && isFlankedByWhitespace('right', node, options)) {
660
+ edges.trailing = edges.trailingNonAscii;
661
+ }
662
+
663
+ return { leading: edges.leading, trailing: edges.trailing }
664
+ }
665
+
666
+ function edgeWhitespace (string) {
667
+ var m = string.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);
668
+ return {
669
+ leading: m[1], // whole string for whitespace-only strings
670
+ leadingAscii: m[2],
671
+ leadingNonAscii: m[3],
672
+ trailing: m[4], // empty for whitespace-only strings
673
+ trailingNonAscii: m[5],
674
+ trailingAscii: m[6]
675
+ }
676
+ }
677
+
678
+ function isFlankedByWhitespace (side, node, options) {
679
+ var sibling;
680
+ var regExp;
681
+ var isFlanked;
682
+
683
+ if (side === 'left') {
684
+ sibling = node.previousSibling;
685
+ regExp = / $/;
686
+ } else {
687
+ sibling = node.nextSibling;
688
+ regExp = /^ /;
689
+ }
690
+
691
+ if (sibling) {
692
+ if (sibling.nodeType === 3) {
693
+ isFlanked = regExp.test(sibling.nodeValue);
694
+ } else if (options.preformattedCode && sibling.nodeName === 'CODE') {
695
+ isFlanked = false;
696
+ } else if (sibling.nodeType === 1 && !isBlock(sibling)) {
697
+ isFlanked = regExp.test(sibling.textContent);
698
+ }
699
+ }
700
+ return isFlanked
701
+ }
702
+
703
+ var reduce = Array.prototype.reduce;
704
+ var escapes = [
705
+ [/\\/g, '\\\\'],
706
+ [/\*/g, '\\*'],
707
+ [/^-/g, '\\-'],
708
+ [/^\+ /g, '\\+ '],
709
+ [/^(=+)/g, '\\$1'],
710
+ [/^(#{1,6}) /g, '\\$1 '],
711
+ [/`/g, '\\`'],
712
+ [/^~~~/g, '\\~~~'],
713
+ [/\[/g, '\\['],
714
+ [/\]/g, '\\]'],
715
+ [/^>/g, '\\>'],
716
+ [/_/g, '\\_'],
717
+ [/^(\d+)\. /g, '$1\\. ']
718
+ ];
719
+
720
+ function TurndownService (options) {
721
+ if (!(this instanceof TurndownService)) return new TurndownService(options)
722
+
723
+ var defaults = {
724
+ rules: rules,
725
+ headingStyle: 'setext',
726
+ hr: '* * *',
727
+ bulletListMarker: '*',
728
+ codeBlockStyle: 'indented',
729
+ fence: '```',
730
+ emDelimiter: '_',
731
+ strongDelimiter: '**',
732
+ linkStyle: 'inlined',
733
+ linkReferenceStyle: 'full',
734
+ br: ' ',
735
+ preformattedCode: false,
736
+ blankReplacement: function (content, node) {
737
+ return node.isBlock ? '\n\n' : ''
738
+ },
739
+ keepReplacement: function (content, node) {
740
+ return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
741
+ },
742
+ defaultReplacement: function (content, node) {
743
+ return node.isBlock ? '\n\n' + content + '\n\n' : content
744
+ }
745
+ };
746
+ this.options = extend({}, defaults, options);
747
+ this.rules = new Rules(this.options);
748
+ }
749
+
750
+ TurndownService.prototype = {
751
+ /**
752
+ * The entry point for converting a string or DOM node to Markdown
753
+ * @public
754
+ * @param {String|HTMLElement} input The string or DOM node to convert
755
+ * @returns A Markdown representation of the input
756
+ * @type String
757
+ */
758
+
759
+ turndown: function (input) {
760
+ if (!canConvert(input)) {
761
+ throw new TypeError(
762
+ input + ' is not a string, or an element/document/fragment node.'
763
+ )
764
+ }
765
+
766
+ if (input === '') return ''
767
+
768
+ var output = process.call(this, new RootNode(input, this.options));
769
+ return postProcess.call(this, output)
770
+ },
771
+
772
+ /**
773
+ * Add one or more plugins
774
+ * @public
775
+ * @param {Function|Array} plugin The plugin or array of plugins to add
776
+ * @returns The Turndown instance for chaining
777
+ * @type Object
778
+ */
779
+
780
+ use: function (plugin) {
781
+ if (Array.isArray(plugin)) {
782
+ for (var i = 0; i < plugin.length; i++) this.use(plugin[i]);
783
+ } else if (typeof plugin === 'function') {
784
+ plugin(this);
785
+ } else {
786
+ throw new TypeError('plugin must be a Function or an Array of Functions')
787
+ }
788
+ return this
789
+ },
790
+
791
+ /**
792
+ * Adds a rule
793
+ * @public
794
+ * @param {String} key The unique key of the rule
795
+ * @param {Object} rule The rule
796
+ * @returns The Turndown instance for chaining
797
+ * @type Object
798
+ */
799
+
800
+ addRule: function (key, rule) {
801
+ this.rules.add(key, rule);
802
+ return this
803
+ },
804
+
805
+ /**
806
+ * Keep a node (as HTML) that matches the filter
807
+ * @public
808
+ * @param {String|Array|Function} filter The unique key of the rule
809
+ * @returns The Turndown instance for chaining
810
+ * @type Object
811
+ */
812
+
813
+ keep: function (filter) {
814
+ this.rules.keep(filter);
815
+ return this
816
+ },
817
+
818
+ /**
819
+ * Remove a node that matches the filter
820
+ * @public
821
+ * @param {String|Array|Function} filter The unique key of the rule
822
+ * @returns The Turndown instance for chaining
823
+ * @type Object
824
+ */
825
+
826
+ remove: function (filter) {
827
+ this.rules.remove(filter);
828
+ return this
829
+ },
830
+
831
+ /**
832
+ * Escapes Markdown syntax
833
+ * @public
834
+ * @param {String} string The string to escape
835
+ * @returns A string with Markdown syntax escaped
836
+ * @type String
837
+ */
838
+
839
+ escape: function (string) {
840
+ return escapes.reduce(function (accumulator, escape) {
841
+ return accumulator.replace(escape[0], escape[1])
842
+ }, string)
843
+ }
844
+ };
845
+
846
+ /**
847
+ * Reduces a DOM node down to its Markdown string equivalent
848
+ * @private
849
+ * @param {HTMLElement} parentNode The node to convert
850
+ * @returns A Markdown representation of the node
851
+ * @type String
852
+ */
853
+
854
+ function process (parentNode) {
855
+ var self = this;
856
+ return reduce.call(parentNode.childNodes, function (output, node) {
857
+ node = new Node(node, self.options);
858
+
859
+ var replacement = '';
860
+ if (node.nodeType === 3) {
861
+ replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue);
862
+ } else if (node.nodeType === 1) {
863
+ replacement = replacementForNode.call(self, node);
864
+ }
865
+
866
+ return join(output, replacement)
867
+ }, '')
868
+ }
869
+
870
+ /**
871
+ * Appends strings as each rule requires and trims the output
872
+ * @private
873
+ * @param {String} output The conversion output
874
+ * @returns A trimmed version of the ouput
875
+ * @type String
876
+ */
877
+
878
+ function postProcess (output) {
879
+ var self = this;
880
+ this.rules.forEach(function (rule) {
881
+ if (typeof rule.append === 'function') {
882
+ output = join(output, rule.append(self.options));
883
+ }
884
+ });
885
+
886
+ return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '')
887
+ }
888
+
889
+ /**
890
+ * Converts an element node to its Markdown equivalent
891
+ * @private
892
+ * @param {HTMLElement} node The node to convert
893
+ * @returns A Markdown representation of the node
894
+ * @type String
895
+ */
896
+
897
+ function replacementForNode (node) {
898
+ var rule = this.rules.forNode(node);
899
+ var content = process.call(this, node);
900
+ var whitespace = node.flankingWhitespace;
901
+ if (whitespace.leading || whitespace.trailing) content = content.trim();
902
+ return (
903
+ whitespace.leading +
904
+ rule.replacement(content, node, this.options) +
905
+ whitespace.trailing
906
+ )
907
+ }
908
+
909
+ /**
910
+ * Joins replacement to the current output with appropriate number of new lines
911
+ * @private
912
+ * @param {String} output The current conversion output
913
+ * @param {String} replacement The string to append to the output
914
+ * @returns Joined output
915
+ * @type String
916
+ */
917
+
918
+ function join (output, replacement) {
919
+ var s1 = trimTrailingNewlines(output);
920
+ var s2 = trimLeadingNewlines(replacement);
921
+ var nls = Math.max(output.length - s1.length, replacement.length - s2.length);
922
+ var separator = '\n\n'.substring(0, nls);
923
+
924
+ return s1 + separator + s2
925
+ }
926
+
927
+ /**
928
+ * Determines whether an input can be converted
929
+ * @private
930
+ * @param {String|HTMLElement} input Describe this parameter
931
+ * @returns Describe what it returns
932
+ * @type String|Object|Array|Boolean|Number
933
+ */
934
+
935
+ function canConvert (input) {
936
+ return (
937
+ input != null && (
938
+ typeof input === 'string' ||
939
+ (input.nodeType && (
940
+ input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11
941
+ ))
942
+ )
943
+ )
944
+ }
945
+
946
+ module.exports = TurndownService;