|
1 |
| -exports.fromMarkdown = require('./from-markdown.js') |
2 |
| -exports.toMarkdown = require('./to-markdown.js') |
| 1 | +import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js' |
| 2 | +import defaultInlineCode from 'mdast-util-to-markdown/lib/handle/inline-code.js' |
| 3 | +import {markdownTable} from 'markdown-table' |
| 4 | + |
| 5 | +export const gfmTableFromMarkdown = { |
| 6 | + enter: { |
| 7 | + table: enterTable, |
| 8 | + tableData: enterCell, |
| 9 | + tableHeader: enterCell, |
| 10 | + tableRow: enterRow |
| 11 | + }, |
| 12 | + exit: { |
| 13 | + codeText: exitCodeText, |
| 14 | + table: exitTable, |
| 15 | + tableData: exit, |
| 16 | + tableHeader: exit, |
| 17 | + tableRow: exit |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function enterTable(token) { |
| 22 | + this.enter({type: 'table', align: token._align, children: []}, token) |
| 23 | + this.setData('inTable', true) |
| 24 | +} |
| 25 | + |
| 26 | +function exitTable(token) { |
| 27 | + this.exit(token) |
| 28 | + this.setData('inTable') |
| 29 | +} |
| 30 | + |
| 31 | +function enterRow(token) { |
| 32 | + this.enter({type: 'tableRow', children: []}, token) |
| 33 | +} |
| 34 | + |
| 35 | +function exit(token) { |
| 36 | + this.exit(token) |
| 37 | +} |
| 38 | + |
| 39 | +function enterCell(token) { |
| 40 | + this.enter({type: 'tableCell', children: []}, token) |
| 41 | +} |
| 42 | + |
| 43 | +// Overwrite the default code text data handler to unescape escaped pipes when |
| 44 | +// they are in tables. |
| 45 | +function exitCodeText(token) { |
| 46 | + var value = this.resume() |
| 47 | + |
| 48 | + if (this.getData('inTable')) { |
| 49 | + value = value.replace(/\\([\\|])/g, replace) |
| 50 | + } |
| 51 | + |
| 52 | + this.stack[this.stack.length - 1].value = value |
| 53 | + this.exit(token) |
| 54 | +} |
| 55 | + |
| 56 | +function replace($0, $1) { |
| 57 | + // Pipes work, backslashes don’t (but can’t escape pipes). |
| 58 | + return $1 === '|' ? $1 : $0 |
| 59 | +} |
| 60 | + |
| 61 | +export function gfmTableToMarkdown(options) { |
| 62 | + var settings = options || {} |
| 63 | + var padding = settings.tableCellPadding |
| 64 | + var alignDelimiters = settings.tablePipeAlign |
| 65 | + var stringLength = settings.stringLength |
| 66 | + var around = padding ? ' ' : '|' |
| 67 | + |
| 68 | + return { |
| 69 | + unsafe: [ |
| 70 | + {character: '\r', inConstruct: 'tableCell'}, |
| 71 | + {character: '\n', inConstruct: 'tableCell'}, |
| 72 | + // A pipe, when followed by a tab or space (padding), or a dash or colon |
| 73 | + // (unpadded delimiter row), could result in a table. |
| 74 | + {atBreak: true, character: '|', after: '[\t :-]'}, |
| 75 | + // A pipe in a cell must be encoded. |
| 76 | + {character: '|', inConstruct: 'tableCell'}, |
| 77 | + // A colon must be followed by a dash, in which case it could start a |
| 78 | + // delimiter row. |
| 79 | + {atBreak: true, character: ':', after: '-'}, |
| 80 | + // A delimiter row can also start with a dash, when followed by more |
| 81 | + // dashes, a colon, or a pipe. |
| 82 | + // This is a stricter version than the built in check for lists, thematic |
| 83 | + // breaks, and setex heading underlines though: |
| 84 | + // <https://github.com/syntax-tree/mdast-util-to-markdown/blob/51a2038/lib/unsafe.js#L57> |
| 85 | + {atBreak: true, character: '-', after: '[:|-]'} |
| 86 | + ], |
| 87 | + handlers: { |
| 88 | + table: handleTable, |
| 89 | + tableRow: handleTableRow, |
| 90 | + tableCell: handleTableCell, |
| 91 | + inlineCode: inlineCodeWithTable |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function handleTable(node, _, context) { |
| 96 | + return serializeData(handleTableAsData(node, context), node.align) |
| 97 | + } |
| 98 | + |
| 99 | + // This function isn’t really used normally, because we handle rows at the |
| 100 | + // table level. |
| 101 | + // But, if someone passes in a table row, this ensures we make somewhat sense. |
| 102 | + function handleTableRow(node, _, context) { |
| 103 | + var row = handleTableRowAsData(node, context) |
| 104 | + // `markdown-table` will always add an align row |
| 105 | + var value = serializeData([row]) |
| 106 | + return value.slice(0, value.indexOf('\n')) |
| 107 | + } |
| 108 | + |
| 109 | + function handleTableCell(node, _, context) { |
| 110 | + var exit = context.enter('tableCell') |
| 111 | + var value = phrasing(node, context, {before: around, after: around}) |
| 112 | + exit() |
| 113 | + return value |
| 114 | + } |
| 115 | + |
| 116 | + function serializeData(matrix, align) { |
| 117 | + return markdownTable(matrix, { |
| 118 | + align, |
| 119 | + alignDelimiters, |
| 120 | + padding, |
| 121 | + stringLength |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + function handleTableAsData(node, context) { |
| 126 | + var children = node.children |
| 127 | + var index = -1 |
| 128 | + var length = children.length |
| 129 | + var result = [] |
| 130 | + var subexit = context.enter('table') |
| 131 | + |
| 132 | + while (++index < length) { |
| 133 | + result[index] = handleTableRowAsData(children[index], context) |
| 134 | + } |
| 135 | + |
| 136 | + subexit() |
| 137 | + |
| 138 | + return result |
| 139 | + } |
| 140 | + |
| 141 | + function handleTableRowAsData(node, context) { |
| 142 | + var children = node.children |
| 143 | + var index = -1 |
| 144 | + var length = children.length |
| 145 | + var result = [] |
| 146 | + var subexit = context.enter('tableRow') |
| 147 | + |
| 148 | + while (++index < length) { |
| 149 | + result[index] = handleTableCell(children[index], node, context) |
| 150 | + } |
| 151 | + |
| 152 | + subexit() |
| 153 | + |
| 154 | + return result |
| 155 | + } |
| 156 | + |
| 157 | + function inlineCodeWithTable(node, parent, context) { |
| 158 | + var value = defaultInlineCode(node, parent, context) |
| 159 | + |
| 160 | + if (context.stack.indexOf('tableCell') !== -1) { |
| 161 | + value = value.replace(/\|/g, '\\$&') |
| 162 | + } |
| 163 | + |
| 164 | + return value |
| 165 | + } |
| 166 | +} |
0 commit comments