react-markdown-table-ts 1.3.0 → 1.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/src/types.ts DELETED
@@ -1,138 +0,0 @@
1
- /**
2
- * @fileoverview Type definitions for the MarkdownTable component including props,
3
- * alignment options, and table configuration.
4
- */
5
-
6
- export interface InputData {
7
- inputDataHeader: string[];
8
- inputDataBody: readonly string[][];
9
- }
10
-
11
- export interface MarkdownTableProps {
12
- /**
13
- * The entire table data as a two-dimensional array.
14
- * If `hasHeader` is true, the first row is treated as the header.
15
- * @default null
16
- */
17
- inputData?: string[][] | null;
18
-
19
- /**
20
- * Optional array specifying the alignment for each column.
21
- * Acceptable values are 'left', 'center', 'right', or 'none'.
22
- * @default []
23
- */
24
- columnAlignments?: readonly Alignment[];
25
-
26
- /**
27
- * Disables column width alignment to provide a more compact markdown table string.
28
- * @default false
29
- */
30
- isCompact?: boolean;
31
-
32
- /**
33
- * Optional flag to add a single space around cell content in the markdown table.
34
- * When true, one space is added before and after the content in each cell.
35
- * @default true
36
- */
37
- hasPadding?: boolean;
38
-
39
- /**
40
- * Optional flag to add tabs as additional padding between column pipes.
41
- * When true, adds a tab character after each | and before the content.
42
- * @default false
43
- */
44
- hasTabs?: boolean;
45
- /**
46
- * Indicates whether the first row of `data` is a header.
47
- * @default true
48
- */
49
- hasHeader?: boolean;
50
-
51
- /**
52
- * Optional flag to replace newlines with <br> tags in table cells.
53
- * @default false
54
- */
55
- convertLineBreaks?: boolean;
56
-
57
- /**
58
- * Optional theme for the Markdown table.
59
- * Acceptable values are 'light' or 'dark'.
60
- * @default 'light'
61
- */
62
- theme?: 'light' | 'dark'
63
-
64
- /**
65
- * Optional CSS class for styling the rendered Markdown table.
66
- * This class will be applied to the <pre> element containing the table.
67
- * It will be combined with built-in classes for syntax highlighting.
68
- * @default undefined
69
- * @example
70
- * <MarkdownTable
71
- * inputData={data}
72
- * className="custom-table-style"
73
- * />
74
- */
75
- className?: string;
76
-
77
- /**
78
- * Optional CSS properties to apply to the pre element containing the Markdown table.
79
- * Allows direct styling of the table container using React's style prop.
80
- * @default undefined
81
- * @example
82
- * <MarkdownTable
83
- * inputData={data}
84
- * preStyle={{
85
- * maxHeight: '300px',
86
- * overflow: 'auto',
87
- * backgroundColor: '#f5f5f5'
88
- * }}
89
- * />
90
- */
91
- preStyle?: React.CSSProperties;
92
-
93
- /**
94
- * Optional callback function to receive the generated Markdown table string.
95
- */
96
- onGenerate?: (markdownTableString: string) => void;
97
-
98
- /**
99
- * Optional top padding for the pre element.
100
- * @default 0
101
- */
102
- topPadding?: number;
103
-
104
- /**
105
- * Optional minimum width in pixels for the pre element containing the Markdown table.
106
- * @default undefined
107
- * @example
108
- * <MarkdownTable
109
- * inputData={data}
110
- * minWidth={300}
111
- * />
112
- */
113
- minWidth?: number;
114
-
115
- /**
116
- * Optional flag to show or hide line numbers in the Prism syntax highlighting.
117
- * When true, line numbers are displayed on the left side of the code block.
118
- * @default true
119
- * @example
120
- * <MarkdownTable
121
- * inputData={data}
122
- * showLineNumbers={false}
123
- * />
124
- */
125
- showLineNumbers?: boolean;
126
-
127
- }
128
-
129
- export type Alignment = 'left' | 'right' | 'center' | 'none';
130
-
131
- export interface TableConfig {
132
- columnCount: number;
133
- columnAlignments: readonly Alignment[];
134
- columnWidths?: readonly number[];
135
- useTabs: boolean;
136
- replaceNewlines: boolean;
137
- hasPadding: boolean;
138
- }
package/src/utils.ts DELETED
@@ -1,180 +0,0 @@
1
- /**
2
- * @fileoverview Utility functions and classes for generating formatted markdown
3
- * table strings with alignment, padding, and column width adjustments.
4
- */
5
-
6
- import type { InputData, Alignment, TableConfig } from './types';
7
-
8
- type TableRow = readonly string[];
9
-
10
- function calculatePadding(config: TableConfig): string {
11
- return config.useTabs ? '\t' : (config.hasPadding ? ' ' : '');
12
- }
13
-
14
- class CellFormatter {
15
- private readonly padding: string;
16
-
17
- constructor(config: TableConfig) {
18
- this.padding = calculatePadding(config);
19
- }
20
-
21
- formatCell(content: string, alignment: Alignment, width: number): string {
22
- const totalWidth = width;
23
-
24
- switch (alignment) {
25
- case 'right':
26
- return `${this.padding}${content.padStart(totalWidth)}${this.padding}`;
27
- case 'center': {
28
- const totalPadding = totalWidth - content.length;
29
- const paddingLeft = Math.floor(totalPadding / 2);
30
- const paddingRight = totalPadding - paddingLeft;
31
- return `${this.padding}${' '.repeat(paddingLeft)}${content}${' '.repeat(paddingRight)}${this.padding}`;
32
- }
33
- default: // left or none
34
- return `${this.padding}${content.padEnd(totalWidth)}${this.padding}`;
35
- }
36
- }
37
- }
38
-
39
- class AlignmentFormatter {
40
- private static readonly indicators = {
41
- left: (width: number) => `:${'-'.repeat(width - 1)}`,
42
- right: (width: number) => `${'-'.repeat(width - 1)}:`,
43
- center: (width: number) => `:${'-'.repeat(width - 2)}:`,
44
- none: (width: number) => '-'.repeat(width)
45
- };
46
-
47
- static formatIndicator(alignment: Alignment, width: number): string {
48
- return this.indicators[alignment](width);
49
- }
50
- }
51
-
52
- class TableFormatter {
53
- private readonly config: TableConfig;
54
- private readonly cellFormatter: CellFormatter;
55
- private readonly adjustedAlignments: Alignment[];
56
-
57
- constructor(config: TableConfig) {
58
- this.config = config;
59
- this.cellFormatter = new CellFormatter(config);
60
- this.adjustedAlignments = this.getAdjustedAlignments();
61
- }
62
-
63
- private getAdjustedAlignments(): Alignment[] {
64
- const defaultAlignment: Alignment = 'none';
65
-
66
- return this.config.columnAlignments.length < this.config.columnCount
67
- ? [
68
- ...Array.from(this.config.columnAlignments),
69
- ...Array(this.config.columnCount - this.config.columnAlignments.length).fill(defaultAlignment),
70
- ]
71
- : Array.from(this.config.columnAlignments);
72
- }
73
-
74
- formatRow(row: TableRow): string {
75
- const formattedCells = Array.from({ length: this.config.columnCount }, (_, i) => {
76
- let cell = row[i] ?? '';
77
- if (this.config.replaceNewlines) {
78
- cell = cell.replace(/\n/g, '<br>');
79
- }
80
- const alignment = this.adjustedAlignments[i];
81
- const width = this.config.columnWidths ? this.config.columnWidths[i] : cell.length;
82
-
83
- return this.cellFormatter.formatCell(cell, alignment, width);
84
- });
85
-
86
- return `|${formattedCells.join('|')}|`;
87
- }
88
-
89
- formatAlignmentRow(): string {
90
- const padding = calculatePadding(this.config);
91
- const formattedColumns = Array.from({ length: this.config.columnCount }, (_, i) => {
92
- const alignment = this.adjustedAlignments[i];
93
- const width = this.config.columnWidths ? this.config.columnWidths[i] : 3;
94
- const indicator = AlignmentFormatter.formatIndicator(alignment, width);
95
- return `${padding}${indicator}${padding}`;
96
- });
97
-
98
- return `|${formattedColumns.join('|')}|`;
99
- }
100
- }
101
-
102
- function calculateColumnWidths(
103
- tableRows: readonly TableRow[],
104
- maxColumnCount: number
105
- ): number[] {
106
- const widths: number[] = new Array(maxColumnCount).fill(3);
107
-
108
- tableRows.forEach((row: TableRow) => {
109
- for (let i = 0; i < maxColumnCount; i++) {
110
- const cell = row[i] ?? '';
111
- widths[i] = Math.max(widths[i], cell.length);
112
- }
113
- });
114
-
115
- return widths;
116
- }
117
-
118
- function calculateMaxColumnCount(inputData: InputData): number {
119
- const headerColumnCount = inputData.inputDataHeader.length;
120
- const bodyColumnCounts = inputData.inputDataBody.map(row => row.length);
121
- return Math.max(headerColumnCount, ...bodyColumnCounts);
122
- }
123
-
124
- function getColumnWidths(
125
- inputData: InputData,
126
- maxColumnCount: number,
127
- canAdjustColumnWidths: boolean
128
- ): number[] | undefined {
129
- return canAdjustColumnWidths
130
- ? calculateColumnWidths(
131
- [inputData.inputDataHeader, ...inputData.inputDataBody],
132
- maxColumnCount
133
- )
134
- : undefined;
135
- }
136
-
137
- export function generateMarkdownTableString(
138
- inputData: InputData,
139
- columnAlignments: readonly Alignment[],
140
- canAdjustColumnWidths = true,
141
- useTabs = false,
142
- replaceNewlines = false,
143
- hasPadding = true
144
- ): string {
145
- const maxColumnCount = calculateMaxColumnCount(inputData);
146
- const columnWidths = getColumnWidths(inputData, maxColumnCount, canAdjustColumnWidths);
147
-
148
- const config: TableConfig = {
149
- columnCount: maxColumnCount,
150
- columnAlignments,
151
- columnWidths,
152
- useTabs,
153
- replaceNewlines,
154
- hasPadding
155
- };
156
-
157
- const tableFormatter = new TableFormatter(config);
158
-
159
- const headerRow = tableFormatter.formatRow(inputData.inputDataHeader);
160
- const alignmentRow = tableFormatter.formatAlignmentRow();
161
- const bodyRows = inputData.inputDataBody
162
- .map(row => tableFormatter.formatRow(row))
163
- .join('\n');
164
-
165
- return `${headerRow}\n${alignmentRow}\n${bodyRows}`.trimEnd();
166
- }
167
-
168
- function getColumnName(index: number): string {
169
- let columnName = '';
170
- let currentIndex = index;
171
- while (currentIndex >= 0) {
172
- columnName = String.fromCharCode((currentIndex % 26) + 65) + columnName;
173
- currentIndex = Math.floor(currentIndex / 26) - 1;
174
- }
175
- return columnName;
176
- }
177
-
178
- export function generateAlphabetHeaders(columnCount: number): string[] {
179
- return Array.from({ length: columnCount }, (_, i) => getColumnName(i));
180
- }
package/src/validation.ts DELETED
@@ -1,31 +0,0 @@
1
- /**
2
- * @fileoverview Validation utilities and custom error class for markdown table
3
- * data validation.
4
- */
5
-
6
- /**
7
- * Custom error class for handling Markdown table generation errors.
8
- * Supports ES2022 error chaining via the optional `cause` property.
9
- */
10
- export class MarkdownTableError extends Error {
11
- constructor(message: string, options?: ErrorOptions) {
12
- super(message, options);
13
- this.name = 'MarkdownTableError';
14
- Object.setPrototypeOf(this, MarkdownTableError.prototype);
15
- }
16
- }
17
-
18
- /**
19
- * Validates that the input data is a non-empty two-dimensional array.
20
- * @param inputData - The data to validate
21
- * @throws {MarkdownTableError} If validation fails
22
- */
23
- export function validateInputData(inputData: unknown): void {
24
- if (inputData === null || !Array.isArray(inputData)) {
25
- throw new MarkdownTableError("The 'data' prop must be a two-dimensional array.");
26
- }
27
-
28
- if (inputData.length === 0) {
29
- throw new MarkdownTableError("The 'data' array must contain at least one row.");
30
- }
31
- }
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "useDefineForClassFields": true,
5
- "lib": ["ES2022", "DOM", "DOM.Iterable"],
6
- "module": "ESNext",
7
- "types": ["vite/client", "node"],
8
- "skipLibCheck": true,
9
-
10
- /* Bundler mode */
11
- "moduleResolution": "bundler",
12
- "allowImportingTsExtensions": true,
13
- "verbatimModuleSyntax": true,
14
- "moduleDetection": "force",
15
- "noEmit": true,
16
- "jsx": "react-jsx",
17
-
18
- /* Linting */
19
- "strict": true,
20
- "noUnusedLocals": true,
21
- "noUnusedParameters": true,
22
- "erasableSyntaxOnly": true,
23
- "noFallthroughCasesInSwitch": true,
24
- "noUncheckedSideEffectImports": true
25
- },
26
- "include": ["src", "vite.config.ts"]
27
- }
package/vite.config.ts DELETED
@@ -1,7 +0,0 @@
1
- import { defineConfig } from 'vite'
2
- import react from '@vitejs/plugin-react-swc'
3
-
4
- // https://vite.dev/config/
5
- export default defineConfig({
6
- plugins: [react()],
7
- })
File without changes
File without changes