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 +30 -11
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +30 -11
- package/dist/index.esm.js.map +1 -1
- package/dist/utils.d.ts +5 -4
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -4066,13 +4066,14 @@ var MarkdownTableError = /** @class */ (function (_super) {
|
|
4066
4066
|
|
4067
4067
|
// src/utils.ts
|
4068
4068
|
/**
|
4069
|
-
* Calculates the maximum width for each column based on the content.
|
4069
|
+
* Calculates the maximum width for each column based on the content and newline replacement rules.
|
4070
4070
|
* @param inputDataRows - All rows (header and body) of the input table.
|
4071
4071
|
* @param maxColumnCount - The maximum number of columns in the input table.
|
4072
4072
|
* @param minWidth - The minimum width for each column. Defaults to 3.
|
4073
|
+
* @param canReplaceNewlines - Whether to replace newlines with '<br>' (affects width) or a space.
|
4073
4074
|
* @returns An array of maximum widths for each column.
|
4074
4075
|
*/
|
4075
|
-
function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
|
4076
|
+
function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth, canReplaceNewlines) {
|
4076
4077
|
if (minWidth === void 0) { minWidth = 3; }
|
4077
4078
|
// Initialize column widths with the minimum width.
|
4078
4079
|
var columnWidths = Array.from({ length: maxColumnCount }, function () { return minWidth; });
|
@@ -4084,9 +4085,25 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
|
|
4084
4085
|
// Retrieve the cell value; default to an empty string if undefined or null.
|
4085
4086
|
var cellValue = currentRow[columnIndex];
|
4086
4087
|
var cellString = cellValue !== null && cellValue !== undefined ? String(cellValue) : '';
|
4087
|
-
//
|
4088
|
-
if (
|
4089
|
-
|
4088
|
+
// Replace newlines with the appropriate character based on canReplaceNewlines.
|
4089
|
+
if (canReplaceNewlines) {
|
4090
|
+
// If replacing newlines with <br>, account for its extra length (4 characters)
|
4091
|
+
var newlineCount = (cellString.match(/\n/g) || []).length;
|
4092
|
+
cellString = cellString.replace(/\n/g, '<br>');
|
4093
|
+
var additionalLength = newlineCount * ('<br>'.length - 1); // Add extra 3 characters for each newline
|
4094
|
+
var adjustedLength = cellString.length + additionalLength;
|
4095
|
+
// Update column width considering the extra length due to <br>
|
4096
|
+
if (adjustedLength > columnWidths[columnIndex]) {
|
4097
|
+
columnWidths[columnIndex] = adjustedLength;
|
4098
|
+
}
|
4099
|
+
}
|
4100
|
+
else {
|
4101
|
+
// Replace newlines with a space if not using <br>
|
4102
|
+
cellString = cellString.replace(/\n/g, ' ');
|
4103
|
+
// Update the column width if the current cell's length is greater.
|
4104
|
+
if (cellString.length > columnWidths[columnIndex]) {
|
4105
|
+
columnWidths[columnIndex] = cellString.length;
|
4106
|
+
}
|
4090
4107
|
}
|
4091
4108
|
}
|
4092
4109
|
}
|
@@ -4102,7 +4119,7 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
|
|
4102
4119
|
* @param canReplaceNewlines - Flag to replace newlines with <br> tags.
|
4103
4120
|
* @returns The Markdown string for the row.
|
4104
4121
|
*/
|
4105
|
-
function
|
4122
|
+
function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
|
4106
4123
|
var _a, _b, _c;
|
4107
4124
|
if (useTabs === void 0) { useTabs = false; }
|
4108
4125
|
if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
|
@@ -4140,7 +4157,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
|
|
4140
4157
|
* @param useTabs - Flag to use tabs between columns.
|
4141
4158
|
* @returns The Markdown string for the alignment row.
|
4142
4159
|
*/
|
4143
|
-
function
|
4160
|
+
function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
|
4144
4161
|
var _a;
|
4145
4162
|
if (useTabs === void 0) { useTabs = false; }
|
4146
4163
|
var defaultAlignment = 'left';
|
@@ -4186,13 +4203,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
|
|
4186
4203
|
var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
|
4187
4204
|
var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
|
4188
4205
|
var columnWidths = canAdjustColumnWidths
|
4189
|
-
? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
|
4206
|
+
? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount, 3, // minWidth (you can change this value as needed)
|
4207
|
+
true // canReplaceNewlines (set this to true or false based on your logic)
|
4208
|
+
)
|
4190
4209
|
: undefined;
|
4191
|
-
var markdownHeaderRow =
|
4192
|
-
var markdownAlignmentRow =
|
4210
|
+
var markdownHeaderRow = formatMarkdownTableBody(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4211
|
+
var markdownAlignmentRow = formatMarkdownTableAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
|
4193
4212
|
var markdownBodyRows = inputData.inputTableBody
|
4194
4213
|
.map(function (currentRow) {
|
4195
|
-
return
|
4214
|
+
return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4196
4215
|
})
|
4197
4216
|
.join('\n');
|
4198
4217
|
return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
|