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 +15 -8
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +15 -8
- 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,7 +4107,7 @@ 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
|
4110
|
+
function formatMarkdownTableBody(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
|
4106
4111
|
var _a, _b, _c;
|
4107
4112
|
if (useTabs === void 0) { useTabs = false; }
|
4108
4113
|
if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
|
@@ -4140,7 +4145,7 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
|
|
4140
4145
|
* @param useTabs - Flag to use tabs between columns.
|
4141
4146
|
* @returns The Markdown string for the alignment row.
|
4142
4147
|
*/
|
4143
|
-
function
|
4148
|
+
function formatMarkdownTableAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
|
4144
4149
|
var _a;
|
4145
4150
|
if (useTabs === void 0) { useTabs = false; }
|
4146
4151
|
var defaultAlignment = 'left';
|
@@ -4186,13 +4191,15 @@ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColum
|
|
4186
4191
|
var bodyColumnCounts = inputData.inputTableBody.map(function (currentRow) { return currentRow.length; });
|
4187
4192
|
var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
|
4188
4193
|
var columnWidths = canAdjustColumnWidths
|
4189
|
-
? 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
|
+
)
|
4190
4197
|
: undefined;
|
4191
|
-
var markdownHeaderRow =
|
4192
|
-
var markdownAlignmentRow =
|
4198
|
+
var markdownHeaderRow = formatMarkdownTableBody(maxColumnCount, inputData.inputTableHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4199
|
+
var markdownAlignmentRow = formatMarkdownTableAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
|
4193
4200
|
var markdownBodyRows = inputData.inputTableBody
|
4194
4201
|
.map(function (currentRow) {
|
4195
|
-
return
|
4202
|
+
return formatMarkdownTableBody(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4196
4203
|
})
|
4197
4204
|
.join('\n');
|
4198
4205
|
return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
|