react-markdown-table-ts 0.3.6 → 0.3.8

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,26 @@ 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
+ cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
4125
+ markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
4112
4126
  }
4113
4127
  else if (alignment === 'center') {
4114
4128
  var totalPadding = targetWidth - cell.length;
4115
4129
  var paddingLeft = Math.floor(totalPadding / 2);
4116
4130
  var paddingRight = totalPadding - paddingLeft;
4117
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : ' ', "|");
4131
+ markdownRow += "".concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : '', "|");
4118
4132
  }
4119
4133
  else {
4120
4134
  // Left alignment or default
4121
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4135
+ markdownRow += "".concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : '', "|");
4122
4136
  }
4123
4137
  }
4124
4138
  return markdownRow;
@@ -4156,7 +4170,7 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
4156
4170
  alignIndicator = "".concat('-'.repeat(targetWidth));
4157
4171
  break;
4158
4172
  }
4159
- alignmentRow += "".concat(useTabs ? '\t' : ' ').concat(alignIndicator).concat(useTabs ? '\t' : ' ', "|");
4173
+ alignmentRow += "".concat(alignIndicator).concat(useTabs ? '\t' : '', "|");
4160
4174
  }
4161
4175
  return alignmentRow;
4162
4176
  }
@@ -4173,29 +4187,21 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4173
4187
  if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
4174
4188
  if (useTabs === void 0) { useTabs = false; }
4175
4189
  if (replaceNewlines === void 0) { replaceNewlines = false; }
4176
- var headerColumnCount = inputData.inputDataHeader.length;
4177
- var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
4190
+ var headerColumnCount = inputData.inputTableHeader.length;
4191
+ var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4178
4192
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4179
4193
  var columnWidths = canAdjustColumnWidths
4180
- ? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
4194
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4181
4195
  : undefined;
4182
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4196
+ var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4183
4197
  var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4184
- var markdownBodyRows = inputData.inputDataBody
4198
+ var markdownBodyRows = inputData.inputTableBody
4185
4199
  .map(function (currentRow) {
4186
4200
  return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4187
4201
  })
4188
4202
  .join('\n');
4189
4203
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
4190
4204
  }
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
4205
  /**
4200
4206
  * Converts a zero-based column index to its corresponding Excel-style column name.
4201
4207
  * For example, 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z', 26 -> 'AA', etc.
@@ -4232,7 +4238,7 @@ var MarkdownTable = function (_a) {
4232
4238
  var preElementRef = useRef(null);
4233
4239
  var markdownTableSyntax = useMemo(function () {
4234
4240
  if (inputData === null) {
4235
- return 'Error: No data provided for the table.';
4241
+ return '| | | | | |\n|---|---|---|---|---|\n| | | | | |\n| | | | | |\n| | | | | |';
4236
4242
  }
4237
4243
  try {
4238
4244
  if (!Array.isArray(inputData) || inputData.length === 0) {
@@ -4240,12 +4246,12 @@ var MarkdownTable = function (_a) {
4240
4246
  }
4241
4247
  var tableData = hasHeader
4242
4248
  ? {
4243
- inputDataHeader: inputData[0],
4244
- inputDataBody: inputData.slice(1),
4249
+ inputTableHeader: inputData[0],
4250
+ inputTableBody: inputData.slice(1),
4245
4251
  }
4246
4252
  : {
4247
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
4248
- inputDataBody: inputData,
4253
+ inputTableHeader: generateAlphabetHeaders(inputData[0].length),
4254
+ inputTableBody: inputData,
4249
4255
  };
4250
4256
  return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines);
4251
4257
  }