react-markdown-table-ts 0.5.5 → 0.5.7

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/README.md CHANGED
@@ -1,63 +1,65 @@
1
- # ⚛️ react-markdown-table-ts 🛡️
2
-
3
- [![NPM Version](https://img.shields.io/npm/v/react-markdown-table-ts.svg)](https://www.npmjs.com/package/react-markdown-table-ts)
4
- ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=flat-square&logo=typescript&logoColor=white)
5
- [![codecov](https://codecov.io/gh/keithwalsh/react-markdown-table-ts/branch/main/graph/badge.svg)](https://codecov.io/gh/keithwalsh/react-markdown-table-ts)
6
-
7
- A React component that converts structured data into Markdown table syntax and displays it within a `<pre>` tag.
8
-
9
- ## Features
10
-
11
- - **Type Safety:** Built with TypeScript to provide strong type guarantees.
12
- - **Easy Integration:** Simple API for converting data arrays into Markdown table strings.
13
- - **Customizable Alignments:** Specify column alignments (left, center, right, or none) with ease.
14
- - **Compact Mode:** Option to generate compact tables with minimal padding.
15
- - **Tab-Separated Columns:** Option to add tabs between columns.
16
- - **Newline Handling: Option** to replace newlines in cells with HTML line breaks.
17
- - **Raw Markdown Access:** Retrieve the generated Markdown string for further processing or usage.
18
- - **Header Options:** Choose whether to include a header row or use default alphabetical headers.
19
- - **Flexible Styling:** Apply custom CSS classes for styling the rendered Markdown.
20
-
21
- ## 📦 Installation
22
-
23
- Install the package via npm:
24
-
25
- ```
26
-
27
- npm install react-markdown-table-ts
28
-
29
- ```
30
-
31
- ## 🔧 API
32
-
33
- ### MarkdownTable Props
34
-
35
- | Prop | Type | Default | Description |
36
- | :------------------: | :-------------------------------------------: | :---------: | :-----------------------------------------: |
37
- | `data` | `string[][]` | `null` | The table data as a 2D array of strings |
38
- | `columnAlignments` | `('left' \| 'center' \| 'right' \| 'none')[]` | `[]` | Alignment for each column |
39
- | `isCompact` | `boolean` | `false` | Use minimal column widths |
40
- | `className` | `string` | `undefined` | CSS class for the `<pre>` tag |
41
- | `hasTabs` | `boolean` | `false` | Add tabs between table columns |
42
- | `canReplaceNewlines` | `boolean` | `false` | Replace newlines in cells with `<br>` tags |
43
- | `onTableCreate` | `(markdownString: string) => void` | `undefined` | Callback to receive the Markdown string |
44
- | `hasHeader` | `boolean` | `true` | Whether the first row of `data` is a header |
45
-
46
- ## 🚀 Usage
47
-
48
- ```jsx
49
- import React from 'react';
50
- import {MarkdownTable} from 'markdown-table-component';
51
-
52
- const App = () => {
53
- const data = [
54
- ['Header 1', 'Header 2', 'Header 3'],
55
- ['Row 1, Col 1', 'Row 1, Col 2', 'Row 1, Col 3'],
56
- ['Row 2, Col 1', 'Row 2, Col 2', 'Row 2, Col 3'],
57
- ];
58
-
59
- return <MarkdownTable data={data} />;
60
- };
61
-
62
- export default App;
63
- ```
1
+ # ⚛️ react-markdown-table-ts 🛡️
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/react-markdown-table-ts.svg)](https://www.npmjs.com/package/react-markdown-table-ts)
4
+ ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=flat-square&logo=typescript&logoColor=white)
5
+ [![codecov](https://codecov.io/gh/keithwalsh/react-markdown-table-ts/branch/main/graph/badge.svg)](https://codecov.io/gh/keithwalsh/react-markdown-table-ts)
6
+ [![Package Quality](https://packagequality.com/shield/react-markdown-table-ts.svg)](https://packagequality.com/#?package=react-markdown-table-ts)
7
+ [![Code Climate](https://codeclimate.com/github/keithwalsh/react-markdown-table-ts/badges/gpa.svg)](https://codeclimate.com/github/keithwalsh/react-markdown-table-ts)
8
+
9
+ A React component that converts structured data into Markdown table syntax and displays it within a `<pre>` tag.
10
+
11
+ ## Features
12
+
13
+ - **Type Safety:** Built with TypeScript to provide strong type guarantees.
14
+ - **Easy Integration:** Simple API for converting data arrays into Markdown table strings.
15
+ - **Customizable Alignments:** Specify column alignments (left, center, right, or none) with ease.
16
+ - **Compact Mode:** Option to generate compact tables with minimal padding.
17
+ - **Tab-Separated Columns:** Option to add tabs between columns.
18
+ - **Newline Handling: Option** to replace newlines in cells with HTML line breaks.
19
+ - **Raw Markdown Access:** Retrieve the generated Markdown string for further processing or usage.
20
+ - **Header Options:** Choose whether to include a header row or use default alphabetical headers.
21
+ - **Flexible Styling:** Apply custom CSS classes for styling the rendered Markdown.
22
+
23
+ ## 📦 Installation
24
+
25
+ Install the package via npm:
26
+
27
+ ```
28
+
29
+ npm install react-markdown-table-ts
30
+
31
+ ```
32
+
33
+ ## 🔧 API
34
+
35
+ ### MarkdownTable Props
36
+
37
+ | Prop | Type | Default | Description |
38
+ | :------------------: | :-------------------------------------------: | :---------: | :-----------------------------------------: |
39
+ | `data` | `string[][]` | `null` | The table data as a 2D array of strings |
40
+ | `columnAlignments` | `('left' \| 'center' \| 'right' \| 'none')[]` | `[]` | Alignment for each column |
41
+ | `isCompact` | `boolean` | `false` | Use minimal column widths |
42
+ | `className` | `string` | `undefined` | CSS class for the `<pre>` tag |
43
+ | `hasTabs` | `boolean` | `false` | Add tabs between table columns |
44
+ | `canReplaceNewlines` | `boolean` | `false` | Replace newlines in cells with `<br>` tags |
45
+ | `onTableCreate` | `(markdownString: string) => void` | `undefined` | Callback to receive the Markdown string |
46
+ | `hasHeader` | `boolean` | `true` | Whether the first row of `data` is a header |
47
+
48
+ ## 🚀 Usage
49
+
50
+ ```jsx
51
+ import React from 'react';
52
+ import {MarkdownTable} from 'markdown-table-component';
53
+
54
+ const App = () => {
55
+ const data = [
56
+ ['Header 1', 'Header 2', 'Header 3'],
57
+ ['Row 1, Col 1', 'Row 1, Col 2', 'Row 1, Col 3'],
58
+ ['Row 2, Col 1', 'Row 2, Col 2', 'Row 2, Col 3'],
59
+ ];
60
+
61
+ return <MarkdownTable data={data} />;
62
+ };
63
+
64
+ export default App;
65
+ ```
package/dist/index.cjs.js CHANGED
@@ -4095,6 +4095,17 @@ var MarkdownTableError = /** @class */ (function (_super) {
4095
4095
  }(Error));
4096
4096
 
4097
4097
  // src/utils.ts
4098
+ /**
4099
+ * Adjusts column alignments array to match the required column count
4100
+ * @param columnAlignments - Original alignment settings
4101
+ * @param columnCount - Required number of columns
4102
+ * @returns Adjusted array of column alignments
4103
+ */
4104
+ function getAdjustedAlignments(columnAlignments, columnCount) {
4105
+ var defaultAlignment = 'left';
4106
+ return columnAlignments.length < columnCount
4107
+ ? __spreadArray(__spreadArray([], Array.from(columnAlignments), true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : Array.from(columnAlignments);
4108
+ }
4098
4109
  /**
4099
4110
  * Calculates the maximum width for each column based on the content.
4100
4111
  * @param tableRows - All rows (header and body) of the table.
@@ -4128,16 +4139,14 @@ function formatMarkdownRow(columnCount, currentRow, columnAlignments, columnWidt
4128
4139
  if (useTabs === void 0) { useTabs = false; }
4129
4140
  if (canReplaceNewlines === void 0) { canReplaceNewlines = false; }
4130
4141
  if (hasPadding === void 0) { hasPadding = true; }
4131
- var defaultAlignment = 'left';
4132
- var adjustedAlignments = columnAlignments.length < columnCount
4133
- ? __spreadArray(__spreadArray([], columnAlignments, true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : columnAlignments;
4142
+ var adjustedAlignments = getAdjustedAlignments(columnAlignments, columnCount);
4134
4143
  var markdownRow = '|';
4135
4144
  for (var i = 0; i < columnCount; i++) {
4136
4145
  var cell = (_a = currentRow[i]) !== null && _a !== void 0 ? _a : '';
4137
4146
  if (canReplaceNewlines) {
4138
4147
  cell = replaceNewlinesInCell(cell);
4139
4148
  }
4140
- var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : defaultAlignment;
4149
+ var alignment = (_b = adjustedAlignments[i]) !== null && _b !== void 0 ? _b : 'left';
4141
4150
  var targetWidth = columnWidths ? columnWidths[i] : cell.length;
4142
4151
  var padding = hasPadding ? ' ' : '';
4143
4152
  if (alignment === 'right') {
@@ -4169,12 +4178,10 @@ function formatAlignmentRow(columnCount, columnAlignments, columnWidths, useTabs
4169
4178
  var _a;
4170
4179
  if (useTabs === void 0) { useTabs = false; }
4171
4180
  if (hasPadding === void 0) { hasPadding = true; }
4172
- var defaultAlignment = 'left';
4173
- var adjustedAlignments = columnAlignments.length < columnCount
4174
- ? __spreadArray(__spreadArray([], columnAlignments, true), Array(columnCount - columnAlignments.length).fill(defaultAlignment), true) : columnAlignments;
4181
+ var adjustedAlignments = getAdjustedAlignments(columnAlignments, columnCount);
4175
4182
  var alignmentRow = '|';
4176
4183
  for (var i = 0; i < columnCount; i++) {
4177
- var alignment = (_a = adjustedAlignments[i]) !== null && _a !== void 0 ? _a : defaultAlignment;
4184
+ var alignment = (_a = adjustedAlignments[i]) !== null && _a !== void 0 ? _a : 'left';
4178
4185
  var targetWidth = columnWidths ? columnWidths[i] : 3;
4179
4186
  var alignIndicator = '';
4180
4187
  var padding = hasPadding ? ' ' : '';
@@ -18566,10 +18573,10 @@ var Tooltip$1 = Tooltip;
18566
18573
  var LIGHT_THEME_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: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";
18567
18574
  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";
18568
18575
  var MarkdownTable = function (_a) {
18569
- 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;
18576
+ 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;
18570
18577
  var adjustColumnWidths = !isCompact;
18571
18578
  var preElementRef = React.useRef(null);
18572
- var _k = React.useState(false), isCopied = _k[0], setIsCopied = _k[1];
18579
+ var _l = React.useState(false), isCopied = _l[0], setIsCopied = _l[1];
18573
18580
  var markdownTableSyntax = React.useMemo(function () {
18574
18581
  if (inputData === null) {
18575
18582
  return 'Error: No data provided for the table.';
@@ -18578,24 +18585,13 @@ var MarkdownTable = function (_a) {
18578
18585
  if (!Array.isArray(inputData) || inputData.length === 0) {
18579
18586
  throw new MarkdownTableError("The 'data' prop must be a non-empty two-dimensional array.");
18580
18587
  }
18581
- var tableData = hasHeader
18582
- ? {
18583
- inputDataHeader: inputData[0],
18584
- inputDataBody: inputData.slice(1),
18585
- }
18586
- : {
18587
- inputDataHeader: generateAlphabetHeaders(inputData[0].length),
18588
- inputDataBody: inputData,
18589
- };
18590
- return generateMarkdownTableString(tableData, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines, hasPadding);
18588
+ var _a = hasHeader
18589
+ ? { inputDataHeader: inputData[0], inputDataBody: inputData.slice(1) }
18590
+ : { inputDataHeader: generateAlphabetHeaders(inputData[0].length), inputDataBody: inputData }, inputDataHeader = _a.inputDataHeader, inputDataBody = _a.inputDataBody;
18591
+ return generateMarkdownTableString({ inputDataHeader: inputDataHeader, inputDataBody: inputDataBody }, columnAlignments, adjustColumnWidths, hasTabs, canReplaceNewlines, hasPadding);
18591
18592
  }
18592
18593
  catch (error) {
18593
- if (error instanceof MarkdownTableError) {
18594
- return "Error: ".concat(error.message);
18595
- }
18596
- else {
18597
- throw error;
18598
- }
18594
+ return error instanceof MarkdownTableError ? "Error: ".concat(error.message) : (function () { throw error; })();
18599
18595
  }
18600
18596
  }, [
18601
18597
  inputData,
@@ -18628,12 +18624,12 @@ var MarkdownTable = function (_a) {
18628
18624
  return (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx("style", { children: theme === 'light' ? LIGHT_THEME_CSS : DARK_THEME_CSS }), jsxRuntimeExports.jsxs("div", { style: {
18629
18625
  position: 'relative',
18630
18626
  isolation: 'isolate'
18631
- }, children: [jsxRuntimeExports.jsx(Tooltip$1, { title: isCopied ? 'Copied!' : 'Copy markdown table syntax', placement: "left-end", arrow: true, children: jsxRuntimeExports.jsx(IconButton$1, { onClick: handleCopy, sx: {
18627
+ }, 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: {
18632
18628
  position: 'absolute',
18633
18629
  top: '12px',
18634
18630
  right: '8px',
18635
18631
  zIndex: 1
18636
- }, "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 }) })] })] }));
18632
+ }, "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 }) })] })] }));
18637
18633
  };
18638
18634
 
18639
18635
  exports.MarkdownTable = MarkdownTable;