wassemgtk commited on
Commit
be19770
1 Parent(s): a880854

Upload 3 files

Browse files
Files changed (3) hide show
  1. bootstrap.css +0 -0
  2. clipboard2markdown.js +186 -0
  3. to-markdown.js +787 -0
bootstrap.css ADDED
The diff for this file is too large to render. See raw diff
 
clipboard2markdown.js ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function () {
2
+ 'use strict';
3
+
4
+ // http://pandoc.org/README.html#pandocs-markdown
5
+ var pandoc = [
6
+ {
7
+ filter: 'h1',
8
+ replacement: function (content, node) {
9
+ var underline = Array(content.length + 1).join('=');
10
+ return '\n\n' + content + '\n' + underline + '\n\n';
11
+ }
12
+ },
13
+
14
+ {
15
+ filter: 'h2',
16
+ replacement: function (content, node) {
17
+ var underline = Array(content.length + 1).join('-');
18
+ return '\n\n' + content + '\n' + underline + '\n\n';
19
+ }
20
+ },
21
+
22
+ {
23
+ filter: 'sup',
24
+ replacement: function (content) {
25
+ return '^' + content + '^';
26
+ }
27
+ },
28
+
29
+ {
30
+ filter: 'sub',
31
+ replacement: function (content) {
32
+ return '~' + content + '~';
33
+ }
34
+ },
35
+
36
+ {
37
+ filter: 'br',
38
+ replacement: function () {
39
+ return '\\\n';
40
+ }
41
+ },
42
+
43
+ {
44
+ filter: 'hr',
45
+ replacement: function () {
46
+ return '\n\n* * * * *\n\n';
47
+ }
48
+ },
49
+
50
+ {
51
+ filter: ['em', 'i', 'cite', 'var'],
52
+ replacement: function (content) {
53
+ return '*' + content + '*';
54
+ }
55
+ },
56
+
57
+ {
58
+ filter: function (node) {
59
+ var hasSiblings = node.previousSibling || node.nextSibling;
60
+ var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
61
+ var isCodeElem = node.nodeName === 'CODE' ||
62
+ node.nodeName === 'KBD' ||
63
+ node.nodeName === 'SAMP' ||
64
+ node.nodeName === 'TT';
65
+
66
+ return isCodeElem && !isCodeBlock;
67
+ },
68
+ replacement: function (content) {
69
+ return '`' + content + '`';
70
+ }
71
+ },
72
+
73
+ {
74
+ filter: function (node) {
75
+ return node.nodeName === 'A' && node.getAttribute('href');
76
+ },
77
+ replacement: function (content, node) {
78
+ var url = node.getAttribute('href');
79
+ var titlePart = node.title ? ' "' + node.title + '"' : '';
80
+ if (content === url) {
81
+ return '<' + url + '>';
82
+ } else if (url === ('mailto:' + content)) {
83
+ return '<' + content + '>';
84
+ } else {
85
+ return '[' + content + '](' + url + titlePart + ')';
86
+ }
87
+ }
88
+ },
89
+
90
+ {
91
+ filter: 'li',
92
+ replacement: function (content, node) {
93
+ content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ');
94
+ var prefix = '- ';
95
+ var parent = node.parentNode;
96
+
97
+ if (/ol/i.test(parent.nodeName)) {
98
+ var index = Array.prototype.indexOf.call(parent.children, node) + 1;
99
+ prefix = index + '. ';
100
+ while (prefix.length < 4) {
101
+ prefix += ' ';
102
+ }
103
+ }
104
+
105
+ return prefix + content;
106
+ }
107
+ }
108
+ ];
109
+
110
+ // http://pandoc.org/README.html#smart-punctuation
111
+ var escape = function (str) {
112
+ return str.replace(/[\u2018\u2019\u00b4]/g, "'")
113
+ .replace(/[\u201c\u201d\u2033]/g, '"')
114
+ .replace(/[\u2212\u2022\u00b7\u25aa]/g, '-')
115
+ .replace(/[\u2013\u2015]/g, '--')
116
+ .replace(/\u2014/g, '---')
117
+ .replace(/\u2026/g, '...')
118
+ .replace(/[ ]+\n/g, '\n')
119
+ .replace(/\s*\\\n/g, '\\\n')
120
+ .replace(/\s*\\\n\s*\\\n/g, '\n\n')
121
+ .replace(/\s*\\\n\n/g, '\n\n')
122
+ .replace(/\n-\n/g, '\n')
123
+ .replace(/\n\n\s*\\\n/g, '\n\n')
124
+ .replace(/\n\n\n*/g, '\n\n')
125
+ .replace(/[ ]+$/gm, '')
126
+ .replace(/^\s+|[\s\\]+$/g, '');
127
+ };
128
+
129
+ var convert = function (str) {
130
+ return escape(toMarkdown(str, { converters: pandoc, gfm: true }));
131
+ }
132
+
133
+ var insert = function (myField, myValue) {
134
+ if (document.selection) {
135
+ myField.focus();
136
+ sel = document.selection.createRange();
137
+ sel.text = myValue;
138
+ sel.select()
139
+ } else {
140
+ if (myField.selectionStart || myField.selectionStart == "0") {
141
+ var startPos = myField.selectionStart;
142
+ var endPos = myField.selectionEnd;
143
+ var beforeValue = myField.value.substring(0, startPos);
144
+ var afterValue = myField.value.substring(endPos, myField.value.length);
145
+ myField.value = beforeValue + myValue + afterValue;
146
+ myField.selectionStart = startPos + myValue.length;
147
+ myField.selectionEnd = startPos + myValue.length;
148
+ myField.focus()
149
+ } else {
150
+ myField.value += myValue;
151
+ myField.focus()
152
+ }
153
+ }
154
+ };
155
+
156
+ // http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser
157
+ document.addEventListener('DOMContentLoaded', function () {
158
+ var info = document.querySelector('#info');
159
+ var pastebin = document.querySelector('#pastebin');
160
+ var output = document.querySelector('#output');
161
+ var wrapper = document.querySelector('#wrapper');
162
+
163
+ document.addEventListener('keydown', function (event) {
164
+ if (event.ctrlKey || event.metaKey) {
165
+ if (String.fromCharCode(event.which).toLowerCase() === 'v') {
166
+ pastebin.innerHTML = '';
167
+ pastebin.focus();
168
+ info.classList.add('hidden');
169
+ wrapper.classList.add('hidden');
170
+ }
171
+ }
172
+ });
173
+
174
+ pastebin.addEventListener('paste', function () {
175
+ setTimeout(function () {
176
+ var html = pastebin.innerHTML;
177
+ var markdown = convert(html);
178
+ // output.value = markdown;
179
+ insert(output, markdown);
180
+ wrapper.classList.remove('hidden');
181
+ output.focus();
182
+ output.select();
183
+ }, 200);
184
+ });
185
+ });
186
+ })();
to-markdown.js ADDED
@@ -0,0 +1,787 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.toMarkdown = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2
+ /*
3
+ * to-markdown - an HTML to Markdown converter
4
+ *
5
+ * Copyright 2011+, Dom Christie
6
+ * Licenced under the MIT licence
7
+ *
8
+ */
9
+
10
+ 'use strict'
11
+
12
+ var toMarkdown
13
+ var converters
14
+ var mdConverters = require('./lib/md-converters')
15
+ var gfmConverters = require('./lib/gfm-converters')
16
+ var HtmlParser = require('./lib/html-parser')
17
+ var collapse = require('collapse-whitespace')
18
+
19
+ /*
20
+ * Utilities
21
+ */
22
+
23
+ var blocks = ['address', 'article', 'aside', 'audio', 'blockquote', 'body',
24
+ 'canvas', 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption',
25
+ 'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
26
+ 'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav',
27
+ 'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table',
28
+ 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul'
29
+ ]
30
+
31
+ function isBlock (node) {
32
+ return blocks.indexOf(node.nodeName.toLowerCase()) !== -1
33
+ }
34
+
35
+ var voids = [
36
+ 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
37
+ 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
38
+ ]
39
+
40
+ function isVoid (node) {
41
+ return voids.indexOf(node.nodeName.toLowerCase()) !== -1
42
+ }
43
+
44
+ function htmlToDom (string) {
45
+ var tree = new HtmlParser().parseFromString(string, 'text/html')
46
+ collapse(tree.documentElement, isBlock)
47
+ return tree
48
+ }
49
+
50
+ /*
51
+ * Flattens DOM tree into single array
52
+ */
53
+
54
+ function bfsOrder (node) {
55
+ var inqueue = [node]
56
+ var outqueue = []
57
+ var elem
58
+ var children
59
+ var i
60
+
61
+ while (inqueue.length > 0) {
62
+ elem = inqueue.shift()
63
+ outqueue.push(elem)
64
+ children = elem.childNodes
65
+ for (i = 0; i < children.length; i++) {
66
+ if (children[i].nodeType === 1) inqueue.push(children[i])
67
+ }
68
+ }
69
+ outqueue.shift()
70
+ return outqueue
71
+ }
72
+
73
+ /*
74
+ * Contructs a Markdown string of replacement text for a given node
75
+ */
76
+
77
+ function getContent (node) {
78
+ var text = ''
79
+ for (var i = 0; i < node.childNodes.length; i++) {
80
+ if (node.childNodes[i].nodeType === 1) {
81
+ text += node.childNodes[i]._replacement
82
+ } else if (node.childNodes[i].nodeType === 3) {
83
+ text += node.childNodes[i].data
84
+ } else continue
85
+ }
86
+ return text
87
+ }
88
+
89
+ /*
90
+ * Returns the HTML string of an element with its contents converted
91
+ */
92
+
93
+ function outer (node, content) {
94
+ return node.cloneNode(false).outerHTML.replace('><', '>' + content + '<')
95
+ }
96
+
97
+ function canConvert (node, filter) {
98
+ if (typeof filter === 'string') {
99
+ return filter === node.nodeName.toLowerCase()
100
+ }
101
+ if (Array.isArray(filter)) {
102
+ return filter.indexOf(node.nodeName.toLowerCase()) !== -1
103
+ } else if (typeof filter === 'function') {
104
+ return filter.call(toMarkdown, node)
105
+ } else {
106
+ throw new TypeError('`filter` needs to be a string, array, or function')
107
+ }
108
+ }
109
+
110
+ function isFlankedByWhitespace (side, node) {
111
+ var sibling
112
+ var regExp
113
+ var isFlanked
114
+
115
+ if (side === 'left') {
116
+ sibling = node.previousSibling
117
+ regExp = / $/
118
+ } else {
119
+ sibling = node.nextSibling
120
+ regExp = /^ /
121
+ }
122
+
123
+ if (sibling) {
124
+ if (sibling.nodeType === 3) {
125
+ isFlanked = regExp.test(sibling.nodeValue)
126
+ } else if (sibling.nodeType === 1 && !isBlock(sibling)) {
127
+ isFlanked = regExp.test(sibling.textContent)
128
+ }
129
+ }
130
+ return isFlanked
131
+ }
132
+
133
+ function flankingWhitespace (node) {
134
+ var leading = ''
135
+ var trailing = ''
136
+
137
+ if (!isBlock(node)) {
138
+ var hasLeading = /^[ \r\n\t]/.test(node.innerHTML)
139
+ var hasTrailing = /[ \r\n\t]$/.test(node.innerHTML)
140
+
141
+ if (hasLeading && !isFlankedByWhitespace('left', node)) {
142
+ leading = ' '
143
+ }
144
+ if (hasTrailing && !isFlankedByWhitespace('right', node)) {
145
+ trailing = ' '
146
+ }
147
+ }
148
+
149
+ return { leading: leading, trailing: trailing }
150
+ }
151
+
152
+ /*
153
+ * Finds a Markdown converter, gets the replacement, and sets it on
154
+ * `_replacement`
155
+ */
156
+
157
+ function process (node) {
158
+ var replacement
159
+ var content = getContent(node)
160
+
161
+ // Remove blank nodes
162
+ if (!isVoid(node) && !/A|TH|TD/.test(node.nodeName) && /^\s*$/i.test(content)) {
163
+ node._replacement = ''
164
+ return
165
+ }
166
+
167
+ for (var i = 0; i < converters.length; i++) {
168
+ var converter = converters[i]
169
+
170
+ if (canConvert(node, converter.filter)) {
171
+ if (typeof converter.replacement !== 'function') {
172
+ throw new TypeError(
173
+ '`replacement` needs to be a function that returns a string'
174
+ )
175
+ }
176
+
177
+ var whitespace = flankingWhitespace(node)
178
+
179
+ if (whitespace.leading || whitespace.trailing) {
180
+ content = content.trim()
181
+ }
182
+ replacement = whitespace.leading +
183
+ converter.replacement.call(toMarkdown, content, node) +
184
+ whitespace.trailing
185
+ break
186
+ }
187
+ }
188
+
189
+ node._replacement = replacement
190
+ }
191
+
192
+ toMarkdown = function (input, options) {
193
+ options = options || {}
194
+
195
+ if (typeof input !== 'string') {
196
+ throw new TypeError(input + ' is not a string')
197
+ }
198
+
199
+ // Escape potential ol triggers
200
+ input = input.replace(/(>[\r\n\s]*)(\d+)\.(&nbsp;| )/g, '$1$2\\.$3')
201
+
202
+ var clone = htmlToDom(input).body
203
+ var nodes = bfsOrder(clone)
204
+ var output
205
+
206
+ converters = mdConverters.slice(0)
207
+ if (options.gfm) {
208
+ converters = gfmConverters.concat(converters)
209
+ }
210
+
211
+ if (options.converters) {
212
+ converters = options.converters.concat(converters)
213
+ }
214
+
215
+ // Process through nodes in reverse (so deepest child elements are first).
216
+ for (var i = nodes.length - 1; i >= 0; i--) {
217
+ process(nodes[i])
218
+ }
219
+ output = getContent(clone)
220
+
221
+ return output.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g, '')
222
+ .replace(/\n\s+\n/g, '\n\n')
223
+ .replace(/\n{3,}/g, '\n\n')
224
+ }
225
+
226
+ toMarkdown.isBlock = isBlock
227
+ toMarkdown.isVoid = isVoid
228
+ toMarkdown.outer = outer
229
+
230
+ module.exports = toMarkdown
231
+
232
+ },{"./lib/gfm-converters":2,"./lib/html-parser":3,"./lib/md-converters":4,"collapse-whitespace":7}],2:[function(require,module,exports){
233
+ 'use strict'
234
+
235
+ function cell (content, node) {
236
+ var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node)
237
+ var prefix = ' '
238
+ if (index === 0) prefix = '| '
239
+ return prefix + content + ' |'
240
+ }
241
+
242
+ var highlightRegEx = /highlight highlight-(\S+)/
243
+
244
+ module.exports = [
245
+ {
246
+ filter: 'br',
247
+ replacement: function () {
248
+ return '\n'
249
+ }
250
+ },
251
+ {
252
+ filter: ['del', 's', 'strike'],
253
+ replacement: function (content) {
254
+ return '~~' + content + '~~'
255
+ }
256
+ },
257
+
258
+ {
259
+ filter: function (node) {
260
+ return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'
261
+ },
262
+ replacement: function (content, node) {
263
+ return (node.checked ? '[x]' : '[ ]') + ' '
264
+ }
265
+ },
266
+
267
+ {
268
+ filter: ['th', 'td'],
269
+ replacement: function (content, node) {
270
+ return cell(content, node)
271
+ }
272
+ },
273
+
274
+ {
275
+ filter: 'tr',
276
+ replacement: function (content, node) {
277
+ var borderCells = ''
278
+ var alignMap = { left: ':--', right: '--:', center: ':-:' }
279
+
280
+ if (node.parentNode.nodeName === 'THEAD') {
281
+ for (var i = 0; i < node.childNodes.length; i++) {
282
+ var align = node.childNodes[i].attributes.align
283
+ var border = '---'
284
+
285
+ if (align) border = alignMap[align.value] || border
286
+
287
+ borderCells += cell(border, node.childNodes[i])
288
+ }
289
+ }
290
+ return '\n' + content + (borderCells ? '\n' + borderCells : '')
291
+ }
292
+ },
293
+
294
+ {
295
+ filter: 'table',
296
+ replacement: function (content) {
297
+ return '\n\n' + content + '\n\n'
298
+ }
299
+ },
300
+
301
+ {
302
+ filter: ['thead', 'tbody', 'tfoot'],
303
+ replacement: function (content) {
304
+ return content
305
+ }
306
+ },
307
+
308
+ // Fenced code blocks
309
+ {
310
+ filter: function (node) {
311
+ return node.nodeName === 'PRE' &&
312
+ node.firstChild &&
313
+ node.firstChild.nodeName === 'CODE'
314
+ },
315
+ replacement: function (content, node) {
316
+ return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'
317
+ }
318
+ },
319
+
320
+ // Syntax-highlighted code blocks
321
+ {
322
+ filter: function (node) {
323
+ return node.nodeName === 'PRE' &&
324
+ node.parentNode.nodeName === 'DIV' &&
325
+ highlightRegEx.test(node.parentNode.className)
326
+ },
327
+ replacement: function (content, node) {
328
+ var language = node.parentNode.className.match(highlightRegEx)[1]
329
+ return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'
330
+ }
331
+ },
332
+
333
+ {
334
+ filter: function (node) {
335
+ return node.nodeName === 'DIV' &&
336
+ highlightRegEx.test(node.className)
337
+ },
338
+ replacement: function (content) {
339
+ return '\n\n' + content + '\n\n'
340
+ }
341
+ }
342
+ ]
343
+
344
+ },{}],3:[function(require,module,exports){
345
+ /*
346
+ * Set up window for Node.js
347
+ */
348
+
349
+ var _window = (typeof window !== 'undefined' ? window : this)
350
+
351
+ /*
352
+ * Parsing HTML strings
353
+ */
354
+
355
+ function canParseHtmlNatively () {
356
+ var Parser = _window.DOMParser
357
+ var canParse = false
358
+
359
+ // Adapted from https://gist.github.com/1129031
360
+ // Firefox/Opera/IE throw errors on unsupported types
361
+ try {
362
+ // WebKit returns null on unsupported types
363
+ if (new Parser().parseFromString('', 'text/html')) {
364
+ canParse = true
365
+ }
366
+ } catch (e) {}
367
+
368
+ return canParse
369
+ }
370
+
371
+ function createHtmlParser () {
372
+ var Parser = function () {}
373
+
374
+ // For Node.js environments
375
+ if (typeof document === 'undefined') {
376
+ var jsdom = require('jsdom')
377
+ Parser.prototype.parseFromString = function (string) {
378
+ return jsdom.jsdom(string, {
379
+ features: {
380
+ FetchExternalResources: [],
381
+ ProcessExternalResources: false
382
+ }
383
+ })
384
+ }
385
+ } else {
386
+ if (!shouldUseActiveX()) {
387
+ Parser.prototype.parseFromString = function (string) {
388
+ var doc = document.implementation.createHTMLDocument('')
389
+ doc.open()
390
+ doc.write(string)
391
+ doc.close()
392
+ return doc
393
+ }
394
+ } else {
395
+ Parser.prototype.parseFromString = function (string) {
396
+ var doc = new window.ActiveXObject('htmlfile')
397
+ doc.designMode = 'on' // disable on-page scripts
398
+ doc.open()
399
+ doc.write(string)
400
+ doc.close()
401
+ return doc
402
+ }
403
+ }
404
+ }
405
+ return Parser
406
+ }
407
+
408
+ function shouldUseActiveX () {
409
+ var useActiveX = false
410
+
411
+ try {
412
+ document.implementation.createHTMLDocument('').open()
413
+ } catch (e) {
414
+ if (window.ActiveXObject) useActiveX = true
415
+ }
416
+
417
+ return useActiveX
418
+ }
419
+
420
+ module.exports = canParseHtmlNatively() ? _window.DOMParser : createHtmlParser()
421
+
422
+ },{"jsdom":6}],4:[function(require,module,exports){
423
+ 'use strict'
424
+
425
+ module.exports = [
426
+ {
427
+ filter: 'p',
428
+ replacement: function (content) {
429
+ return '\n\n' + content + '\n\n'
430
+ }
431
+ },
432
+
433
+ {
434
+ filter: 'br',
435
+ replacement: function () {
436
+ return ' \n'
437
+ }
438
+ },
439
+
440
+ {
441
+ filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
442
+ replacement: function (content, node) {
443
+ var hLevel = node.nodeName.charAt(1)
444
+ var hPrefix = ''
445
+ for (var i = 0; i < hLevel; i++) {
446
+ hPrefix += '#'
447
+ }
448
+ return '\n\n' + hPrefix + ' ' + content + '\n\n'
449
+ }
450
+ },
451
+
452
+ {
453
+ filter: 'hr',
454
+ replacement: function () {
455
+ return '\n\n* * *\n\n'
456
+ }
457
+ },
458
+
459
+ {
460
+ filter: ['em', 'i'],
461
+ replacement: function (content) {
462
+ return '_' + content + '_'
463
+ }
464
+ },
465
+
466
+ {
467
+ filter: ['strong', 'b'],
468
+ replacement: function (content) {
469
+ return '**' + content + '**'
470
+ }
471
+ },
472
+
473
+ // Inline code
474
+ {
475
+ filter: function (node) {
476
+ var hasSiblings = node.previousSibling || node.nextSibling
477
+ var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
478
+
479
+ return node.nodeName === 'CODE' && !isCodeBlock
480
+ },
481
+ replacement: function (content) {
482
+ return '`' + content + '`'
483
+ }
484
+ },
485
+
486
+ {
487
+ filter: function (node) {
488
+ return node.nodeName === 'A' && node.getAttribute('href')
489
+ },
490
+ replacement: function (content, node) {
491
+ var titlePart = node.title ? ' "' + node.title + '"' : ''
492
+ return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'
493
+ }
494
+ },
495
+
496
+ {
497
+ filter: 'img',
498
+ replacement: function (content, node) {
499
+ var alt = node.alt || ''
500
+ var src = node.getAttribute('src') || ''
501
+ var title = node.title || ''
502
+ var titlePart = title ? ' "' + title + '"' : ''
503
+ return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
504
+ }
505
+ },
506
+
507
+ // Code blocks
508
+ {
509
+ filter: function (node) {
510
+ return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'
511
+ },
512
+ replacement: function (content, node) {
513
+ return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'
514
+ }
515
+ },
516
+
517
+ {
518
+ filter: 'blockquote',
519
+ replacement: function (content) {
520
+ content = content.trim()
521
+ content = content.replace(/\n{3,}/g, '\n\n')
522
+ content = content.replace(/^/gm, '> ')
523
+ return '\n\n' + content + '\n\n'
524
+ }
525
+ },
526
+
527
+ {
528
+ filter: 'li',
529
+ replacement: function (content, node) {
530
+ content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ')
531
+ var prefix = '* '
532
+ var parent = node.parentNode
533
+ var index = Array.prototype.indexOf.call(parent.children, node) + 1
534
+
535
+ prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '
536
+ return prefix + content
537
+ }
538
+ },
539
+
540
+ {
541
+ filter: ['ul', 'ol'],
542
+ replacement: function (content, node) {
543
+ var strings = []
544
+ for (var i = 0; i < node.childNodes.length; i++) {
545
+ strings.push(node.childNodes[i]._replacement)
546
+ }
547
+
548
+ if (/li/i.test(node.parentNode.nodeName)) {
549
+ return '\n' + strings.join('\n')
550
+ }
551
+ return '\n\n' + strings.join('\n') + '\n\n'
552
+ }
553
+ },
554
+
555
+ {
556
+ filter: function (node) {
557
+ return this.isBlock(node)
558
+ },
559
+ replacement: function (content, node) {
560
+ // return '\n\n' + this.outer(node, content) + '\n\n'
561
+ return '\n\n' + content + '\n\n'
562
+ }
563
+ },
564
+
565
+ // Anything else!
566
+ {
567
+ filter: function () {
568
+ return true
569
+ },
570
+ replacement: function (content, node) {
571
+ // return this.outer(node, content)
572
+ return content
573
+ }
574
+ }
575
+ ]
576
+
577
+ },{}],5:[function(require,module,exports){
578
+ /**
579
+ * This file automatically generated from `build.js`.
580
+ * Do not manually edit.
581
+ */
582
+
583
+ module.exports = [
584
+ "address",
585
+ "article",
586
+ "aside",
587
+ "audio",
588
+ "blockquote",
589
+ "canvas",
590
+ "dd",
591
+ "div",
592
+ "dl",
593
+ "fieldset",
594
+ "figcaption",
595
+ "figure",
596
+ "footer",
597
+ "form",
598
+ "h1",
599
+ "h2",
600
+ "h3",
601
+ "h4",
602
+ "h5",
603
+ "h6",
604
+ "header",
605
+ "hgroup",
606
+ "hr",
607
+ "main",
608
+ "nav",
609
+ "noscript",
610
+ "ol",
611
+ "output",
612
+ "p",
613
+ "pre",
614
+ "section",
615
+ "table",
616
+ "tfoot",
617
+ "ul",
618
+ "video"
619
+ ];
620
+
621
+ },{}],6:[function(require,module,exports){
622
+
623
+ },{}],7:[function(require,module,exports){
624
+ 'use strict';
625
+
626
+ var voidElements = require('void-elements');
627
+ Object.keys(voidElements).forEach(function (name) {
628
+ voidElements[name.toUpperCase()] = 1;
629
+ });
630
+
631
+ var blockElements = {};
632
+ require('block-elements').forEach(function (name) {
633
+ blockElements[name.toUpperCase()] = 1;
634
+ });
635
+
636
+ /**
637
+ * isBlockElem(node) determines if the given node is a block element.
638
+ *
639
+ * @param {Node} node
640
+ * @return {Boolean}
641
+ */
642
+ function isBlockElem(node) {
643
+ return !!(node && blockElements[node.nodeName]);
644
+ }
645
+
646
+ /**
647
+ * isVoid(node) determines if the given node is a void element.
648
+ *
649
+ * @param {Node} node
650
+ * @return {Boolean}
651
+ */
652
+ function isVoid(node) {
653
+ return !!(node && voidElements[node.nodeName]);
654
+ }
655
+
656
+ /**
657
+ * whitespace(elem [, isBlock]) removes extraneous whitespace from an
658
+ * the given element. The function isBlock may optionally be passed in
659
+ * to determine whether or not an element is a block element; if none
660
+ * is provided, defaults to using the list of block elements provided
661
+ * by the `block-elements` module.
662
+ *
663
+ * @param {Node} elem
664
+ * @param {Function} blockTest
665
+ */
666
+ function collapseWhitespace(elem, isBlock) {
667
+ if (!elem.firstChild || elem.nodeName === 'PRE') return;
668
+
669
+ if (typeof isBlock !== 'function') {
670
+ isBlock = isBlockElem;
671
+ }
672
+
673
+ var prevText = null;
674
+ var prevVoid = false;
675
+
676
+ var prev = null;
677
+ var node = next(prev, elem);
678
+
679
+ while (node !== elem) {
680
+ if (node.nodeType === 3) {
681
+ // Node.TEXT_NODE
682
+ var text = node.data.replace(/[ \r\n\t]+/g, ' ');
683
+
684
+ if ((!prevText || / $/.test(prevText.data)) && !prevVoid && text[0] === ' ') {
685
+ text = text.substr(1);
686
+ }
687
+
688
+ // `text` might be empty at this point.
689
+ if (!text) {
690
+ node = remove(node);
691
+ continue;
692
+ }
693
+
694
+ node.data = text;
695
+ prevText = node;
696
+ } else if (node.nodeType === 1) {
697
+ // Node.ELEMENT_NODE
698
+ if (isBlock(node) || node.nodeName === 'BR') {
699
+ if (prevText) {
700
+ prevText.data = prevText.data.replace(/ $/, '');
701
+ }
702
+
703
+ prevText = null;
704
+ prevVoid = false;
705
+ } else if (isVoid(node)) {
706
+ // Avoid trimming space around non-block, non-BR void elements.
707
+ prevText = null;
708
+ prevVoid = true;
709
+ }
710
+ } else {
711
+ node = remove(node);
712
+ continue;
713
+ }
714
+
715
+ var nextNode = next(prev, node);
716
+ prev = node;
717
+ node = nextNode;
718
+ }
719
+
720
+ if (prevText) {
721
+ prevText.data = prevText.data.replace(/ $/, '');
722
+ if (!prevText.data) {
723
+ remove(prevText);
724
+ }
725
+ }
726
+ }
727
+
728
+ /**
729
+ * remove(node) removes the given node from the DOM and returns the
730
+ * next node in the sequence.
731
+ *
732
+ * @param {Node} node
733
+ * @return {Node} node
734
+ */
735
+ function remove(node) {
736
+ var next = node.nextSibling || node.parentNode;
737
+
738
+ node.parentNode.removeChild(node);
739
+
740
+ return next;
741
+ }
742
+
743
+ /**
744
+ * next(prev, current) returns the next node in the sequence, given the
745
+ * current and previous nodes.
746
+ *
747
+ * @param {Node} prev
748
+ * @param {Node} current
749
+ * @return {Node}
750
+ */
751
+ function next(prev, current) {
752
+ if (prev && prev.parentNode === current || current.nodeName === 'PRE') {
753
+ return current.nextSibling || current.parentNode;
754
+ }
755
+
756
+ return current.firstChild || current.nextSibling || current.parentNode;
757
+ }
758
+
759
+ module.exports = collapseWhitespace;
760
+
761
+ },{"block-elements":5,"void-elements":8}],8:[function(require,module,exports){
762
+ /**
763
+ * This file automatically generated from `pre-publish.js`.
764
+ * Do not manually edit.
765
+ */
766
+
767
+ module.exports = {
768
+ "area": true,
769
+ "base": true,
770
+ "br": true,
771
+ "col": true,
772
+ "embed": true,
773
+ "hr": true,
774
+ "img": true,
775
+ "input": true,
776
+ "keygen": true,
777
+ "link": true,
778
+ "menuitem": true,
779
+ "meta": true,
780
+ "param": true,
781
+ "source": true,
782
+ "track": true,
783
+ "wbr": true
784
+ };
785
+
786
+ },{}]},{},[1])(1)
787
+ });