react-markdown-table-ts 0.6.4 → 1.1.1

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
@@ -2,63 +2,102 @@
2
2
 
3
3
  [![NPM Version](https://img.shields.io/npm/v/react-markdown-table-ts.svg)](https://www.npmjs.com/package/react-markdown-table-ts)
4
4
  [![codecov](https://codecov.io/gh/keithwalsh/react-markdown-table-ts/branch/main/graph/badge.svg)](https://codecov.io/gh/keithwalsh/react-markdown-table-ts)
5
- ![Build](https://github.com/keithwalsh/react-markdown-table-ts/actions/workflows/release.yaml/badge.svg)
5
+ ![Build](https://github.com/keithwalsh/react-markdown-table-ts/actions/workflows/build.yml/badge.svg)
6
6
  [![Code Climate](https://codeclimate.com/github/keithwalsh/react-markdown-table-ts/badges/gpa.svg)](https://codeclimate.com/github/keithwalsh/react-markdown-table-ts)
7
-
8
- A React component that converts structured data into Markdown table syntax and displays it within a `<pre>` tag.
9
-
10
- ## ✨ Features
11
-
12
- - **Type Safety:** Built with TypeScript to provide strong type guarantees.
13
- - **Easy Integration:** Simple API for converting data arrays into Markdown table strings.
14
- - **Customizable Alignments:** Specify column alignments (left, center, right, or none) with ease.
15
- - **Compact Mode:** Option to generate compact tables with minimal padding.
16
- - **Tab-Separated Columns:** Option to add tabs between columns.
17
- - **Newline Handling: Option** to replace newlines in cells with HTML line breaks.
18
- - **Raw Markdown Access:** Retrieve the generated Markdown string for further processing or usage.
19
- - **Header Options:** Choose whether to include a header row or use default alphabetical headers.
20
- - **Flexible Styling:** Apply custom CSS classes for styling the rendered Markdown.
21
-
22
- ## 📦 Installation
23
-
24
- Install the package via npm:
25
-
7
+ [![GitHub branch status](https://img.shields.io/github/checks-status/keithwalsh/react-markdown-table-ts/HEAD)](https://github.com/keithwalsh/react-markdown-table-ts/commits/HEAD/)
8
+
9
+ | ![Light Theme Example](public/light.png) | ![Dark Theme Example](public/dark.png) |
10
+ |------------------------------------------|-----------------------------------------|
11
+ | `'light'` theme | `'dark'` theme |
12
+
13
+ ## Overview
14
+ This library provides a React component for generating and displaying formatted Markdown tables with syntax highlighting. The core component is `MarkdownTable` which converts 2D array data into properly formatted Markdown table syntax.
15
+
16
+ ## API
17
+ ```typescript
18
+ interface MarkdownTableProps {
19
+ inputData?: string[][] | null;
20
+ columnAlignments?: readonly Alignment[];
21
+ isCompact?: boolean;
22
+ hasPadding?: boolean;
23
+ hasTabs?: boolean;
24
+ hasHeader?: boolean;
25
+ convertLineBreaks?: boolean;
26
+ theme?: 'light' | 'dark';
27
+ className?: string;
28
+ preStyle?: React.CSSProperties;
29
+ onGenerate?: (markdownTableString: string) => void;
30
+ }
26
31
  ```
27
-
28
- npm install react-markdown-table-ts
29
-
32
+ | Prop | Type | Default | Description |
33
+ |----------------------|-----------------------------------------|-------------|------------------------------------------------------------------------------------|
34
+ | `inputData` | `string[][] \| null` | `null` | The outer array represents rows. The inner array represent cells within each row. |
35
+ | `columnAlignments` | `readonly Alignment[]` | `[]` | Acceptable values are 'left', 'center', 'right', or 'none'. |
36
+ | `isCompact` | `boolean` | `false` | Disables column width alignment to provide a more compact markdown table string. |
37
+ | `hasPadding` | `boolean` | `true` | One space added before and after the content in each cell. |
38
+ | `hasTabs` | `boolean` | `false` | Adds a tab character after each \| and before the content. |
39
+ | `hasHeader` | `boolean` | `true` | Indicates whether the first row of `data` is a header. |
40
+ | `convertLineBreaks` | `boolean` | `false` | Replace newlines with <br> tags in table cells. |
41
+ | `theme` | `'light' \| 'dark'` | `light` | Controls the color scheme of the \<pre\> element display. |
42
+ | `className` | `string` | `undefined` | Class will be applied to the \<pre\> element display. |
43
+ | `preStyle` | `React.CSSProperties` | `undefined` | Allows direct styling of the display with CSS properties. |
44
+ | `onGenerate` | `(markdownTableString: string) => void` | `undefined` | Callback to receive the generated Markdown table string. |
45
+ ## Usage Patterns
46
+
47
+ 1. **Basic Table Generation**:
48
+ ```typescript
49
+ <MarkdownTable
50
+ inputData={[
51
+ ["Header 1", "Header 2"],
52
+ ["Row 1 Col 1", "Row 1 Col 2"]
53
+ ]}
54
+ />
55
+ ```
56
+ 2. **Column Alignment**:
57
+ ```typescript
58
+ <MarkdownTable
59
+ inputData={data}
60
+ columnAlignments={['left', 'center', 'right']}
61
+ />
62
+ ```
63
+ 3. **Auto-Generated Headers**:
64
+ ```typescript
65
+ <MarkdownTable
66
+ inputData={data}
67
+ hasHeader={false} // Will generate A, B, C... headers
68
+ />
30
69
  ```
31
70
 
32
- ## 🔧 API
71
+ ## Behaviors
33
72
 
34
- ### MarkdownTable Props
73
+ 1. **Input Validation**:
74
+ - Input must be non-null 2D string array
75
+ - All rows should contain string values
76
+ - Empty arrays are not allowed
77
+ - Column alignments must be valid ('left', 'center', 'right', 'none')
35
78
 
36
- | Prop | Type | Default | Description |
37
- | :------------------: | :-------------------------------------------: | :---------: | :-----------------------------------------: |
38
- | `data` | `string[][]` | `null` | The table data as a 2D array of strings |
39
- | `columnAlignments` | `('left' \| 'center' \| 'right' \| 'none')[]` | `[]` | Alignment for each column |
40
- | `isCompact` | `boolean` | `false` | Use minimal column widths |
41
- | `className` | `string` | `undefined` | CSS class for the `<pre>` tag |
42
- | `hasTabs` | `boolean` | `false` | Add tabs between table columns |
43
- | `canReplaceNewlines` | `boolean` | `false` | Replace newlines in cells with `<br>` tags |
44
- | `onTableCreate` | `(markdownString: string) => void` | `undefined` | Callback to receive the Markdown string |
45
- | `hasHeader` | `boolean` | `true` | Whether the first row of `data` is a header |
79
+ 2. **Column Width Handling**:
80
+ - Default: Adjusts columns to fit content
81
+ - `isCompact={true}`: Minimizes column widths
82
+ - Maintains minimum width of 3 characters for alignment indicators
46
83
 
47
- ## 🚀 Usage
84
+ 3. **Error Handling**:
85
+ - Returns error message string if validation fails
86
+ - Wraps errors in `MarkdownTableError` class
87
+ - Preserves stack traces for debugging
48
88
 
49
- ```jsx
50
- import React from 'react';
51
- import {MarkdownTable} from 'markdown-table-component';
89
+ 4. **Styling**:
90
+ - Uses Prism.js for syntax highlighting
91
+ - Supports light/dark themes
92
+ - Custom styles via `className` and `preStyle` props
52
93
 
53
- const App = () => {
54
- const data = [
55
- ['Header 1', 'Header 2', 'Header 3'],
56
- ['Row 1, Col 1', 'Row 1, Col 2', 'Row 1, Col 3'],
57
- ['Row 2, Col 1', 'Row 2, Col 2', 'Row 2, Col 3'],
58
- ];
94
+ ## Common Transformations
59
95
 
60
- return <MarkdownTable data={data} />;
61
- };
96
+ 1. **Data Formatting**:
97
+ - Newlines can be converted to `<br>` tags with `canReplaceNewlines`
98
+ - Padding can be controlled with `hasPadding`
99
+ - Tab spacing available with `hasTabs`
62
100
 
63
- export default App;
64
- ```
101
+ 2. **Header Generation**:
102
+ - Auto-generates A, B, C... headers when `hasHeader={false}`
103
+ - Supports custom headers via first row when `hasHeader={true}`
@@ -0,0 +1,143 @@
1
+ /**
2
+ * @fileoverview Storybook stories for the MarkdownTable component, demonstrating
3
+ * various configurations and use cases for markdown table generation and display.
4
+ */
5
+ /// <reference types="react" />
6
+ import type { StoryObj } from '@storybook/react';
7
+ import { MarkdownTable } from './index';
8
+ declare const meta: {
9
+ title: string;
10
+ component: import("react").FC<import("./types").MarkdownTableProps>;
11
+ parameters: {
12
+ layout: string;
13
+ };
14
+ tags: string[];
15
+ args: {
16
+ inputData: null;
17
+ columnAlignments: never[];
18
+ isCompact: false;
19
+ hasPadding: true;
20
+ hasTabs: false;
21
+ hasHeader: true;
22
+ convertLineBreaks: false;
23
+ theme: "light";
24
+ };
25
+ argTypes: {
26
+ inputData: {
27
+ control: {
28
+ type: "object";
29
+ };
30
+ description: string;
31
+ table: {
32
+ category: string;
33
+ type: {
34
+ summary: string;
35
+ };
36
+ };
37
+ };
38
+ columnAlignments: {
39
+ control: {
40
+ type: "object";
41
+ };
42
+ description: string;
43
+ table: {
44
+ category: string;
45
+ type: {
46
+ summary: string;
47
+ detail: string;
48
+ };
49
+ };
50
+ };
51
+ isCompact: {
52
+ control: "boolean";
53
+ description: string;
54
+ table: {
55
+ category: string;
56
+ type: {
57
+ summary: string;
58
+ };
59
+ };
60
+ };
61
+ hasPadding: {
62
+ control: "boolean";
63
+ description: string;
64
+ table: {
65
+ category: string;
66
+ type: {
67
+ summary: string;
68
+ };
69
+ };
70
+ };
71
+ hasTabs: {
72
+ control: "boolean";
73
+ description: string;
74
+ table: {
75
+ category: string;
76
+ type: {
77
+ summary: string;
78
+ };
79
+ };
80
+ };
81
+ hasHeader: {
82
+ control: "boolean";
83
+ description: string;
84
+ table: {
85
+ category: string;
86
+ type: {
87
+ summary: string;
88
+ };
89
+ };
90
+ };
91
+ convertLineBreaks: {
92
+ control: "boolean";
93
+ description: string;
94
+ table: {
95
+ category: string;
96
+ type: {
97
+ summary: string;
98
+ };
99
+ };
100
+ };
101
+ theme: {
102
+ options: string[];
103
+ control: {
104
+ type: "select";
105
+ };
106
+ description: string;
107
+ table: {
108
+ category: string;
109
+ type: {
110
+ summary: string;
111
+ };
112
+ defaultValue: {
113
+ summary: string;
114
+ };
115
+ };
116
+ };
117
+ className: {
118
+ control: "text";
119
+ description: string;
120
+ table: {
121
+ category: string;
122
+ type: {
123
+ summary: string;
124
+ };
125
+ };
126
+ };
127
+ preStyle: {
128
+ control: {
129
+ type: "object";
130
+ };
131
+ description: string;
132
+ table: {
133
+ category: string;
134
+ type: {
135
+ summary: string;
136
+ };
137
+ };
138
+ };
139
+ };
140
+ };
141
+ export default meta;
142
+ type Story = StoryObj<typeof MarkdownTable>;
143
+ export declare const Default: Story;