react-markdown-table-ts 0.3.1 → 0.3.2
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 +140 -82
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +140 -82
- package/dist/index.esm.js.map +1 -1
- package/dist/src/index.d.ts +1 -15
- package/dist/src/index.js +6 -145
- package/dist/src/index.js.map +1 -1
- package/dist/src/types.d.ts +0 -6
- package/dist/src/utils.d.ts +19 -9
- package/dist/src/utils.js +36 -9
- package/dist/src/utils.js.map +1 -1
- package/dist/src/validation.d.ts +6 -0
- package/dist/src/validation.js +29 -1
- package/dist/src/validation.js.map +1 -1
- package/dist/test/utils.test.js +44 -1
- package/dist/test/utils.test.js.map +1 -1
- package/dist/test/validation.test.js +1 -2
- package/dist/test/validation.test.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -1,51 +1,5 @@
|
|
1
1
|
import require$$0, { useRef, useMemo, useEffect } from 'react';
|
2
2
|
|
3
|
-
/******************************************************************************
|
4
|
-
Copyright (c) Microsoft Corporation.
|
5
|
-
|
6
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
7
|
-
purpose with or without fee is hereby granted.
|
8
|
-
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
10
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
11
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
12
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
13
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
14
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
15
|
-
PERFORMANCE OF THIS SOFTWARE.
|
16
|
-
***************************************************************************** */
|
17
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
18
|
-
|
19
|
-
var extendStatics = function(d, b) {
|
20
|
-
extendStatics = Object.setPrototypeOf ||
|
21
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
22
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
23
|
-
return extendStatics(d, b);
|
24
|
-
};
|
25
|
-
|
26
|
-
function __extends(d, b) {
|
27
|
-
if (typeof b !== "function" && b !== null)
|
28
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
29
|
-
extendStatics(d, b);
|
30
|
-
function __() { this.constructor = d; }
|
31
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
32
|
-
}
|
33
|
-
|
34
|
-
function __spreadArray(to, from, pack) {
|
35
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
36
|
-
if (ar || !(i in from)) {
|
37
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
38
|
-
ar[i] = from[i];
|
39
|
-
}
|
40
|
-
}
|
41
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
42
|
-
}
|
43
|
-
|
44
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
45
|
-
var e = new Error(message);
|
46
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
47
|
-
};
|
48
|
-
|
49
3
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
50
4
|
|
51
5
|
function getDefaultExportFromCjs (x) {
|
@@ -4049,42 +4003,106 @@ var Prism$1 = /*@__PURE__*/getDefaultExportFromCjs(prismExports);
|
|
4049
4003
|
|
4050
4004
|
}());
|
4051
4005
|
|
4052
|
-
|
4053
|
-
|
4006
|
+
/******************************************************************************
|
4007
|
+
Copyright (c) Microsoft Corporation.
|
4008
|
+
|
4009
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
4010
|
+
purpose with or without fee is hereby granted.
|
4011
|
+
|
4012
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
4013
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
4014
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
4015
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
4016
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
4017
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
4018
|
+
PERFORMANCE OF THIS SOFTWARE.
|
4019
|
+
***************************************************************************** */
|
4020
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
4021
|
+
|
4022
|
+
var extendStatics = function(d, b) {
|
4023
|
+
extendStatics = Object.setPrototypeOf ||
|
4024
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
4025
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
4026
|
+
return extendStatics(d, b);
|
4027
|
+
};
|
4028
|
+
|
4029
|
+
function __extends(d, b) {
|
4030
|
+
if (typeof b !== "function" && b !== null)
|
4031
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
4032
|
+
extendStatics(d, b);
|
4033
|
+
function __() { this.constructor = d; }
|
4034
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
4035
|
+
}
|
4036
|
+
|
4037
|
+
function __spreadArray(to, from, pack) {
|
4038
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
4039
|
+
if (ar || !(i in from)) {
|
4040
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
4041
|
+
ar[i] = from[i];
|
4042
|
+
}
|
4043
|
+
}
|
4044
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
4045
|
+
}
|
4046
|
+
|
4047
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
4048
|
+
var e = new Error(message);
|
4049
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
4050
|
+
};
|
4051
|
+
|
4052
|
+
// src/validation.ts
|
4053
|
+
/**
|
4054
|
+
* Custom error class for handling Markdown table generation errors.
|
4055
|
+
*/
|
4054
4056
|
var MarkdownTableError = /** @class */ (function (_super) {
|
4055
4057
|
__extends(MarkdownTableError, _super);
|
4056
4058
|
function MarkdownTableError(message) {
|
4057
4059
|
var _this = _super.call(this, message) || this;
|
4058
4060
|
_this.name = 'MarkdownTableError';
|
4061
|
+
Object.setPrototypeOf(_this, MarkdownTableError.prototype);
|
4059
4062
|
return _this;
|
4060
4063
|
}
|
4061
4064
|
return MarkdownTableError;
|
4062
4065
|
}(Error));
|
4063
|
-
|
4064
|
-
|
4065
|
-
|
4066
|
+
|
4067
|
+
// src/utils.ts
|
4068
|
+
/**
|
4069
|
+
* Calculates the maximum width for each column based on the content.
|
4070
|
+
* @param tableRows - All rows (header and body) of the table.
|
4071
|
+
* @param maxColumnCount - The maximum number of columns in the table.
|
4072
|
+
* @returns An array of maximum widths for each column.
|
4073
|
+
*/
|
4074
|
+
function calculateColumnWidths(tableRows, maxColumnCount) {
|
4075
|
+
var widths = new Array(maxColumnCount).fill(3); // Minimum width of 3 for each column.
|
4076
|
+
tableRows.forEach(function (currentRow) {
|
4066
4077
|
var _a;
|
4067
4078
|
for (var i = 0; i < maxColumnCount; i++) {
|
4068
|
-
var cell = (_a =
|
4079
|
+
var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
|
4069
4080
|
widths[i] = Math.max(widths[i], cell.length);
|
4070
4081
|
}
|
4071
4082
|
});
|
4072
4083
|
return widths;
|
4073
4084
|
}
|
4074
|
-
|
4075
|
-
|
4076
|
-
|
4077
|
-
|
4085
|
+
/**
|
4086
|
+
* Formats a single row into a Markdown-formatted string.
|
4087
|
+
* @param columnCount - The number of columns in the table.
|
4088
|
+
* @param currentRow - The data of the current row.
|
4089
|
+
* @param columnAlignments - Alignment settings for each column.
|
4090
|
+
* @param columnWidths - Widths of each column.
|
4091
|
+
* @param useTabs - Flag to use tabs between columns.
|
4092
|
+
* @param canReplaceNewlines - Flag to replace newlines with <br> tags.
|
4093
|
+
* @returns The Markdown string for the row.
|
4094
|
+
*/
|
4095
|
+
function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines) {
|
4078
4096
|
var _a, _b;
|
4079
4097
|
if (useTabs === void 0) { useTabs = false; }
|
4080
|
-
if (
|
4098
|
+
if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
|
4081
4099
|
var defaultAlignment = 'left';
|
4082
4100
|
var adjustedAlignments = columnAlignments.length < columnCount
|
4083
4101
|
? __spreadArray(__spreadArray([], columnAlignments, true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : columnAlignments;
|
4084
4102
|
var markdownRow = '|';
|
4085
4103
|
for (var i = 0; i < columnCount; i++) {
|
4086
|
-
var cell = (_a =
|
4087
|
-
if (
|
4104
|
+
var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
|
4105
|
+
if (canReplaceNewlines) {
|
4088
4106
|
cell = replaceNewlinesInCell(cell);
|
4089
4107
|
}
|
4090
4108
|
var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
|
@@ -4099,11 +4117,20 @@ function formatMarkdownRow(columnCount, row, columnAlignments, columnWidths, use
|
|
4099
4117
|
markdownRow += "".concat(useTabs ? '\t' : ' ').concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : ' ', "|");
|
4100
4118
|
}
|
4101
4119
|
else {
|
4120
|
+
// Left alignment or default
|
4102
4121
|
markdownRow += "".concat(useTabs ? '\t' : ' ').concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : ' ', "|");
|
4103
4122
|
}
|
4104
4123
|
}
|
4105
4124
|
return markdownRow;
|
4106
4125
|
}
|
4126
|
+
/**
|
4127
|
+
* Generates the alignment row for the Markdown table syntax.
|
4128
|
+
* @param columnCount - The number of columns in the table.
|
4129
|
+
* @param columnAlignments - Alignment settings for each column.
|
4130
|
+
* @param columnWidths - Widths of each column.
|
4131
|
+
* @param useTabs - Flag to use tabs between columns.
|
4132
|
+
* @returns The Markdown string for the alignment row.
|
4133
|
+
*/
|
4107
4134
|
function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs) {
|
4108
4135
|
var _a;
|
4109
4136
|
if (useTabs === void 0) { useTabs = false; }
|
@@ -4133,41 +4160,72 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
|
|
4133
4160
|
}
|
4134
4161
|
return alignmentRow;
|
4135
4162
|
}
|
4136
|
-
|
4137
|
-
|
4163
|
+
/**
|
4164
|
+
* Generates a complete Markdown table string from the provided data.
|
4165
|
+
* @param inputData - The table data including headers and rows.
|
4166
|
+
* @param columnAlignments - Alignment settings for each column.
|
4167
|
+
* @param canAdjustColumnWidths - Flag to adjust column widths based on content.
|
4168
|
+
* @param useTabs - Flag to use tabs between columns.
|
4169
|
+
* @param replaceNewlines - Flag to replace newlines with <br> tags.
|
4170
|
+
* @returns The complete Markdown table string.
|
4171
|
+
*/
|
4172
|
+
function generateMarkdownTableString(inputData, columnAlignments, canAdjustColumnWidths, useTabs, replaceNewlines) {
|
4173
|
+
if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
|
4138
4174
|
if (useTabs === void 0) { useTabs = false; }
|
4139
4175
|
if (replaceNewlines === void 0) { replaceNewlines = false; }
|
4140
|
-
var headerColumnCount =
|
4141
|
-
var bodyColumnCounts =
|
4176
|
+
var headerColumnCount = inputData.inputDataHeader.length;
|
4177
|
+
var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
|
4142
4178
|
var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
|
4143
|
-
var columnWidths =
|
4144
|
-
? calculateColumnWidths(__spreadArray([
|
4179
|
+
var columnWidths = canAdjustColumnWidths
|
4180
|
+
? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
|
4145
4181
|
: undefined;
|
4146
|
-
var markdownHeaderRow = formatMarkdownRow(maxColumnCount,
|
4182
|
+
var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4147
4183
|
var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs);
|
4148
|
-
var markdownBodyRows =
|
4149
|
-
.map(function (
|
4150
|
-
return formatMarkdownRow(maxColumnCount,
|
4184
|
+
var markdownBodyRows = inputData.inputDataBody
|
4185
|
+
.map(function (currentRow) {
|
4186
|
+
return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines);
|
4151
4187
|
})
|
4152
4188
|
.join('\n');
|
4153
4189
|
return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
|
4154
4190
|
}
|
4155
|
-
|
4156
|
-
|
4157
|
-
|
4158
|
-
|
4159
|
-
|
4160
|
-
|
4191
|
+
/**
|
4192
|
+
* Replaces newline characters in a string with <br> tags.
|
4193
|
+
* @param cell - The cell content to process.
|
4194
|
+
* @returns The processed cell content with newlines replaced.
|
4195
|
+
*/
|
4196
|
+
function replaceNewlinesInCell(cell) {
|
4197
|
+
return cell.replace(/\n/g, '<br>');
|
4161
4198
|
}
|
4199
|
+
/**
|
4200
|
+
* Converts a zero-based column index to its corresponding Excel-style column name.
|
4201
|
+
* For example, 0 -> 'A', 1 -> 'B', ..., 25 -> 'Z', 26 -> 'AA', etc.
|
4202
|
+
* @param index - The zero-based column index.
|
4203
|
+
* @returns The corresponding column name.
|
4204
|
+
*/
|
4162
4205
|
function getColumnName(index) {
|
4163
|
-
var
|
4206
|
+
var columnName = '';
|
4164
4207
|
var currentIndex = index;
|
4165
4208
|
while (currentIndex >= 0) {
|
4166
|
-
|
4209
|
+
columnName = String.fromCharCode((currentIndex % 26) + 65) + columnName;
|
4167
4210
|
currentIndex = Math.floor(currentIndex / 26) - 1;
|
4168
4211
|
}
|
4169
|
-
return
|
4212
|
+
return columnName;
|
4170
4213
|
}
|
4214
|
+
/**
|
4215
|
+
* Generates an array of alphabetic headers based on the specified column count.
|
4216
|
+
* @param columnCount - The number of columns.
|
4217
|
+
* @returns An array of column header names.
|
4218
|
+
*/
|
4219
|
+
function generateAlphabetHeaders(columnCount) {
|
4220
|
+
var alphabetHeaders = [];
|
4221
|
+
for (var i = 0; i < columnCount; i++) {
|
4222
|
+
alphabetHeaders.push(getColumnName(i));
|
4223
|
+
}
|
4224
|
+
return alphabetHeaders;
|
4225
|
+
}
|
4226
|
+
|
4227
|
+
// CSS styles
|
4228
|
+
var PRISM_CSS = "\ncode[class*=language-],pre[class*=language-]{color:#000;background:0 0;text-shadow:0 1px #fff;font-family:Consolas,Monaco,\"Andale Mono\",\"Ubuntu Mono\",monospace;font-size:1em;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}code[class*=language-] ::-moz-selection,code[class*=language-]::-moz-selection,pre[class*=language-] ::-moz-selection,pre[class*=language-]::-moz-selection{text-shadow:none;background:#b3d4fc}code[class*=language-] ::selection,code[class*=language-]::selection,pre[class*=language-] ::selection,pre[class*=language-]::selection{text-shadow:none;background:#b3d4fc}@media print{code[class*=language-],pre[class*=language-]{text-shadow:none}}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#f5f2f0}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}pre[class*=language-].line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}pre[class*=language-].line-numbers>code{position:relative;white-space:inherit}.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:0;font-size:100%;left:-3.8em;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.line-numbers-rows>span{display:block;counter-increment:linenumber}.line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}\n";
|
4171
4229
|
var MarkdownTable = function (_a) {
|
4172
4230
|
var _b = _a.inputData, inputData = _b === void 0 ? null : _b, _c = _a.hasHeader, hasHeader = _c === void 0 ? true : _c, _d = _a.columnAlignments, columnAlignments = _d === void 0 ? [] : _d, _e = _a.isCompact, isCompact = _e === void 0 ? false : _e, _f = _a.hasTabs, hasTabs = _f === void 0 ? false : _f, _g = _a.canReplaceNewlines, canReplaceNewlines = _g === void 0 ? false : _g, className = _a.className, onTableCreate = _a.onTableCreate;
|
4173
4231
|
var adjustColumnWidths = !isCompact;
|
@@ -4182,12 +4240,12 @@ var MarkdownTable = function (_a) {
|
|
4182
4240
|
}
|
4183
4241
|
var tableData = hasHeader
|
4184
4242
|
? {
|
4185
|
-
|
4186
|
-
|
4243
|
+
inputDataHeader: inputData[0],
|
4244
|
+
inputDataBody: inputData.slice(1),
|
4187
4245
|
}
|
4188
4246
|
: {
|
4189
|
-
|
4190
|
-
|
4247
|
+
inputDataHeader: generateAlphabetHeaders(inputData[0].length),
|
4248
|
+
inputDataBody: inputData,
|
4191
4249
|
};
|
4192
4250
|
return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines);
|
4193
4251
|
}
|