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.esm.js CHANGED
@@ -4067,20 +4067,30 @@ var MarkdownTableError = /** @class */ (function (_super) {
4067
4067
  // src/utils.ts
4068
4068
  /**
4069
4069
  * Calculates the maximum width for each column based on the content.
4070
- * @param tableRows - All rows (header and body) of the table.
4071
- * @param maxColumnCount - The maximum number of columns in the table.
4070
+ * @param inputDataRows - All rows (header and body) of the input table.
4071
+ * @param maxColumnCount - The maximum number of columns in the input table.
4072
+ * @param minWidth - The minimum width for each column. Defaults to 3.
4072
4073
  * @returns An array of maximum widths for each column.
4073
4074
  */
4074
- function calculateColumnWidths(tableRows, maxColumnCount) {
4075
- var widths = new Array(maxColumnCount).fill(3); // Minimum width of 3 for each column.
4076
- tableRows.forEach(function (currentRow) {
4077
- var _a;
4078
- for (var i = 0; i < maxColumnCount; i++) {
4079
- var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4080
- widths[i] = Math.max(widths[i], cell.length);
4075
+ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4076
+ if (minWidth === void 0) { minWidth = 3; }
4077
+ // Initialize column widths with the minimum width.
4078
+ var columnWidths = Array.from({ length: maxColumnCount }, function () { return minWidth; });
4079
+ // Iterate over each row in the input data.
4080
+ for (var _i = 0, inputDataRows_1 = inputDataRows; _i < inputDataRows_1.length; _i++) {
4081
+ var currentRow = inputDataRows_1[_i];
4082
+ // Iterate over each column index up to maxColumnCount.
4083
+ for (var columnIndex = 0; columnIndex < maxColumnCount; columnIndex++) {
4084
+ // Retrieve the cell value; default to an empty string if undefined or null.
4085
+ var cellValue = currentRow[columnIndex];
4086
+ var cellString = cellValue !== null && cellValue !== undefined ? String(cellValue) : '';
4087
+ // Update the column width if the current cell's length is greater.
4088
+ if (cellString.length > columnWidths[columnIndex]) {
4089
+ columnWidths[columnIndex] = cellString.length;
4090
+ }
4081
4091
  }
4082
- });
4083
- return widths;
4092
+ }
4093
+ return columnWidths;
4084
4094
  }
4085
4095
  /**
4086
4096
  * Formats a single row into a Markdown-formatted string.
@@ -4103,22 +4113,25 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4103
4113
  for (var i = 0; i < columnCount; i++) {
4104
4114
  var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4105
4115
  if (canReplaceNewlines) {
4106
- cell = replaceNewlinesInCell(cell);
4116
+ cell = cell.replace(/\n/g, '<br>');
4117
+ }
4118
+ else {
4119
+ cell = cell.replace(/\n/g, ' ');
4107
4120
  }
4108
4121
  var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
4109
4122
  var targetWidth = columnWidths ? columnWidths[i] : cell.length;
4110
4123
  if (alignment === 'right') {
4111
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4124
+ markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
4112
4125
  }
4113
4126
  else if (alignment === 'center') {
4114
4127
  var totalPadding = targetWidth - cell.length;
4115
4128
  var paddingLeft = Math.floor(totalPadding / 2);
4116
4129
  var paddingRight = totalPadding - paddingLeft;
4117
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : ' ', "|");
4130
+ markdownRow += "".concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : '', "|");
4118
4131
  }
4119
4132
  else {
4120
4133
  // Left alignment or default
4121
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4134
+ markdownRow += "".concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : '', "|");
4122
4135
  }
4123
4136
  }
4124
4137
  return markdownRow;
@@ -4156,7 +4169,7 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
4156
4169
  alignIndicator = "".concat('-'.repeat(targetWidth));
4157
4170
  break;
4158
4171
  }
4159
- alignmentRow += "".concat(useTabs ? '\t' : ' ').concat(alignIndicator).concat(useTabs ? '\t' : ' ', "|");
4172
+ alignmentRow += "".concat(alignIndicator).concat(useTabs ? '\t' : '', "|");
4160
4173
  }
4161
4174
  return alignmentRow;
4162
4175
  }
@@ -4173,29 +4186,21 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4173
4186
  if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
4174
4187
  if (useTabs === void 0) { useTabs = false; }
4175
4188
  if (replaceNewlines === void 0) { replaceNewlines = false; }
4176
- var headerColumnCount = inputData.inputDataHeader.length;
4177
- var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
4189
+ var headerColumnCount = inputData.inputTableHeader.length;
4190
+ var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4178
4191
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4179
4192
  var columnWidths = canAdjustColumnWidths
4180
- ? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
4193
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4181
4194
  : undefined;
4182
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4195
+ var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4183
4196
  var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4184
- var markdownBodyRows = inputData.inputDataBody
4197
+ var markdownBodyRows = inputData.inputTableBody
4185
4198
  .map(function (currentRow) {
4186
4199
  return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4187
4200
  })
4188
4201
  .join('\n');
4189
4202
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
4190
4203
  }
4191
- /**
4192
- * Replaces newline characters in a string with <br> tags.
4193
- * @param cell - The cell content to process.
4194
- * @returns The processed cell content with newlines replaced.
4195
- */
4196
- function replaceNewlinesInCell(cell) {
4197
- return cell.replace(/\n/g, '<br>');
4198
- }
4199
4204
  /**
4200
4205
  * Converts a zero-based column index to its corresponding Excel-style column name.
4201
4206
  * For example, 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z', 26 -> 'AA', etc.
@@ -4232,7 +4237,7 @@ var MarkdownTable = function (_a) {
4232
4237
  var preElementRef = useRef(null);
4233
4238
  var markdownTableSyntax = useMemo(function () {
4234
4239
  if (inputData === null) {
4235
- return 'Error: No data provided for the table.';
4240
+ return '| | | | | |\n|---|---|---|---|---|\n| | | | | |\n| | | | | |\n| | | | | |';
4236
4241
  }
4237
4242
  try {
4238
4243
  if (!Array.isArray(inputData) || inputData.length === 0) {
@@ -4240,12 +4245,12 @@ var MarkdownTable = function (_a) {
4240
4245
  }
4241
4246
  var tableData = hasHeader
4242
4247
  ? {
4243
- inputDataHeader: inputData[0],
4244
- inputDataBody: inputData.slice(1),
4248
+ inputTableHeader: inputData[0],
4249
+ inputTableBody: inputData.slice(1),
4245
4250
  }
4246
4251
  : {
4247
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
4248
- inputDataBody: inputData,
4252
+ inputTableHeader: generateAlphabetHeaders(inputData[0].length),
4253
+ inputTableBody: inputData,
4249
4254
  };
4250
4255
  return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines);
4251
4256
  }