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.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,26 @@ 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
+ cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
4133
+ markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
4120
4134
  }
4121
4135
  else if (alignment === 'center') {
4122
4136
  var totalPadding = targetWidth - cell.length;
4123
4137
  var paddingLeft = Math.floor(totalPadding / 2);
4124
4138
  var paddingRight = totalPadding - paddingLeft;
4125
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : ' ', "|");
4139
+ markdownRow += "".concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : '', "|");
4126
4140
  }
4127
4141
  else {
4128
4142
  // Left alignment or default
4129
- markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
4143
+ markdownRow += "".concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : '', "|");
4130
4144
  }
4131
4145
  }
4132
4146
  return markdownRow;
@@ -4164,7 +4178,7 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
4164
4178
  alignIndicator = "".concat('-'.repeat(targetWidth));
4165
4179
  break;
4166
4180
  }
4167
- alignmentRow += "".concat(useTabs ? '\t' : ' ').concat(alignIndicator).concat(useTabs ? '\t' : ' ', "|");
4181
+ alignmentRow += "".concat(alignIndicator).concat(useTabs ? '\t' : '', "|");
4168
4182
  }
4169
4183
  return alignmentRow;
4170
4184
  }
@@ -4181,29 +4195,21 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4181
4195
  if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
4182
4196
  if (useTabs === void 0) { useTabs = false; }
4183
4197
  if (replaceNewlines === void 0) { replaceNewlines = false; }
4184
- var headerColumnCount = inputData.inputDataHeader.length;
4185
- var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
4198
+ var headerColumnCount = inputData.inputTableHeader.length;
4199
+ var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4186
4200
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4187
4201
  var columnWidths = canAdjustColumnWidths
4188
- ? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
4202
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4189
4203
  : undefined;
4190
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4204
+ var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4191
4205
  var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4192
- var markdownBodyRows = inputData.inputDataBody
4206
+ var markdownBodyRows = inputData.inputTableBody
4193
4207
  .map(function (currentRow) {
4194
4208
  return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4195
4209
  })
4196
4210
  .join('\n');
4197
4211
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
4198
4212
  }
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
4213
  /**
4208
4214
  * Converts a zero-based column index to its corresponding Excel-style column name.
4209
4215
  * For example, 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z', 26 -> 'AA', etc.
@@ -4240,7 +4246,7 @@ var MarkdownTable = function (_a) {
4240
4246
  var preElementRef = require$$0.useRef(null);
4241
4247
  var markdownTableSyntax = require$$0.useMemo(function () {
4242
4248
  if (inputData === null) {
4243
- return 'Error: No data provided for the table.';
4249
+ return '| | | | | |\n|---|---|---|---|---|\n| | | | | |\n| | | | | |\n| | | | | |';
4244
4250
  }
4245
4251
  try {
4246
4252
  if (!Array.isArray(inputData) || inputData.length === 0) {
@@ -4248,12 +4254,12 @@ var MarkdownTable = function (_a) {
4248
4254
  }
4249
4255
  var tableData = hasHeader
4250
4256
  ? {
4251
- inputDataHeader: inputData[0],
4252
- inputDataBody: inputData.slice(1),
4257
+ inputTableHeader: inputData[0],
4258
+ inputTableBody: inputData.slice(1),
4253
4259
  }
4254
4260
  : {
4255
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
4256
- inputDataBody: inputData,
4261
+ inputTableHeader: generateAlphabetHeaders(inputData[0].length),
4262
+ inputTableBody: inputData,
4257
4263
  };
4258
4264
  return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines);
4259
4265
  }