UNPKG

@atlaskit/editor-wikimarkup-transformer

Version:

Wiki markup transformer for JIRA and Confluence

56 lines 1.66 kB
import { encode } from '..'; import { unknown } from './unknown'; export var table = function table(node) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; try { var result = []; node.forEach(function (n) { result.push(tableRow(n, opts)); }); return result.join('\n'); } catch (err) { return unknown(node); } }; var tableRow = function tableRow(node) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var result = ''; var separator = '|'; node.forEach(function (n) { if (n.type.name === 'tableHeader') { separator = '||'; } else { separator = '|'; } result = "".concat(result).concat(separator).concat(tableCell(n, opts)); }); return "".concat(result).concat(separator); }; var tableCell = function tableCell(node) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, context = _ref.context; if (hasMergedCell(node)) { // This is an advanced table throw new Error('Advanced feature of table is not supported'); } var result = []; node.forEach(function (n) { result.push(encode(n, context)); }); var output = result.join('\n').trim(); // Return single whitespace if content of cell is empty // to preserve correct empty cell rendering in wiki return output === '' ? ' ' : output; }; var hasMergedCell = function hasMergedCell(node) { if (!node.attrs) { return false; } if (node.attrs.colspan && node.attrs.colspan !== 1) { return true; } if (node.attrs.rowspan && node.attrs.rowspan !== 1) { return true; } return false; };