github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/to-markdown/dist/to-markdown.js (about) 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-15, 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 collapse = require('collapse-whitespace'); 17 18 /* 19 * Set up window and document for Node.js 20 */ 21 22 var _window = (typeof window !== 'undefined' ? window : this), _document; 23 if (typeof document === 'undefined') { 24 _document = require('jsdom').jsdom(); 25 } 26 else { 27 _document = document; 28 } 29 30 /* 31 * Utilities 32 */ 33 34 function trim(string) { 35 return string.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g, ''); 36 } 37 38 var blocks = ['address', 'article', 'aside', 'audio', 'blockquote', 'body', 39 'canvas', 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption', 40 'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4','h5', 'h6', 41 'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav', 42 'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table', 43 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul' 44 ]; 45 46 function isBlock(node) { 47 return blocks.indexOf(node.nodeName.toLowerCase()) !== -1; 48 } 49 50 var voids = [ 51 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 52 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' 53 ]; 54 55 function isVoid(node) { 56 return voids.indexOf(node.nodeName.toLowerCase()) !== -1; 57 } 58 59 /* 60 * Parsing HTML strings 61 */ 62 63 function canParseHtml() { 64 var Parser = _window.DOMParser, canParse = false; 65 66 // Adapted from https://gist.github.com/1129031 67 // Firefox/Opera/IE throw errors on unsupported types 68 try { 69 // WebKit returns null on unsupported types 70 if (new Parser().parseFromString('', 'text/html')) { 71 canParse = true; 72 } 73 } catch (e) {} 74 return canParse; 75 } 76 77 function createHtmlParser() { 78 var Parser = function () {}; 79 80 Parser.prototype.parseFromString = function (string) { 81 var newDoc = _document.implementation.createHTMLDocument(''); 82 83 if (string.toLowerCase().indexOf('<!doctype') > -1) { 84 newDoc.documentElement.innerHTML = string; 85 } 86 else { 87 newDoc.body.innerHTML = string; 88 } 89 return newDoc; 90 }; 91 return Parser; 92 } 93 94 var HtmlParser = canParseHtml() ? _window.DOMParser : createHtmlParser(); 95 96 function htmlToDom(string) { 97 var tree = new HtmlParser().parseFromString(string, 'text/html'); 98 collapse(tree, isBlock); 99 return tree; 100 } 101 102 /* 103 * Flattens DOM tree into single array 104 */ 105 106 function bfsOrder(node) { 107 var inqueue = [node], 108 outqueue = [], 109 elem, children, i; 110 111 while (inqueue.length > 0) { 112 elem = inqueue.shift(); 113 outqueue.push(elem); 114 children = elem.childNodes; 115 for (i = 0 ; i < children.length; i++) { 116 if (children[i].nodeType === 1) { inqueue.push(children[i]); } 117 } 118 } 119 outqueue.shift(); 120 return outqueue; 121 } 122 123 /* 124 * Contructs a Markdown string of replacement text for a given node 125 */ 126 127 function getContent(node) { 128 var text = ''; 129 for (var i = 0; i < node.childNodes.length; i++) { 130 if (node.childNodes[i].nodeType === 1) { 131 text += node.childNodes[i]._replacement; 132 } 133 else if (node.childNodes[i].nodeType === 3) { 134 text += node.childNodes[i].data; 135 } 136 else { continue; } 137 } 138 return text; 139 } 140 141 /* 142 * Returns the HTML string of an element with its contents converted 143 */ 144 145 function outer(node, content) { 146 return node.cloneNode(false).outerHTML.replace('><', '>'+ content +'<'); 147 } 148 149 function canConvert(node, filter) { 150 if (typeof filter === 'string') { 151 return filter === node.nodeName.toLowerCase(); 152 } 153 if (Array.isArray(filter)) { 154 return filter.indexOf(node.nodeName.toLowerCase()) !== -1; 155 } 156 else if (typeof filter === 'function') { 157 return filter.call(toMarkdown, node); 158 } 159 else { 160 throw new TypeError('`filter` needs to be a string, array, or function'); 161 } 162 } 163 164 function isFlankedByWhitespace(side, node) { 165 var sibling, regExp, isFlanked; 166 167 if (side === 'left') { 168 sibling = node.previousSibling; 169 regExp = / $/; 170 } 171 else { 172 sibling = node.nextSibling; 173 regExp = /^ /; 174 } 175 176 if (sibling) { 177 if (sibling.nodeType === 3) { 178 isFlanked = regExp.test(sibling.nodeValue); 179 } 180 else if(sibling.nodeType === 1 && !isBlock(sibling)) { 181 isFlanked = regExp.test(sibling.textContent); 182 } 183 } 184 return isFlanked; 185 } 186 187 function flankingWhitespace(node) { 188 var leading = '', trailing = ''; 189 190 if (!isBlock(node)) { 191 var hasLeading = /^[ \r\n\t]/.test(node.innerHTML), 192 hasTrailing = /[ \r\n\t]$/.test(node.innerHTML); 193 194 if (hasLeading && !isFlankedByWhitespace('left', node)) { 195 leading = ' '; 196 } 197 if (hasTrailing && !isFlankedByWhitespace('right', node)) { 198 trailing = ' '; 199 } 200 } 201 202 return { leading: leading, trailing: trailing }; 203 } 204 205 /* 206 * Finds a Markdown converter, gets the replacement, and sets it on 207 * `_replacement` 208 */ 209 210 function process(node) { 211 var replacement, content = getContent(node); 212 213 for (var i = 0; i < converters.length; i++) { 214 var converter = converters[i]; 215 216 if (canConvert(node, converter.filter)) { 217 if (typeof converter.replacement !== 'function') { 218 throw new TypeError( 219 '`replacement` needs to be a function that returns a string' 220 ); 221 } 222 223 var whitespace = flankingWhitespace(node); 224 225 if (whitespace.leading || whitespace.trailing) { 226 content = trim(content); 227 } 228 replacement = whitespace.leading + 229 converter.replacement.call(toMarkdown, content, node) + 230 whitespace.trailing; 231 break; 232 } 233 } 234 235 // Remove blank nodes 236 if (!isVoid(node) && !/A/.test(node.nodeName) && /^\s*$/i.test(content)) { 237 replacement = ''; 238 } 239 240 node._replacement = replacement; 241 } 242 243 toMarkdown = function (input, options) { 244 options = options || {}; 245 246 if (typeof input !== 'string') { 247 throw new TypeError(input + ' is not a string'); 248 } 249 250 // Escape potential ol triggers 251 input = input.replace(/(\d+)\. /g, '$1\\. '); 252 253 var clone = htmlToDom(input).body, 254 nodes = bfsOrder(clone), 255 output; 256 257 converters = mdConverters.slice(0); 258 if (options.gfm) { 259 converters = gfmConverters.concat(converters); 260 } 261 262 if (options.converters) { 263 converters = options.converters.concat(converters); 264 } 265 266 // Process through nodes in reverse (so deepest child elements are first). 267 for (var i = nodes.length - 1; i >= 0; i--) { 268 process(nodes[i]); 269 } 270 output = getContent(clone); 271 272 return output.replace(/^[\t\r\n]+|[\t\r\n\s]+$/g, '') 273 .replace(/\n\s+\n/g, '\n\n') 274 .replace(/\n{3,}/g, '\n\n'); 275 }; 276 277 toMarkdown.isBlock = isBlock; 278 toMarkdown.isVoid = isVoid; 279 toMarkdown.trim = trim; 280 toMarkdown.outer = outer; 281 282 module.exports = toMarkdown; 283 284 },{"./lib/gfm-converters":2,"./lib/md-converters":3,"collapse-whitespace":5,"jsdom":6}],2:[function(require,module,exports){ 285 'use strict'; 286 287 function cell(content, node) { 288 var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node); 289 var prefix = ' '; 290 if (index === 0) { prefix = '| '; } 291 return prefix + content + ' |'; 292 } 293 294 var highlightRegEx = /highlight highlight-(\S+)/; 295 296 module.exports = [ 297 { 298 filter: 'br', 299 replacement: function () { 300 return '\n'; 301 } 302 }, 303 { 304 filter: ['del', 's', 'strike'], 305 replacement: function (content) { 306 return '~~' + content + '~~'; 307 } 308 }, 309 310 { 311 filter: function (node) { 312 return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'; 313 }, 314 replacement: function (content, node) { 315 return (node.checked ? '[x]' : '[ ]') + ' '; 316 } 317 }, 318 319 { 320 filter: ['th', 'td'], 321 replacement: function (content, node) { 322 return cell(content, node); 323 } 324 }, 325 326 { 327 filter: 'tr', 328 replacement: function (content, node) { 329 var borderCells = ''; 330 var alignMap = { left: ':--', right: '--:', center: ':-:' }; 331 332 if (node.parentNode.nodeName === 'THEAD') { 333 for (var i = 0; i < node.childNodes.length; i++) { 334 var align = node.childNodes[i].attributes.align; 335 var border = '---'; 336 337 if (align) { border = alignMap[align.value] || border; } 338 339 borderCells += cell(border, node.childNodes[i]); 340 } 341 } 342 return '\n' + content + (borderCells ? '\n' + borderCells : ''); 343 } 344 }, 345 346 { 347 filter: 'table', 348 replacement: function (content) { 349 return '\n\n' + content + '\n\n'; 350 } 351 }, 352 353 { 354 filter: ['thead', 'tbody', 'tfoot'], 355 replacement: function (content) { 356 return content; 357 } 358 }, 359 360 // Fenced code blocks 361 { 362 filter: function (node) { 363 return node.nodeName === 'PRE' && 364 node.firstChild && 365 node.firstChild.nodeName === 'CODE'; 366 }, 367 replacement: function(content, node) { 368 return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'; 369 } 370 }, 371 372 // Syntax-highlighted code blocks 373 { 374 filter: function (node) { 375 return node.nodeName === 'PRE' && 376 node.parentNode.nodeName === 'DIV' && 377 highlightRegEx.test(node.parentNode.className); 378 }, 379 replacement: function (content, node) { 380 var language = node.parentNode.className.match(highlightRegEx)[1]; 381 return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'; 382 } 383 }, 384 385 { 386 filter: function (node) { 387 return node.nodeName === 'DIV' && 388 highlightRegEx.test(node.className); 389 }, 390 replacement: function (content) { 391 return '\n\n' + content + '\n\n'; 392 } 393 } 394 ]; 395 396 },{}],3:[function(require,module,exports){ 397 'use strict'; 398 399 module.exports = [ 400 { 401 filter: 'p', 402 replacement: function (content) { 403 return '\n\n' + content + '\n\n'; 404 } 405 }, 406 407 { 408 filter: 'br', 409 replacement: function () { 410 return ' \n'; 411 } 412 }, 413 414 { 415 filter: ['h1', 'h2', 'h3', 'h4','h5', 'h6'], 416 replacement: function(content, node) { 417 var hLevel = node.nodeName.charAt(1); 418 var hPrefix = ''; 419 for(var i = 0; i < hLevel; i++) { 420 hPrefix += '#'; 421 } 422 return '\n\n' + hPrefix + ' ' + content + '\n\n'; 423 } 424 }, 425 426 { 427 filter: 'hr', 428 replacement: function () { 429 return '\n\n* * *\n\n'; 430 } 431 }, 432 433 { 434 filter: ['em', 'i'], 435 replacement: function (content) { 436 return '_' + content + '_'; 437 } 438 }, 439 440 { 441 filter: ['strong', 'b'], 442 replacement: function (content) { 443 return '**' + content + '**'; 444 } 445 }, 446 447 // Inline code 448 { 449 filter: function (node) { 450 var hasSiblings = node.previousSibling || node.nextSibling; 451 var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings; 452 453 return node.nodeName === 'CODE' && !isCodeBlock; 454 }, 455 replacement: function(content) { 456 return '`' + content + '`'; 457 } 458 }, 459 460 { 461 filter: function (node) { 462 return node.nodeName === 'A' && node.getAttribute('href'); 463 }, 464 replacement: function(content, node) { 465 var titlePart = node.title ? ' "'+ node.title +'"' : ''; 466 return '[' + content + '](' + node.getAttribute('href') + titlePart + ')'; 467 } 468 }, 469 470 { 471 filter: 'img', 472 replacement: function(content, node) { 473 var alt = node.alt || ''; 474 var src = node.getAttribute('src') || ''; 475 var title = node.title || ''; 476 var titlePart = title ? ' "'+ title +'"' : ''; 477 return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''; 478 } 479 }, 480 481 // Code blocks 482 { 483 filter: function (node) { 484 return node.nodeName === 'PRE' && node.firstChild.nodeName === 'CODE'; 485 }, 486 replacement: function(content, node) { 487 return '\n\n ' + node.firstChild.textContent.replace(/\n/g, '\n ') + '\n\n'; 488 } 489 }, 490 491 { 492 filter: 'blockquote', 493 replacement: function (content) { 494 content = this.trim(content); 495 content = content.replace(/\n{3,}/g, '\n\n'); 496 content = content.replace(/^/gm, '> '); 497 return '\n\n' + content + '\n\n'; 498 } 499 }, 500 501 { 502 filter: 'li', 503 replacement: function (content, node) { 504 content = content.replace(/^\s+/, '').replace(/\n/gm, '\n '); 505 var prefix = '* '; 506 var parent = node.parentNode; 507 var index = Array.prototype.indexOf.call(parent.children, node) + 1; 508 509 prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '* '; 510 return prefix + content; 511 } 512 }, 513 514 { 515 filter: ['ul', 'ol'], 516 replacement: function (content, node) { 517 var strings = []; 518 for (var i = 0; i < node.childNodes.length; i++) { 519 strings.push(node.childNodes[i]._replacement); 520 } 521 522 if (/li/i.test(node.parentNode.nodeName)) { 523 return '\n' + strings.join('\n'); 524 } 525 return '\n\n' + strings.join('\n') + '\n\n'; 526 } 527 }, 528 529 { 530 filter: function (node) { 531 return this.isBlock(node); 532 }, 533 replacement: function (content, node) { 534 return '\n\n' + this.outer(node, content) + '\n\n'; 535 } 536 }, 537 538 // Anything else! 539 { 540 filter: function () { 541 return true; 542 }, 543 replacement: function (content, node) { 544 return this.outer(node, content); 545 } 546 } 547 ]; 548 },{}],4:[function(require,module,exports){ 549 /** 550 * This file automatically generated from `build.js`. 551 * Do not manually edit. 552 */ 553 554 module.exports = [ 555 "address", 556 "article", 557 "aside", 558 "audio", 559 "blockquote", 560 "canvas", 561 "dd", 562 "div", 563 "dl", 564 "fieldset", 565 "figcaption", 566 "figure", 567 "footer", 568 "form", 569 "h1", 570 "h2", 571 "h3", 572 "h4", 573 "h5", 574 "h6", 575 "header", 576 "hgroup", 577 "hr", 578 "noscript", 579 "ol", 580 "output", 581 "p", 582 "pre", 583 "section", 584 "table", 585 "tfoot", 586 "ul", 587 "video" 588 ]; 589 590 },{}],5:[function(require,module,exports){ 591 'use strict'; 592 593 var blocks = require('block-elements').map(function (name) { 594 return name.toUpperCase() 595 }) 596 597 function defaultBlockTest (node) { 598 return isElem(node) && blocks.indexOf(node.nodeName) >= 0 599 } 600 601 function isText (node) { 602 return node && node.nodeType === 3 // Node.TEXT_NODE 603 } 604 605 function isElem (node) { 606 return node && node.nodeType === 1 // Node.ELEMENT_NODE 607 } 608 609 /** 610 * whitespace(elem [, isBlock]) removes extraneous whitespace from an 611 * the given element. The function isBlock may optionally be passed in 612 * to determine whether or not an element is a block element; if none 613 * is provided, defaults to using the list of block elements provided 614 * by the `block-elements` module. 615 * 616 * @param {Element} root 617 * @param {Function} isBlock 618 */ 619 function whitespace (root, isBlock) { 620 var startSpace = /^ /, 621 endSpace = / $/, 622 nextNode, 623 prevNode, 624 prevText, 625 node, 626 text 627 628 if (typeof isBlock !== 'function') 629 isBlock = defaultBlockTest 630 631 function next (node) { 632 while (node && node !== root) { 633 if (node.nextSibling) 634 return node.nextSibling 635 636 node = node.parentNode 637 if (prevText && isBlock(node)) { 638 prevText.data = prevText.data.replace(/[ \r\n\t]$/, '') 639 prevText = null 640 } 641 } 642 643 return null 644 } 645 646 function first (node) { 647 return node.firstChild ? node.firstChild : next(node) 648 } 649 650 function remove (node) { 651 var nextNode = next(node) 652 653 node.parentNode.removeChild(node) 654 return nextNode 655 } 656 657 if (root.nodeName === 'PRE') return 658 659 // Join adjacent text nodes and whatnot. 660 root.normalize() 661 662 node = first(root) 663 while (node) { 664 prevNode = node.previousSibling 665 nextNode = node.nextSibling 666 667 if (isText(node)) { 668 text = node.data.replace(/[ \r\n\t]+/g, ' ') 669 670 if (!prevText || prevNode && isBlock(prevNode)) 671 text = text.replace(startSpace, '') 672 if (nextNode && isBlock(nextNode)) 673 text = text.replace(endSpace, '') 674 675 if (prevText && endSpace.test(prevText.data) && 676 startSpace.test(text)) 677 text = text.substr(1) 678 679 if (text) { 680 node.data = text 681 prevText = node 682 node = next(node) 683 } else { 684 node = remove(node) 685 } 686 } else if (isElem(node)) { 687 if (node.nodeName === 'PRE') { 688 node = next(node) 689 continue 690 } 691 692 if (prevText && isBlock(node)) { 693 prevText.data = prevText.data.replace(endSpace, '') 694 prevText = null 695 } 696 697 node = first(node) 698 } else { 699 node = remove(node) 700 } 701 } 702 703 // Trim trailing space from last text node 704 if (prevText) 705 prevText.data = prevText.data.replace(endSpace, '') 706 } 707 708 module.exports = whitespace 709 710 },{"block-elements":4}],6:[function(require,module,exports){ 711 712 },{}]},{},[1])(1) 713 });