react-markdown-table-ts 0.3.8 → 0.3.10

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
@@ -4074,13 +4074,14 @@ var MarkdownTableError = /** @class */ (function (_super) {
4074
4074
 
4075
4075
  // src/utils.ts
4076
4076
  /**
4077
- * Calculates the maximum width for each column based on the content.
4077
+ * Calculates the maximum width for each column based on the content and newline replacement rules.
4078
4078
  * @param inputDataRows - All rows (header and body) of the input table.
4079
4079
  * @param maxColumnCount - The maximum number of columns in the input table.
4080
4080
  * @param minWidth - The minimum width for each column. Defaults to 3.
4081
+ * @param canReplaceNewlines - Whether to replace newlines with '<br>' (affects width) or a space.
4081
4082
  * @returns An array of maximum widths for each column.
4082
4083
  */
4083
- function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4084
+ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth, canReplaceNewlines) {
4084
4085
  if (minWidth === void 0) { minWidth = 3; }
4085
4086
  // Initialize column widths with the minimum width.
4086
4087
  var columnWidths = Array.from({ length: maxColumnCount }, function () { return minWidth; });
@@ -4092,6 +4093,10 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4092
4093
  // Retrieve the cell value; default to an empty string if undefined or null.
4093
4094
  var cellValue = currentRow[columnIndex];
4094
4095
  var cellString = cellValue !== null && cellValue !== undefined ? String(cellValue) : '';
4096
+ // Replace newlines with the appropriate character based on canReplaceNewlines.
4097
+ cellString = canReplaceNewlines
4098
+ ? cellString.replace(/\n/g, '<br>')
4099
+ : cellString.replace(/\n/g, ' ');
4095
4100
  // Update the column width if the current cell's length is greater.
4096
4101
  if (cellString.length > columnWidths[columnIndex]) {
4097
4102
  columnWidths[columnIndex] = cellString.length;
@@ -4110,8 +4115,8 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4110
4115
  * @param canReplaceNewlines - Flag to replace newlines with <br> tags.
4111
4116
  * @returns The Markdown string for the row.
4112
4117
  */
4113
- function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
4114
- var _a, _b;
4118
+ function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
4119
+ var _a, _b, _c;
4115
4120
  if (useTabs === void 0) { useTabs = false; }
4116
4121
  if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
4117
4122
  var defaultAlignment = 'left';
@@ -4120,16 +4125,11 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4120
4125
  var markdownRow = '|';
4121
4126
  for (var i = 0; i < columnCount; i++) {
4122
4127
  var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4123
- if (canReplaceNewlines) {
4124
- cell = cell.replace(/\n/g, '<br>');
4125
- }
4126
- else {
4127
- cell = cell.replace(/\n/g, ' ');
4128
- }
4129
4128
  var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
4130
- var targetWidth = columnWidths ? columnWidths[i] : cell.length;
4129
+ cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
4130
+ // Safe access to columnWidths with fallback
4131
+ var targetWidth = (_c = columnWidths === null || columnWidths === void 0 ? void 0 : columnWidths[i]) !== null && _c !== void 0 ? _c : cell.length;
4131
4132
  if (alignment === 'right') {
4132
- cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
4133
4133
  markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
4134
4134
  }
4135
4135
  else if (alignment === 'center') {
@@ -4153,7 +4153,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4153
4153
  * @param useTabs - Flag to use tabs between columns.
4154
4154
  * @returns The Markdown string for the alignment row.
4155
4155
  */
4156
- function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4156
+ function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4157
4157
  var _a;
4158
4158
  if (useTabs === void 0) { useTabs = false; }
4159
4159
  var defaultAlignment = 'left';
@@ -4199,13 +4199,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4199
4199
  var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4200
4200
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4201
4201
  var columnWidths = canAdjustColumnWidths
4202
- ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4202
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount, 3, // minWidth (you can change this value as needed)
4203
+ true // canReplaceNewlines (set this to true or false based on your logic)
4204
+ )
4203
4205
  : undefined;
4204
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4205
- var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4206
+ var markdownHeaderRow = formatMarkdownTableBody(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4207
+ var markdownAlignmentRow = formatMarkdownTableAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4206
4208
  var markdownBodyRows = inputData.inputTableBody
4207
4209
  .map(function (currentRow) {
4208
- return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4210
+ return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4209
4211
  })
4210
4212
  .join('\n');
4211
4213
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();