UNPKG

@atlaskit/editor-wikimarkup-transformer

Version:

Wiki markup transformer for JIRA and Confluence

54 lines 1.32 kB
import { encode } from '..'; import { unknown } from './unknown'; export const table = (node, opts = {}) => { try { const result = []; node.forEach(n => { result.push(tableRow(n, opts)); }); return result.join('\n'); } catch (err) { return unknown(node); } }; const tableRow = (node, opts = {}) => { let result = ''; let separator = '|'; node.forEach(n => { if (n.type.name === 'tableHeader') { separator = '||'; } else { separator = '|'; } result = `${result}${separator}${tableCell(n, opts)}`; }); return `${result}${separator}`; }; const tableCell = (node, { context } = {}) => { if (hasMergedCell(node)) { // This is an advanced table throw new Error('Advanced feature of table is not supported'); } const result = []; node.forEach(n => { result.push(encode(n, context)); }); const 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; }; const 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; };