react-markdown-table-ts 0.3.6 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -4075,20 +4075,30 @@ var MarkdownTableError = /** @class */ (function (_super) {
4075
4075
  // src/utils.ts
4076
4076
  /**
4077
4077
  * Calculates the maximum width for each column based on the content.
4078
- * @param tableRows - All rows (header and body) of the table.
4079
- * @param maxColumnCount - The maximum number of columns in the table.
4078
+ * @param inputDataRows - All rows (header and body) of the input table.
4079
+ * @param maxColumnCount - The maximum number of columns in the input table.
4080
+ * @param minWidth - The minimum width for each column. Defaults to 3.
4080
4081
  * @returns An array of maximum widths for each column.
4081
4082
  */
4082
- function calculateColumnWidths(tableRows, maxColumnCount) {
4083
- var widths = new Array(maxColumnCount).fill(3); // Minimum width of 3 for each column.
4084
- tableRows.forEach(function (currentRow) {
4085
- var _a;
4086
- for (var i = 0; i < maxColumnCount; i++) {
4087
- var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4088
- widths[i] = Math.max(widths[i], cell.length);
4083
+ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4084
+ if (minWidth === void 0) { minWidth = 3; }
4085
+ // Initialize column widths with the minimum width.
4086
+ var columnWidths = Array.from({ length: maxColumnCount }, function () { return minWidth; });
4087
+ // Iterate over each row in the input data.
4088
+ for (var _i = 0, inputDataRows_1 = inputDataRows; _i < inputDataRows_1.length; _i++) {
4089
+ var currentRow = inputDataRows_1[_i];
4090
+ // Iterate over each column index up to maxColumnCount.
4091
+ for (var columnIndex = 0; columnIndex < maxColumnCount; columnIndex++) {
4092
+ // Retrieve the cell value; default to an empty string if undefined or null.
4093
+ var cellValue = currentRow[columnIndex];
4094
+ var cellString = cellValue !== null && cellValue !== undefined ? String(cellValue) : '';
4095
+ // Update the column width if the current cell's length is greater.
4096
+ if (cellString.length > columnWidths[columnIndex]) {
4097
+ columnWidths[columnIndex] = cellString.length;
4098
+ }
4089
4099
  }
4090
- });
4091
- return widths;
4100
+ }
4101
+ return columnWidths;
4092
4102
  }
4093
4103
  /**
4094
4104
  * Formats a single row into a Markdown-formatted string.
@@ -4111,22 +4121,25 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4111
4121
  for (var i = 0; i < columnCount; i++) {
4112
4122
  var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4113
4123
  if (canReplaceNewlines) {
4114
- cell = replaceNewlinesInCell(cell);
4124
+ cell = cell.replace(/\n/g, '<br>');
4125
+ }
4126
+ else {
4127
+ cell = cell.replace(/\n/g, ' ');
4115
4128
  }
4116
4129
  var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
4117
4130
  var targetWidth = columnWidths ? columnWidths[i] : cell.length;
4118
4131
  if (alignment === 'right') {
4119
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4132
+ markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
4120
4133
  }
4121
4134
  else if (alignment === 'center') {
4122
4135
  var totalPadding = targetWidth - cell.length;
4123
4136
  var paddingLeft = Math.floor(totalPadding / 2);
4124
4137
  var paddingRight = totalPadding - paddingLeft;
4125
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : ' ', "|");
4138
+ markdownRow += "".concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : '', "|");
4126
4139
  }
4127
4140
  else {
4128
4141
  // Left alignment or default
4129
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4142
+ markdownRow += "".concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : '', "|");
4130
4143
  }
4131
4144
  }
4132
4145
  return markdownRow;
@@ -4164,7 +4177,7 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
4164
4177
  alignIndicator = "".concat('-'.repeat(targetWidth));
4165
4178
  break;
4166
4179
  }
4167
- alignmentRow += "".concat(useTabs ? '\t' : ' ').concat(alignIndicator).concat(useTabs ? '\t' : ' ', "|");
4180
+ alignmentRow += "".concat(alignIndicator).concat(useTabs ? '\t' : '', "|");
4168
4181
  }
4169
4182
  return alignmentRow;
4170
4183
  }
@@ -4181,29 +4194,21 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4181
4194
  if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
4182
4195
  if (useTabs === void 0) { useTabs = false; }
4183
4196
  if (replaceNewlines === void 0) { replaceNewlines = false; }
4184
- var headerColumnCount = inputData.inputDataHeader.length;
4185
- var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
4197
+ var headerColumnCount = inputData.inputTableHeader.length;
4198
+ var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4186
4199
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4187
4200
  var columnWidths = canAdjustColumnWidths
4188
- ? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
4201
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4189
4202
  : undefined;
4190
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4203
+ var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4191
4204
  var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4192
- var markdownBodyRows = inputData.inputDataBody
4205
+ var markdownBodyRows = inputData.inputTableBody
4193
4206
  .map(function (currentRow) {
4194
4207
  return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4195
4208
  })
4196
4209
  .join('\n');
4197
4210
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
4198
4211
  }
4199
- /**
4200
- * Replaces newline characters in a string with <br> tags.
4201
- * @param cell - The cell content to process.
4202
- * @returns The processed cell content with newlines replaced.
4203
- */
4204
- function replaceNewlinesInCell(cell) {
4205
- return cell.replace(/\n/g, '<br>');
4206
- }
4207
4212
  /**
4208
4213
  * Converts a zero-based column index to its corresponding Excel-style column name.
4209
4214
  * For example, 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z', 26 -> 'AA', etc.
@@ -4240,7 +4245,7 @@ var MarkdownTable = function (_a) {
4240
4245
  var preElementRef = require$$0.useRef(null);
4241
4246
  var markdownTableSyntax = require$$0.useMemo(function () {
4242
4247
  if (inputData === null) {
4243
- return 'Error: No data provided for the table.';
4248
+ return '| | | | | |\n|---|---|---|---|---|\n| | | | | |\n| | | | | |\n| | | | | |';
4244
4249
  }
4245
4250
  try {
4246
4251
  if (!Array.isArray(inputData) || inputData.length === 0) {
@@ -4248,12 +4253,12 @@ var MarkdownTable = function (_a) {
4248
4253
  }
4249
4254
  var tableData = hasHeader
4250
4255
  ? {
4251
- inputDataHeader: inputData[0],
4252
- inputDataBody: inputData.slice(1),
4256
+ inputTableHeader: inputData[0],
4257
+ inputTableBody: inputData.slice(1),
4253
4258
  }
4254
4259
  : {
4255
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
4256
- inputDataBody: inputData,
4260
+ inputTableHeader: generateAlphabetHeaders(inputData[0].length),
4261
+ inputTableBody: inputData,
4257
4262
  };
4258
4263
  return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines);
4259
4264
  }