react-markdown-table-ts 0.3.9 → 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,7 +4115,7 @@ 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) {
4118
+ function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
4114
4119
  var _a, _b, _c;
4115
4120
  if (useTabs === void 0) { useTabs = false; }
4116
4121
  if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
@@ -4148,7 +4153,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4148
4153
  * @param useTabs - Flag to use tabs between columns.
4149
4154
  * @returns The Markdown string for the alignment row.
4150
4155
  */
4151
- function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4156
+ function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
4152
4157
  var _a;
4153
4158
  if (useTabs === void 0) { useTabs = false; }
4154
4159
  var defaultAlignment = 'left';
@@ -4194,13 +4199,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
4194
4199
  var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
4195
4200
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4196
4201
  var columnWidths = canAdjustColumnWidths
4197
- ? 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
+ )
4198
4205
  : undefined;
4199
- var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
4200
- 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);
4201
4208
  var markdownBodyRows = inputData.inputTableBody
4202
4209
  .map(function (currentRow) {
4203
- return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4210
+ return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
4204
4211
  })
4205
4212
  .join('\n');
4206
4213
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();