react-markdown-table-ts 0.5.8 → 0.5.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.esm.js CHANGED
@@ -4077,7 +4077,7 @@ var MarkdownTableError = /** @class */ (function (_super) {
4077
4077
  function getAdjustedAlignments(columnAlignments, columnCount) {
4078
4078
  var defaultAlignment = 'left';
4079
4079
  return columnAlignments.length < columnCount
4080
- ? __spreadArray(__spreadArray([], columnAlignments, true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : columnAlignments.slice(0, columnCount);
4080
+ ? __spreadArray(__spreadArray([], Array.from(columnAlignments), true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : Array.from(columnAlignments);
4081
4081
  }
4082
4082
  /**
4083
4083
  * Calculates the maximum width for each column based on the content.
@@ -4096,104 +4096,104 @@ function calculateColumnWidths(tableRows, maxColumnCount) {
4096
4096
  });
4097
4097
  return widths;
4098
4098
  }
4099
- /**
4100
- * Formats a single row into a Markdown-formatted string.
4101
- * @param params - Object containing formatting options and row data.
4102
- * @returns The Markdown string for the row.
4103
- */
4104
- function formatMarkdownRow(_a) {
4105
- var _b, _c;
4106
- var columnCount = _a.columnCount, currentRow = _a.currentRow, columnAlignments = _a.columnAlignments, columnWidths = _a.columnWidths, _d = _a.useTabs, useTabs = _d === void 0 ? false : _d, _e = _a.replaceNewlines, replaceNewlines = _e === void 0 ? false : _e, _f = _a.hasPadding, hasPadding = _f === void 0 ? true : _f;
4099
+ function formatCell(cell, alignment, targetWidth, useTabs, padding) {
4100
+ switch (alignment) {
4101
+ case 'right':
4102
+ return "".concat(useTabs ? '\t' : padding).concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : padding);
4103
+ case 'center':
4104
+ return formatCenterAlignedCell(cell, targetWidth, useTabs, padding);
4105
+ default:
4106
+ return "".concat(useTabs ? '\t' : padding).concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : padding);
4107
+ }
4108
+ }
4109
+ function formatCenterAlignedCell(cell, targetWidth, useTabs, padding) {
4110
+ var totalPadding = targetWidth - cell.length;
4111
+ var paddingLeft = Math.floor(totalPadding / 2);
4112
+ var paddingRight = totalPadding - paddingLeft;
4113
+ return "".concat(useTabs ? '\t' : padding).concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : padding);
4114
+ }
4115
+ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidths, useTabs, canReplaceNewlines, hasPadding) {
4116
+ if (useTabs === void 0) { useTabs = false; }
4117
+ if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
4118
+ if (hasPadding === void 0) { hasPadding = true; }
4107
4119
  var adjustedAlignments = getAdjustedAlignments(columnAlignments, columnCount);
4108
- var markdownRow = '|';
4109
- for (var i = 0; i < columnCount; i++) {
4110
- var cell = (_b = currentRow[i]) !== null && _b !== void 0 ? _b : '';
4111
- if (replaceNewlines) {
4120
+ var padding = hasPadding ? ' ' : '';
4121
+ var formattedCells = Array.from({ length: columnCount }, function (_, i) {
4122
+ var _a, _b;
4123
+ var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4124
+ if (canReplaceNewlines) {
4112
4125
  cell = replaceNewlinesInCell(cell);
4113
4126
  }
4114
- var alignment = (_c = adjustedAlignments[i]) !== null && _c !== void 0 ? _c : 'left';
4127
+ var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : 'left';
4115
4128
  var targetWidth = columnWidths ? columnWidths[i] : cell.length;
4116
- var padding = hasPadding ? ' ' : '';
4117
- if (alignment === 'right') {
4118
- markdownRow += "".concat(useTabs ? '\t' : padding).concat(cell.padStart(targetWidth)).concat(useTabs ? '\t' : padding, "|");
4119
- }
4120
- else if (alignment === 'center') {
4121
- var totalPadding = targetWidth - cell.length;
4122
- var paddingLeft = Math.floor(totalPadding / 2);
4123
- var paddingRight = totalPadding - paddingLeft;
4124
- markdownRow += "".concat(useTabs ? '\t' : padding).concat(' '.repeat(paddingLeft)).concat(cell).concat(' '.repeat(paddingRight)).concat(useTabs ? '\t' : padding, "|");
4125
- }
4126
- else {
4127
- // Left alignment or default
4128
- markdownRow += "".concat(useTabs ? '\t' : padding).concat(cell.padEnd(targetWidth)).concat(useTabs ? '\t' : padding, "|");
4129
- }
4130
- }
4131
- return markdownRow;
4132
- }
4133
- function getAlignmentIndicator(_a) {
4134
- var _b;
4135
- var alignment = _a.alignment, targetWidth = _a.targetWidth;
4136
- var indicators = {
4137
- left: ":".concat('-'.repeat(targetWidth - 1)),
4138
- center: ":".concat('-'.repeat(targetWidth - 2), ":"),
4139
- right: "".concat('-'.repeat(targetWidth - 1), ":"),
4140
- none: "".concat('-'.repeat(targetWidth)),
4141
- };
4142
- return (_b = indicators[alignment]) !== null && _b !== void 0 ? _b : indicators.none;
4129
+ return formatCell(cell, alignment, targetWidth, useTabs, padding);
4130
+ });
4131
+ return "|".concat(formattedCells.join('|'), "|");
4143
4132
  }
4144
- function formatAlignmentRow(_a) {
4145
- var columnCount = _a.columnCount, columnAlignments = _a.columnAlignments, columnWidths = _a.columnWidths, _b = _a.useTabs, useTabs = _b === void 0 ? false : _b, _c = _a.hasPadding, hasPadding = _c === void 0 ? true : _c;
4133
+ /**
4134
+ * Generates the alignment row for the Markdown table syntax.
4135
+ * @param columnCount - The number of columns in the table.
4136
+ * @param columnAlignments - Alignment settings for each column.
4137
+ * @param columnWidths - Widths of each column.
4138
+ * @param useTabs - Flag to use tabs between columns.
4139
+ * @param hasPadding - Flag to add padding spaces around cell content.
4140
+ * @returns The Markdown string for the alignment row.
4141
+ */
4142
+ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs, hasPadding) {
4143
+ var _a;
4144
+ if (useTabs === void 0) { useTabs = false; }
4145
+ if (hasPadding === void 0) { hasPadding = true; }
4146
4146
  var adjustedAlignments = getAdjustedAlignments(columnAlignments, columnCount);
4147
- var padding = hasPadding ? ' ' : '';
4148
- var alignmentCells = Array.from({ length: columnCount }, function (_, i) {
4149
- var _a, _b;
4147
+ var alignmentRow = '|';
4148
+ for (var i = 0; i < columnCount; i++) {
4150
4149
  var alignment = (_a = adjustedAlignments[i]) !== null && _a !== void 0 ? _a : 'left';
4151
- var targetWidth = (_b = columnWidths === null || columnWidths === void 0 ? void 0 : columnWidths[i]) !== null && _b !== void 0 ? _b : 3;
4152
- var alignIndicator = getAlignmentIndicator({ alignment: alignment, targetWidth: targetWidth });
4153
- return "".concat(useTabs ? '\t' : padding).concat(alignIndicator).concat(useTabs ? '\t' : padding);
4154
- });
4155
- return "|".concat(alignmentCells.join('|'), "|");
4150
+ var targetWidth = columnWidths ? columnWidths[i] : 3;
4151
+ var alignIndicator = '';
4152
+ var padding = hasPadding ? ' ' : '';
4153
+ switch (alignment) {
4154
+ case 'left':
4155
+ alignIndicator = ":".concat('-'.repeat(targetWidth - 1));
4156
+ break;
4157
+ case 'center':
4158
+ alignIndicator = ":".concat('-'.repeat(targetWidth - 2), ":");
4159
+ break;
4160
+ case 'right':
4161
+ alignIndicator = "".concat('-'.repeat(targetWidth - 1), ":");
4162
+ break;
4163
+ default:
4164
+ alignIndicator = "".concat('-'.repeat(targetWidth));
4165
+ break;
4166
+ }
4167
+ alignmentRow += "".concat(useTabs ? '\t' : padding).concat(alignIndicator).concat(useTabs ? '\t' : padding, "|");
4168
+ }
4169
+ return alignmentRow;
4156
4170
  }
4157
4171
  /**
4158
4172
  * Generates a complete Markdown table string from the provided data.
4159
- * @param params - Object containing table data and formatting options.
4173
+ * @param inputData - The table data including headers and rows.
4174
+ * @param columnAlignments - Alignment settings for each column.
4175
+ * @param canAdjustColumnWidths - Flag to adjust column widths based on content.
4176
+ * @param useTabs - Flag to use tabs between columns.
4177
+ * @param replaceNewlines - Flag to replace newlines with <br> tags.
4178
+ * @param hasPadding - Flag to add padding spaces around cell content.
4160
4179
  * @returns The complete Markdown table string.
4161
4180
  */
4162
- function generateMarkdownTableString(_a) {
4163
- var inputData = _a.inputData, columnAlignments = _a.columnAlignments, _b = _a.canAdjustColumnWidths, canAdjustColumnWidths = _b === void 0 ? true : _b, _c = _a.useTabs, useTabs = _c === void 0 ? false : _c, _d = _a.replaceNewlines, replaceNewlines = _d === void 0 ? false : _d, _e = _a.hasPadding, hasPadding = _e === void 0 ? true : _e;
4181
+ function generateMarkdownTableString(inputData, columnAlignments, canAdjustColumnWidths, useTabs, replaceNewlines, hasPadding) {
4182
+ if (canAdjustColumnWidths === void 0) { canAdjustColumnWidths = true; }
4183
+ if (useTabs === void 0) { useTabs = false; }
4184
+ if (replaceNewlines === void 0) { replaceNewlines = false; }
4185
+ if (hasPadding === void 0) { hasPadding = true; }
4164
4186
  var headerColumnCount = inputData.inputDataHeader.length;
4165
4187
  var bodyColumnCounts = inputData.inputDataBody.map(function (currentRow) { return currentRow.length; });
4166
4188
  var maxColumnCount = Math.max.apply(Math, __spreadArray([headerColumnCount], bodyColumnCounts, false));
4167
4189
  var columnWidths = canAdjustColumnWidths
4168
4190
  ? calculateColumnWidths(__spreadArray([inputData.inputDataHeader], inputData.inputDataBody, true), maxColumnCount)
4169
4191
  : undefined;
4170
- var markdownHeaderRow = formatMarkdownRow({
4171
- columnCount: maxColumnCount,
4172
- currentRow: inputData.inputDataHeader,
4173
- columnAlignments: columnAlignments,
4174
- columnWidths: columnWidths,
4175
- useTabs: useTabs,
4176
- replaceNewlines: replaceNewlines,
4177
- hasPadding: hasPadding,
4178
- });
4179
- var markdownAlignmentRow = formatAlignmentRow({
4180
- columnCount: maxColumnCount,
4181
- columnAlignments: columnAlignments,
4182
- columnWidths: columnWidths,
4183
- useTabs: useTabs,
4184
- hasPadding: hasPadding,
4185
- });
4192
+ var markdownHeaderRow = formatMarkdownRow(maxColumnCount, inputData.inputDataHeader, columnAlignments, columnWidths, useTabs, replaceNewlines, hasPadding);
4193
+ var markdownAlignmentRow = formatAlignmentRow(maxColumnCount, columnAlignments, columnWidths, useTabs, hasPadding);
4186
4194
  var markdownBodyRows = inputData.inputDataBody
4187
4195
  .map(function (currentRow) {
4188
- return formatMarkdownRow({
4189
- columnCount: maxColumnCount,
4190
- currentRow: currentRow,
4191
- columnAlignments: columnAlignments,
4192
- columnWidths: columnWidths,
4193
- useTabs: useTabs,
4194
- replaceNewlines: replaceNewlines,
4195
- hasPadding: hasPadding,
4196
- });
4196
+ return formatMarkdownRow(maxColumnCount, currentRow, columnAlignments, columnWidths, useTabs, replaceNewlines, hasPadding);
4197
4197
  })
4198
4198
  .join('\n');
4199
4199
  return "".concat(markdownHeaderRow, "\n").concat(markdownAlignmentRow, "\n").concat(markdownBodyRows).trimEnd();
@@ -18539,40 +18539,48 @@ var LIGHT_THEME_CSS = "\ncode[class*=language-],pre[class*=language-]{color:#000
18539
18539
  var DARK_THEME_CSS = "\ncode[class*=language-],pre[class*=language-]{color:#f8f8f2;background:0 0;text-shadow:0 1px rgba(0,0,0,.3);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}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto;border-radius:.3em}:not(pre)>code[class*=language-],pre[class*=language-]{background:#282a36}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}pre[class*=language-].line-numbers{position:relative;padding-left:2.4em;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:.5em;text-align:right}\n";
18540
18540
  var MarkdownTable = function (_a) {
18541
18541
  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.hasPadding, hasPadding = _g === void 0 ? true : _g, _h = _a.canReplaceNewlines, canReplaceNewlines = _h === void 0 ? false : _h, className = _a.className, onTableCreate = _a.onTableCreate, _j = _a.theme, theme = _j === void 0 ? 'light' : _j, preStyle = _a.preStyle, _k = _a.showCopyButton, showCopyButton = _k === void 0 ? false : _k;
18542
+ var adjustColumnWidths = !isCompact;
18542
18543
  var preElementRef = useRef(null);
18543
18544
  var _l = useState(false), isCopied = _l[0], setIsCopied = _l[1];
18544
- var adjustColumnWidths = !isCompact;
18545
- var convertedInputData = useMemo(function () {
18546
- if (!inputData)
18547
- return null;
18548
- var header = inputData[0], body = inputData.slice(1);
18549
- return { inputDataHeader: header, inputDataBody: body };
18550
- }, [inputData]);
18551
18545
  var markdownTableSyntax = useMemo(function () {
18552
- return generateMarkdownTableSyntax({
18553
- inputData: convertedInputData,
18554
- hasHeader: hasHeader,
18555
- columnAlignments: columnAlignments,
18556
- adjustColumnWidths: adjustColumnWidths,
18557
- hasTabs: hasTabs,
18558
- hasPadding: hasPadding,
18559
- canReplaceNewlines: canReplaceNewlines,
18560
- });
18546
+ if (inputData === null) {
18547
+ return 'Error: No data provided for the table.';
18548
+ }
18549
+ try {
18550
+ if (!Array.isArray(inputData) || inputData.length === 0) {
18551
+ throw new MarkdownTableError("The 'data' prop must be a non-empty two-dimensional array.");
18552
+ }
18553
+ var _a = hasHeader
18554
+ ? { inputDataHeader: inputData[0], inputDataBody: inputData.slice(1) }
18555
+ : { inputDataHeader: generateAlphabetHeaders(inputData[0].length), inputDataBody: inputData }, inputDataHeader = _a.inputDataHeader, inputDataBody = _a.inputDataBody;
18556
+ return generateMarkdownTableString({ inputDataHeader: inputDataHeader, inputDataBody: inputDataBody }, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines, hasPadding);
18557
+ }
18558
+ catch (error) {
18559
+ return error instanceof MarkdownTableError ? "Error: ".concat(error.message) : (function () { throw error; })();
18560
+ }
18561
18561
  }, [
18562
- convertedInputData,
18562
+ inputData,
18563
18563
  hasHeader,
18564
18564
  columnAlignments,
18565
- adjustColumnWidths,
18565
+ isCompact,
18566
18566
  hasTabs,
18567
- hasPadding,
18568
18567
  canReplaceNewlines,
18568
+ hasPadding,
18569
18569
  ]);
18570
18570
  useEffect(function () {
18571
18571
  if (onTableCreate) {
18572
18572
  onTableCreate(markdownTableSyntax);
18573
18573
  }
18574
18574
  }, [markdownTableSyntax, onTableCreate]);
18575
- usePrismHighlight(markdownTableSyntax, preElementRef);
18575
+ useEffect(function () {
18576
+ var _a;
18577
+ var codeElement = (_a = preElementRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('code');
18578
+ if (codeElement && markdownTableSyntax) {
18579
+ requestAnimationFrame(function () {
18580
+ Prism$1.highlightElement(codeElement);
18581
+ });
18582
+ }
18583
+ }, [markdownTableSyntax]);
18576
18584
  var handleCopy = function () {
18577
18585
  navigator.clipboard.writeText(markdownTableSyntax);
18578
18586
  setIsCopied(true);
@@ -18580,51 +18588,14 @@ var MarkdownTable = function (_a) {
18580
18588
  };
18581
18589
  return (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx("style", { children: theme === 'light' ? LIGHT_THEME_CSS : DARK_THEME_CSS }), jsxRuntimeExports.jsxs("div", { style: {
18582
18590
  position: 'relative',
18583
- isolation: 'isolate',
18591
+ isolation: 'isolate'
18584
18592
  }, children: [showCopyButton && (jsxRuntimeExports.jsx(Tooltip$1, { title: isCopied ? 'Copied!' : 'Copy markdown table syntax', placement: "left-end", arrow: true, children: jsxRuntimeExports.jsx(IconButton$1, { onClick: handleCopy, sx: {
18585
18593
  position: 'absolute',
18586
18594
  top: '12px',
18587
18595
  right: '8px',
18588
- zIndex: 1,
18596
+ zIndex: 1
18589
18597
  }, "aria-label": "Copy to clipboard", size: "small", children: jsxRuntimeExports.jsx(ContentCopyIcon, { fontSize: "small" }) }) })), jsxRuntimeExports.jsx("pre", { ref: preElementRef, className: "".concat(className, " language-markdown line-numbers ").concat(theme === 'dark' ? 'dark-theme' : ''), style: preStyle, children: jsxRuntimeExports.jsx("code", { className: "language-markdown", role: "code", children: markdownTableSyntax }) })] })] }));
18590
18598
  };
18591
- var generateMarkdownTableSyntax = function (_a) {
18592
- var inputData = _a.inputData, hasHeader = _a.hasHeader, columnAlignments = _a.columnAlignments, adjustColumnWidths = _a.adjustColumnWidths, hasTabs = _a.hasTabs, hasPadding = _a.hasPadding, canReplaceNewlines = _a.canReplaceNewlines;
18593
- if (inputData === null) {
18594
- return 'Error: No data provided for the table.';
18595
- }
18596
- if (!Array.isArray(inputData) || inputData.length === 0) {
18597
- throw new MarkdownTableError("The 'inputData' prop must be a non-empty two-dimensional array.");
18598
- }
18599
- var _b = hasHeader
18600
- ? {
18601
- inputDataHeader: inputData[0],
18602
- inputDataBody: inputData.slice(1),
18603
- }
18604
- : {
18605
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
18606
- inputDataBody: inputData,
18607
- }, inputDataHeader = _b.inputDataHeader, inputDataBody = _b.inputDataBody;
18608
- return generateMarkdownTableString({
18609
- inputData: { inputDataHeader: inputDataHeader, inputDataBody: inputDataBody },
18610
- columnAlignments: columnAlignments,
18611
- canAdjustColumnWidths: adjustColumnWidths,
18612
- useTabs: hasTabs,
18613
- replaceNewlines: canReplaceNewlines,
18614
- hasPadding: hasPadding,
18615
- });
18616
- };
18617
- var usePrismHighlight = function (markdownTableSyntax, preElementRef) {
18618
- useEffect(function () {
18619
- var _a;
18620
- var codeElement = (_a = preElementRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('code');
18621
- if (codeElement) {
18622
- requestAnimationFrame(function () {
18623
- Prism$1.highlightElement(codeElement);
18624
- });
18625
- }
18626
- }, [markdownTableSyntax]);
18627
- };
18628
18599
 
18629
18600
  export { MarkdownTable };
18630
18601
  //# sourceMappingURL=index.esm.js.map