github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/to-markdown/lib/gfm-converters.js (about) 1 'use strict'; 2 3 function cell(content, node) { 4 var index = Array.prototype.indexOf.call(node.parentNode.childNodes, node); 5 var prefix = ' '; 6 if (index === 0) { prefix = '| '; } 7 return prefix + content + ' |'; 8 } 9 10 var highlightRegEx = /highlight highlight-(\S+)/; 11 12 module.exports = [ 13 { 14 filter: 'br', 15 replacement: function () { 16 return '\n'; 17 } 18 }, 19 { 20 filter: ['del', 's', 'strike'], 21 replacement: function (content) { 22 return '~~' + content + '~~'; 23 } 24 }, 25 26 { 27 filter: function (node) { 28 return node.type === 'checkbox' && node.parentNode.nodeName === 'LI'; 29 }, 30 replacement: function (content, node) { 31 return (node.checked ? '[x]' : '[ ]') + ' '; 32 } 33 }, 34 35 { 36 filter: ['th', 'td'], 37 replacement: function (content, node) { 38 return cell(content, node); 39 } 40 }, 41 42 { 43 filter: 'tr', 44 replacement: function (content, node) { 45 var borderCells = ''; 46 var alignMap = { left: ':--', right: '--:', center: ':-:' }; 47 48 if (node.parentNode.nodeName === 'THEAD') { 49 for (var i = 0; i < node.childNodes.length; i++) { 50 var align = node.childNodes[i].attributes.align; 51 var border = '---'; 52 53 if (align) { border = alignMap[align.value] || border; } 54 55 borderCells += cell(border, node.childNodes[i]); 56 } 57 } 58 return '\n' + content + (borderCells ? '\n' + borderCells : ''); 59 } 60 }, 61 62 { 63 filter: 'table', 64 replacement: function (content) { 65 return '\n\n' + content + '\n\n'; 66 } 67 }, 68 69 { 70 filter: ['thead', 'tbody', 'tfoot'], 71 replacement: function (content) { 72 return content; 73 } 74 }, 75 76 // Fenced code blocks 77 { 78 filter: function (node) { 79 return node.nodeName === 'PRE' && 80 node.firstChild && 81 node.firstChild.nodeName === 'CODE'; 82 }, 83 replacement: function(content, node) { 84 return '\n\n```\n' + node.firstChild.textContent + '\n```\n\n'; 85 } 86 }, 87 88 // Syntax-highlighted code blocks 89 { 90 filter: function (node) { 91 return node.nodeName === 'PRE' && 92 node.parentNode.nodeName === 'DIV' && 93 highlightRegEx.test(node.parentNode.className); 94 }, 95 replacement: function (content, node) { 96 var language = node.parentNode.className.match(highlightRegEx)[1]; 97 return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n'; 98 } 99 }, 100 101 { 102 filter: function (node) { 103 return node.nodeName === 'DIV' && 104 highlightRegEx.test(node.className); 105 }, 106 replacement: function (content) { 107 return '\n\n' + content + '\n\n'; 108 } 109 } 110 ];