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 +19 -17
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +19 -17
- 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,6 +4085,10 @@ 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) : '';
|
4088
|
+
// Replace newlines with the appropriate character based on canReplaceNewlines.
|
4089
|
+
cellString = canReplaceNewlines
|
4090
|
+
? cellString.replace(/\n/g, '<br>')
|
4091
|
+
: cellString.replace(/\n/g, ' ');
|
4087
4092
|
// Update the column width if the current cell's length is greater.
|
4088
4093
|
if (cellString.length > columnWidths[columnIndex]) {
|
4089
4094
|
columnWidths[columnIndex] = cellString.length;
|
@@ -4102,8 +4107,8 @@ function calculateColumnWidths(inputDataRows, maxColumnCount, minWidth) {
|
|
4102
4107
|
* @param canReplaceNewlines - Flag to replace newlines with <br> tags.
|
4103
4108
|
* @returns The Markdown string for the row.
|
4104
4109
|
*/
|
4105
|
-
function
|
4106
|
-
var _a, _b;
|
4110
|
+
function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
|
4111
|
+
var _a, _b, _c;
|
4107
4112
|
if (useTabs === void 0) { useTabs = false; }
|
4108
4113
|
if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
|
4109
4114
|
var defaultAlignment = 'left';
|
@@ -4112,16 +4117,11 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
|
|
4112
4117
|
var markdownRow = '|';
|
4113
4118
|
for (var i = 0; i < columnCount; i++) {
|
4114
4119
|
var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
|
4115
|
-
if (canReplaceNewlines) {
|
4116
|
-
cell = cell.replace(/\n/g, '<br>');
|
4117
|
-
}
|
4118
|
-
else {
|
4119
|
-
cell = cell.replace(/\n/g, ' ');
|
4120
|
-
}
|
4121
4120
|
var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
|
4122
|
-
|
4121
|
+
cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
|
4122
|
+
// Safe access to columnWidths with fallback
|
4123
|
+
var targetWidth = (_c = columnWidths === null || columnWidths === void 0 ? void 0 : columnWidths[i]) !== null && _c !== void 0 ? _c : cell.length;
|
4123
4124
|
if (alignment === 'right') {
|
4124
|
-
cell = "".concat(canReplaceNewlines ? cell.replace(/\n/g, '<br>') : cell.replace(/\n/g, ' '));
|
4125
4125
|
markdownRow += "".concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : '', "|");
|
4126
4126
|
}
|
4127
4127
|
else if (alignment === 'center') {
|
@@ -4145,7 +4145,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
|
|
4145
4145
|
* @param useTabs - Flag to use tabs between columns.
|
4146
4146
|
* @returns The Markdown string for the alignment row.
|
4147
4147
|
*/
|
4148
|
-
function
|
4148
|
+
function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
|
4149
4149
|
var _a;
|
4150
4150
|
if (useTabs === void 0) { useTabs = false; }
|
4151
4151
|
var defaultAlignment = 'left';
|
@@ -4191,13 +4191,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
|
|
4191
4191
|
var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
|
4192
4192
|
var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
|
4193
4193
|
var columnWidths = canAdjustColumnWidths
|
4194
|
-
? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount)
|
4194
|
+
? calculateColumnWidths(__spreadArray([inputData.inputTableHeader], inputData.inputTableBody, true), maxColumnCount, 3, // minWidth (you can change this value as needed)
|
4195
|
+
true // canReplaceNewlines (set this to true or false based on your logic)
|
4196
|
+
)
|
4195
4197
|
: undefined;
|
4196
|
-
var markdownHeaderRow =
|
4197
|
-
var markdownAlignmentRow =
|
4198
|
+
var markdownHeaderRow = formatMarkdownTableBody(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4199
|
+
var markdownAlignmentRow = formatMarkdownTableAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
|
4198
4200
|
var markdownBodyRows = inputData.inputTableBody
|
4199
4201
|
.map(function (currentRow) {
|
4200
|
-
return
|
4202
|
+
return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4201
4203
|
})
|
4202
4204
|
.join('\n');
|
4203
4205
|
return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
|