@atlaskit/editor-wikimarkup-transformer
Version:
Wiki markup transformer for JIRA and Confluence
62 lines (61 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.table = void 0;
var _ = require("..");
var _unknown = require("./unknown");
var table = exports.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 (0, _unknown.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((0, _.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;
};