react-markdown-table-ts 0.3.9 → 0.3.11

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,9 +4093,25 @@ 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) : '';
4095
- // Update the column width if the current cell's length is greater.
4096
- if (cellString.length > columnWidths[columnIndex]) {
4097
- columnWidths[columnIndex] = cellString.length;
4096
+ // Replace newlines with the appropriate character based on canReplaceNewlines.
4097
+ if (canReplaceNewlines) {
4098
+ // If replacing newlines with <br>, account for its extra length (4 characters)
4099
+ var newlineCount = (cellString.match(/\n/g) || []).length;
4100
+ cellString = cellString.replace(/\n/g, '<br>');
4101
+ var additionalLength = newlineCount * ('<br>'.length - 1); // Add extra 3 characters for each newline
4102
+ var adjustedLength = cellString.length + additionalLength;
4103
+ // Update column width considering the extra length due to <br>
4104
+ if (adjustedLength > columnWidths[columnIndex]) {
4105
+ columnWidths[columnIndex] = adjustedLength;
4106
+ }
4107
+ }
4108
+ else {
4109
+ // Replace newlines with a space if not using <br>
4110
+ cellString = cellString.replace(/\n/g, ' ');
4111
+ // Update the column width if the current cell's length is greater.
4112
+ if (cellString.length > columnWidths[columnIndex]) {
4113
+ columnWidths[columnIndex] = cellString.length;
4114
+ }
4098
4115
  }
4099
4116
  }
4100
4117
  }
@@ -4110,7 +4127,7 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
4110
4127
  * @param canReplaceNewlines - Flag to replace newlines with <br> tags.
4111
4128
  * @returns The Markdown string for the row.
4112
4129
  */
4113
- function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
4130
+ function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
4114
4131
  var _a, _b, _c;
4115
4132
  if (useTabs === void 0) { useTabs = false; }
4116
4133
  if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
@@ -4148,7 +4165,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4148
4165
  * @param useTabs - Flag to use tabs between columns.
4149
4166
  * @returns The Markdown string for the alignment row.
4150
4167
  */
4151
- function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4168
+ function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4152
4169
  var _a;
4153
4170
  if (useTabs === void 0) { useTabs = false; }
4154
4171
  var defaultAlignment = 'left';
@@ -4194,13 +4211,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4194
4211
  var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4195
4212
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4196
4213
  var columnWidths = canAdjustColumnWidths
4197
- ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
4214
+ ? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount, 3, // minWidth (you can change this value as needed)
4215
+ true // canReplaceNewlines (set this to true or false based on your logic)
4216
+ )
4198
4217
  : undefined;
4199
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4200
- var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4218
+ var markdownHeaderRow = formatMarkdownTableBody(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4219
+ var markdownAlignmentRow = formatMarkdownTableAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
4201
4220
  var markdownBodyRows = inputData.inputTableBody
4202
4221
  .map(function (currentRow) {
4203
- return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4222
+ return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4204
4223
  })
4205
4224
  .join('\n');
4206
4225
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();