|
| 1 | +var TurndownPluginGfmService = (function (exports) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var highlightRegExp = /highlight-(?:text|source)-([a-z0-9]+)/; |
| 5 | + |
| 6 | + function highlightedCodeBlock (turndownService) { |
| 7 | + turndownService.addRule('highlightedCodeBlock', { |
| 8 | + filter: function (node) { |
| 9 | + var firstChild = node.firstChild; |
| 10 | + return ( |
| 11 | + node.nodeName === 'DIV' && |
| 12 | + highlightRegExp.test(node.className) && |
| 13 | + firstChild && |
| 14 | + firstChild.nodeName === 'PRE' |
| 15 | + ) |
| 16 | + }, |
| 17 | + replacement: function (content, node, options) { |
| 18 | + var className = node.className || ''; |
| 19 | + var language = (className.match(highlightRegExp) || [null, ''])[1]; |
| 20 | + |
| 21 | + return ( |
| 22 | + '\n\n' + options.fence + language + '\n' + |
| 23 | + node.firstChild.textContent + |
| 24 | + '\n' + options.fence + '\n\n' |
| 25 | + ) |
| 26 | + } |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + function strikethrough (turndownService) { |
| 31 | + turndownService.addRule('strikethrough', { |
| 32 | + filter: ['del', 's', 'strike'], |
| 33 | + replacement: function (content) { |
| 34 | + return '~' + content + '~' |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + var indexOf = Array.prototype.indexOf; |
| 40 | + var rules = {}; |
| 41 | + |
| 42 | + rules.tableCell = { |
| 43 | + filter: ['th', 'td'], |
| 44 | + replacement: function (content, node) { |
| 45 | + return cell(content, node) + spannedCells(node, '') |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + rules.tableRow = { |
| 50 | + filter: 'tr', |
| 51 | + replacement: function (content, node) { |
| 52 | + var borderCells = ''; |
| 53 | + var alignMap = { left: ':--', right: '--:', center: ':-:' }; |
| 54 | + |
| 55 | + if (isHeadingRow(node)) { |
| 56 | + for (var i = 0; i < node.childNodes.length; i++) { |
| 57 | + var border = '---'; |
| 58 | + var align = ( |
| 59 | + node.childNodes[i].getAttribute('align') || '' |
| 60 | + ).toLowerCase(); |
| 61 | + |
| 62 | + if (align) border = alignMap[align] || border; |
| 63 | + |
| 64 | + borderCells += cell(border, node.childNodes[i]) + spannedCells(node.childNodes[i], border); |
| 65 | + } |
| 66 | + } |
| 67 | + return '\n' + content + (borderCells ? '\n' + borderCells : '') |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + rules.table = { |
| 72 | + // Only convert tables that are not nested in another table, they are kept using `keep` (see below). |
| 73 | + // TODO: nested tables should be converted to plain text in a strict (non HTML) gfm |
| 74 | + filter: function (node) { |
| 75 | + return node.nodeName === 'TABLE' && !isNestedTable(node) |
| 76 | + }, |
| 77 | + |
| 78 | + replacement: function (content) { |
| 79 | + // Ensure there are no blank lines |
| 80 | + content = content.replace('\n\n', '\n'); |
| 81 | + return '\n\n' + content + '\n\n' |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + rules.tableSection = { |
| 86 | + filter: ['thead', 'tbody', 'tfoot'], |
| 87 | + replacement: function (content) { |
| 88 | + return content |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + rules.captionSection = { |
| 93 | + // only return content if caption if the first node immediately after TABLE |
| 94 | + filter: 'caption', |
| 95 | + replacement: function (content, node) { |
| 96 | + if (node.parentNode.nodeName === 'TABLE' && node.parentNode.childNodes[0] === node) return content |
| 97 | + return '' |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + function isHeadingRow (tr) { |
| 102 | + var parentNode = tr.parentNode; |
| 103 | + var tableNode = parentNode; |
| 104 | + if (parentNode.nodeName === 'THEAD' || |
| 105 | + parentNode.nodeName === 'TFOOT' || |
| 106 | + parentNode.nodeName === 'TBODY') { |
| 107 | + tableNode = parentNode.parentNode; |
| 108 | + } |
| 109 | + return (tableNode.nodeName === 'TABLE' && tableNode.rows[0] === tr) |
| 110 | + } |
| 111 | + |
| 112 | + function cell (content, node) { |
| 113 | + var index = indexOf.call(node.parentNode.childNodes, node); |
| 114 | + var prefix = ' '; |
| 115 | + if (index === 0) prefix = '| '; |
| 116 | + // Ensure single line per cell (both windows and unix EoL) |
| 117 | + content = content.replace(/\r\n/g, '\n').replace(/^\n+/g, '').replace(/\n+/g, '<br />'); |
| 118 | + // | must be escaped as \| |
| 119 | + content = content.replace(/\|/g, '\\|'); |
| 120 | + return prefix + content + ' |' |
| 121 | + } |
| 122 | + |
| 123 | + function spannedCells (node, spannedCellContent) { |
| 124 | + var colspan = node.getAttribute('colspan') || 1; |
| 125 | + if (colspan <= 1) return '' |
| 126 | + return (' ' + spannedCellContent + ' |').repeat(colspan - 1) |
| 127 | + } |
| 128 | + |
| 129 | + function isNestedTable (tableNode) { |
| 130 | + var currentNode = tableNode.parentNode; |
| 131 | + while (currentNode) { |
| 132 | + if (currentNode.nodeName === 'TABLE') return true |
| 133 | + currentNode = currentNode.parentNode; |
| 134 | + } |
| 135 | + return false |
| 136 | + } |
| 137 | + |
| 138 | + function tables (turndownService) { |
| 139 | + turndownService.keep(function (node) { |
| 140 | + return node.nodeName === 'TABLE' && isNestedTable(node) |
| 141 | + }); |
| 142 | + for (var key in rules) turndownService.addRule(key, rules[key]); |
| 143 | + } |
| 144 | + |
| 145 | + function taskListItems (turndownService) { |
| 146 | + turndownService.addRule('taskListItems', { |
| 147 | + filter: function (node) { |
| 148 | + return node.type === 'checkbox' && node.parentNode.nodeName === 'LI' |
| 149 | + }, |
| 150 | + replacement: function (content, node) { |
| 151 | + return (node.checked ? '[x]' : '[ ]') + ' ' |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + function gfm (turndownService) { |
| 157 | + turndownService.use([ |
| 158 | + highlightedCodeBlock, |
| 159 | + strikethrough, |
| 160 | + tables, |
| 161 | + taskListItems |
| 162 | + ]); |
| 163 | + } |
| 164 | + |
| 165 | + exports.gfm = gfm; |
| 166 | + exports.highlightedCodeBlock = highlightedCodeBlock; |
| 167 | + exports.strikethrough = strikethrough; |
| 168 | + exports.tables = tables; |
| 169 | + exports.taskListItems = taskListItems; |
| 170 | + |
| 171 | + Object.defineProperty(exports, '__esModule', { value: true }); |
| 172 | + |
| 173 | + return exports; |
| 174 | + |
| 175 | +})({}); |
0 commit comments