@tooee/renderers 0.2.4 → 0.3.0
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/code-blocks.d.ts +6 -6
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js +52 -42
- package/dist/code-blocks.js.map +1 -1
- package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
- package/dist/code-view.d.ts.map +1 -0
- package/dist/{CodeView.js → code-view.js} +5 -5
- package/dist/code-view.js.map +1 -0
- package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
- package/dist/command-palette.d.ts.map +1 -0
- package/dist/{CommandPalette.js → command-palette.js} +27 -24
- package/dist/command-palette.js.map +1 -0
- package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/{ContextMenu.js → context-menu.js} +24 -14
- package/dist/context-menu.js.map +1 -0
- package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
- package/dist/decoration-layer.d.ts.map +1 -0
- package/dist/decoration-layer.js +2 -0
- package/dist/decoration-layer.js.map +1 -0
- package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
- package/dist/document-bindings.d.ts.map +1 -0
- package/dist/document-bindings.js +2 -0
- package/dist/document-bindings.js.map +1 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +9 -13
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +98 -79
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +13 -13
- package/dist/markdown-view.d.ts.map +1 -0
- package/dist/markdown-view.js +455 -0
- package/dist/markdown-view.js.map +1 -0
- package/dist/mermaid.d.ts +5 -5
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +33 -25
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts +5 -5
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +64 -44
- package/dist/parsers.js.map +1 -1
- package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
- package/dist/row-document-renderable.d.ts.map +1 -0
- package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
- package/dist/row-document-renderable.js.map +1 -0
- package/dist/row-document.d.ts +1 -1
- package/dist/row-document.d.ts.map +1 -1
- package/dist/row-document.js +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/source.d.ts +6 -6
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -26
- package/dist/source.js.map +1 -1
- package/dist/table-types.d.ts.map +1 -1
- package/dist/{Table.d.ts → table.d.ts} +6 -6
- package/dist/table.d.ts.map +1 -0
- package/dist/{Table.js → table.js} +65 -35
- package/dist/table.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts +3 -0
- package/dist/use-gutter-palette.d.ts.map +1 -0
- package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
- package/dist/use-gutter-palette.js.map +1 -0
- package/package.json +5 -5
- package/src/code-blocks.tsx +122 -106
- package/src/{CodeView.tsx → code-view.tsx} +19 -14
- package/src/command-palette.tsx +163 -0
- package/src/context-menu.tsx +157 -0
- package/src/decoration-layer.ts +11 -0
- package/src/document-bindings.ts +16 -0
- package/src/index.ts +24 -23
- package/src/markdown-blocks.ts +178 -138
- package/src/markdown-view.tsx +853 -0
- package/src/mermaid.ts +94 -71
- package/src/parsers.ts +141 -103
- package/src/row-document-renderable.ts +672 -0
- package/src/row-document.ts +4 -4
- package/src/source.ts +92 -72
- package/src/table-types.ts +4 -4
- package/src/table.tsx +346 -0
- package/src/text-table.ts +4 -4
- package/src/use-gutter-palette.ts +15 -0
- package/dist/CodeView.d.ts.map +0 -1
- package/dist/CodeView.js.map +0 -1
- package/dist/CommandPalette.d.ts.map +0 -1
- package/dist/CommandPalette.js.map +0 -1
- package/dist/ContextMenu.d.ts.map +0 -1
- package/dist/ContextMenu.js.map +0 -1
- package/dist/DecorationLayer.d.ts.map +0 -1
- package/dist/DecorationLayer.js +0 -2
- package/dist/DecorationLayer.js.map +0 -1
- package/dist/DocumentBindings.d.ts.map +0 -1
- package/dist/DocumentBindings.js +0 -2
- package/dist/DocumentBindings.js.map +0 -1
- package/dist/MarkdownView.d.ts.map +0 -1
- package/dist/MarkdownView.js +0 -360
- package/dist/MarkdownView.js.map +0 -1
- package/dist/RowDocumentRenderable.d.ts.map +0 -1
- package/dist/RowDocumentRenderable.js.map +0 -1
- package/dist/Table.d.ts.map +0 -1
- package/dist/Table.js.map +0 -1
- package/dist/useGutterPalette.d.ts +0 -3
- package/dist/useGutterPalette.d.ts.map +0 -1
- package/dist/useGutterPalette.js.map +0 -1
- package/src/CommandPalette.tsx +0 -147
- package/src/ContextMenu.tsx +0 -132
- package/src/DecorationLayer.ts +0 -11
- package/src/DocumentBindings.ts +0 -16
- package/src/MarkdownView.tsx +0 -738
- package/src/RowDocumentRenderable.ts +0 -593
- package/src/Table.tsx +0 -300
- package/src/useGutterPalette.ts +0 -15
package/src/mermaid.ts
CHANGED
|
@@ -1,136 +1,159 @@
|
|
|
1
|
-
import { renderMermaidASCII } from "beautiful-mermaid"
|
|
2
|
-
import { parseColor, StyledText
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { renderMermaidASCII } from "beautiful-mermaid";
|
|
2
|
+
import { parseColor, StyledText } from "@opentui/core";
|
|
3
|
+
import type { TextChunk } from "@opentui/core";
|
|
4
|
+
|
|
5
|
+
interface BeautifulMermaidAsciiTheme {
|
|
6
|
+
fg?: string;
|
|
7
|
+
border?: string;
|
|
8
|
+
line?: string;
|
|
9
|
+
arrow?: string;
|
|
10
|
+
accent?: string;
|
|
11
|
+
bg?: string;
|
|
12
|
+
corner?: string;
|
|
13
|
+
junction?: string;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export type MermaidRenderMode = "plain" | "ansi"
|
|
16
|
+
export type MermaidRenderMode = "plain" | "ansi";
|
|
16
17
|
|
|
17
18
|
export type MermaidRenderResult =
|
|
18
19
|
| { ok: true; text: string; content: StyledText }
|
|
19
|
-
| { ok: false; reason: "empty" | "render-error"; message: string }
|
|
20
|
+
| { ok: false; reason: "empty" | "render-error"; message: string };
|
|
20
21
|
|
|
21
22
|
export interface MermaidRenderOptions {
|
|
22
|
-
mode?: MermaidRenderMode
|
|
23
|
-
theme?: BeautifulMermaidAsciiTheme
|
|
23
|
+
mode?: MermaidRenderMode;
|
|
24
|
+
theme?: BeautifulMermaidAsciiTheme;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
+
// oxlint-disable-next-line no-control-regex -- ANSI SGR sequences start with the ESC control character
|
|
28
|
+
const SGR_SEQUENCE = /\u001B\[(?<params>[0-9;]*)m/gu;
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
31
|
* Marked may include extra info-string content after the language. Treat only
|
|
30
32
|
* the first word as the fence language, matching common Markdown behavior.
|
|
31
33
|
*/
|
|
32
|
-
export function isMermaidFence(lang?: string): boolean {
|
|
33
|
-
return (lang ?? "").trim().split(/\s+/)[0]?.toLowerCase() === "mermaid"
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
export const isMermaidFence = function isMermaidFence(lang?: string): boolean {
|
|
35
|
+
return (lang ?? "").trim().split(/\s+/u)[0]?.toLowerCase() === "mermaid";
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const appendStyledChunk = function appendStyledChunk(
|
|
39
|
+
chunks: TextChunk[],
|
|
40
|
+
text: string,
|
|
41
|
+
fg?: string,
|
|
42
|
+
) {
|
|
43
|
+
if (text.length === 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
38
46
|
|
|
39
47
|
chunks.push({
|
|
40
48
|
__isChunk: true,
|
|
41
49
|
text,
|
|
42
|
-
...(fg ? { fg: parseColor(fg) } : {}),
|
|
43
|
-
})
|
|
44
|
-
}
|
|
50
|
+
...(fg !== undefined && fg !== "" ? { fg: parseColor(fg) } : {}),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
45
53
|
|
|
46
|
-
function sgrParams(rawParams: string): number[] {
|
|
47
|
-
if (rawParams === "")
|
|
48
|
-
|
|
49
|
-
}
|
|
54
|
+
const sgrParams = function sgrParams(rawParams: string): number[] {
|
|
55
|
+
if (rawParams === "") {
|
|
56
|
+
return [0];
|
|
57
|
+
}
|
|
58
|
+
return rawParams.split(";").map((param) => (param === "" ? 0 : Number(param)));
|
|
59
|
+
};
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
const updateAnsiForeground = function updateAnsiForeground(
|
|
62
|
+
params: number[],
|
|
63
|
+
currentFg: string | undefined,
|
|
64
|
+
): string | undefined {
|
|
65
|
+
let fg = currentFg;
|
|
53
66
|
|
|
54
|
-
for (let i = 0; i < params.length; i
|
|
55
|
-
const param = params[i]
|
|
67
|
+
for (let i = 0; i < params.length; i += 1) {
|
|
68
|
+
const param = params[i];
|
|
56
69
|
|
|
57
70
|
if (param === 0 || param === 39) {
|
|
58
|
-
fg = undefined
|
|
59
|
-
continue
|
|
71
|
+
fg = undefined;
|
|
72
|
+
continue;
|
|
60
73
|
}
|
|
61
74
|
|
|
62
75
|
if (param === 38 && params[i + 1] === 2) {
|
|
63
|
-
const r = params[i + 2]
|
|
64
|
-
const g = params[i + 3]
|
|
65
|
-
const b = params[i + 4]
|
|
66
|
-
|
|
67
|
-
if (
|
|
76
|
+
const r = params[i + 2];
|
|
77
|
+
const g = params[i + 3];
|
|
78
|
+
const b = params[i + 4];
|
|
79
|
+
|
|
80
|
+
if (
|
|
81
|
+
r !== null &&
|
|
82
|
+
r !== undefined &&
|
|
83
|
+
g !== null &&
|
|
84
|
+
g !== undefined &&
|
|
85
|
+
b !== null &&
|
|
86
|
+
b !== undefined
|
|
87
|
+
) {
|
|
68
88
|
fg = `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b
|
|
69
89
|
.toString(16)
|
|
70
|
-
.padStart(2, "0")}
|
|
71
|
-
i += 4
|
|
90
|
+
.padStart(2, "0")}`;
|
|
91
|
+
i += 4;
|
|
72
92
|
}
|
|
73
93
|
}
|
|
74
94
|
}
|
|
75
95
|
|
|
76
|
-
return fg
|
|
77
|
-
}
|
|
96
|
+
return fg;
|
|
97
|
+
};
|
|
78
98
|
|
|
79
99
|
/** Convert beautiful-mermaid truecolor ANSI output into OpenTUI StyledText. */
|
|
80
|
-
export function ansiToStyledText(input: string): {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
100
|
+
export const ansiToStyledText = function ansiToStyledText(input: string): {
|
|
101
|
+
text: string;
|
|
102
|
+
content: StyledText;
|
|
103
|
+
} {
|
|
104
|
+
const chunks: TextChunk[] = [];
|
|
105
|
+
let plainText = "";
|
|
106
|
+
let cursor = 0;
|
|
107
|
+
let currentFg: string | undefined;
|
|
85
108
|
|
|
86
109
|
for (const match of input.matchAll(SGR_SEQUENCE)) {
|
|
87
|
-
const index = match.index ?? 0
|
|
88
|
-
const literal = input.slice(cursor, index)
|
|
89
|
-
appendStyledChunk(chunks, literal, currentFg)
|
|
90
|
-
plainText += literal
|
|
110
|
+
const index = match.index ?? 0;
|
|
111
|
+
const literal = input.slice(cursor, index);
|
|
112
|
+
appendStyledChunk(chunks, literal, currentFg);
|
|
113
|
+
plainText += literal;
|
|
91
114
|
|
|
92
|
-
currentFg = updateAnsiForeground(sgrParams(match
|
|
93
|
-
cursor = index + match[0].length
|
|
115
|
+
currentFg = updateAnsiForeground(sgrParams(match.groups?.params ?? ""), currentFg);
|
|
116
|
+
cursor = index + match[0].length;
|
|
94
117
|
}
|
|
95
118
|
|
|
96
|
-
const tail = input.slice(cursor)
|
|
97
|
-
appendStyledChunk(chunks, tail, currentFg)
|
|
98
|
-
plainText += tail
|
|
119
|
+
const tail = input.slice(cursor);
|
|
120
|
+
appendStyledChunk(chunks, tail, currentFg);
|
|
121
|
+
plainText += tail;
|
|
99
122
|
|
|
100
|
-
return {
|
|
101
|
-
}
|
|
123
|
+
return { content: new StyledText(chunks), text: plainText };
|
|
124
|
+
};
|
|
102
125
|
|
|
103
126
|
/**
|
|
104
127
|
* Render Mermaid source for terminal display. This wrapper keeps third-party
|
|
105
128
|
* parser/layout failures out of React render paths so MarkdownView can fall
|
|
106
129
|
* back to the original source block.
|
|
107
130
|
*/
|
|
108
|
-
export function renderMermaidForTerminal(
|
|
131
|
+
export const renderMermaidForTerminal = function renderMermaidForTerminal(
|
|
109
132
|
source: string,
|
|
110
133
|
options: MermaidRenderOptions = {},
|
|
111
134
|
): MermaidRenderResult {
|
|
112
135
|
if (source.trim().length === 0) {
|
|
113
|
-
return {
|
|
136
|
+
return { message: "Mermaid block is empty", ok: false, reason: "empty" };
|
|
114
137
|
}
|
|
115
138
|
|
|
116
139
|
try {
|
|
117
|
-
const mode = options.mode ?? "plain"
|
|
140
|
+
const mode = options.mode ?? "plain";
|
|
118
141
|
const rendered = renderMermaidASCII(source, {
|
|
119
142
|
colorMode: mode === "ansi" ? "truecolor" : "none",
|
|
120
143
|
theme: options.theme,
|
|
121
|
-
}).trimEnd()
|
|
122
|
-
const { text, content } = ansiToStyledText(rendered)
|
|
144
|
+
}).trimEnd();
|
|
145
|
+
const { text, content } = ansiToStyledText(rendered);
|
|
123
146
|
|
|
124
147
|
if (text.trim().length === 0) {
|
|
125
|
-
return {
|
|
148
|
+
return { message: "Mermaid renderer returned no output", ok: false, reason: "empty" };
|
|
126
149
|
}
|
|
127
150
|
|
|
128
|
-
return { ok: true, text
|
|
151
|
+
return { content, ok: true, text };
|
|
129
152
|
} catch (error) {
|
|
130
153
|
return {
|
|
154
|
+
message: error instanceof Error ? error.message : String(error),
|
|
131
155
|
ok: false,
|
|
132
156
|
reason: "render-error",
|
|
133
|
-
|
|
134
|
-
}
|
|
157
|
+
};
|
|
135
158
|
}
|
|
136
|
-
}
|
|
159
|
+
};
|
package/src/parsers.ts
CHANGED
|
@@ -1,149 +1,187 @@
|
|
|
1
|
-
import type { ColumnDef, TableRow } from "./table-types.js"
|
|
1
|
+
import type { ColumnDef, TableRow } from "./table-types.js";
|
|
2
2
|
|
|
3
3
|
export interface ParsedTable {
|
|
4
|
-
columns: ColumnDef[]
|
|
5
|
-
rows: TableRow[]
|
|
6
|
-
format: Format
|
|
4
|
+
columns: ColumnDef[];
|
|
5
|
+
rows: TableRow[];
|
|
6
|
+
format: Format;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export function parseCSV(input: string): {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export const parseCSV = function parseCSV(input: string): {
|
|
10
|
+
columns: ColumnDef[];
|
|
11
|
+
rows: TableRow[];
|
|
12
|
+
} {
|
|
13
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
14
|
+
const lines = splitLines(input);
|
|
15
|
+
if (lines.length === 0) {
|
|
16
|
+
return { columns: [], rows: [] };
|
|
17
|
+
}
|
|
18
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
19
|
+
const columns = createColumnDefs(parseCSVLine(lines[0]));
|
|
20
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
21
|
+
const rows = buildRows(columns, lines.slice(1).map(parseCSVLine));
|
|
22
|
+
return { columns, rows };
|
|
23
|
+
};
|
|
16
24
|
|
|
17
|
-
function parseCSVLine(line: string): string[] {
|
|
18
|
-
const fields: string[] = []
|
|
19
|
-
let i = 0
|
|
25
|
+
const parseCSVLine = function parseCSVLine(line: string): string[] {
|
|
26
|
+
const fields: string[] = [];
|
|
27
|
+
let i = 0;
|
|
20
28
|
while (i < line.length) {
|
|
21
29
|
if (line[i] === '"') {
|
|
22
30
|
// Quoted field
|
|
23
|
-
i
|
|
24
|
-
let field = ""
|
|
31
|
+
i += 1;
|
|
32
|
+
let field = "";
|
|
25
33
|
while (i < line.length) {
|
|
26
34
|
if (line[i] === '"') {
|
|
27
35
|
if (i + 1 < line.length && line[i + 1] === '"') {
|
|
28
|
-
field += '"'
|
|
29
|
-
i += 2
|
|
36
|
+
field += '"';
|
|
37
|
+
i += 2;
|
|
30
38
|
} else {
|
|
31
|
-
i
|
|
32
|
-
break
|
|
39
|
+
i += 1;
|
|
40
|
+
break;
|
|
33
41
|
}
|
|
34
42
|
} else {
|
|
35
|
-
field += line[i]
|
|
36
|
-
i
|
|
43
|
+
field += line[i];
|
|
44
|
+
i += 1;
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
|
-
fields.push(field)
|
|
40
|
-
|
|
47
|
+
fields.push(field);
|
|
48
|
+
// Skip the comma after a quoted field.
|
|
49
|
+
if (i < line.length && line[i] === ",") {
|
|
50
|
+
i += 1;
|
|
51
|
+
}
|
|
41
52
|
} else {
|
|
42
|
-
const nextComma = line.indexOf(",", i)
|
|
53
|
+
const nextComma = line.indexOf(",", i);
|
|
43
54
|
if (nextComma === -1) {
|
|
44
|
-
fields.push(line.slice(i))
|
|
45
|
-
break
|
|
55
|
+
fields.push(line.slice(i));
|
|
56
|
+
break;
|
|
46
57
|
} else {
|
|
47
|
-
fields.push(line.slice(i, nextComma))
|
|
48
|
-
i = nextComma + 1
|
|
58
|
+
fields.push(line.slice(i, nextComma));
|
|
59
|
+
i = nextComma + 1;
|
|
49
60
|
}
|
|
50
61
|
}
|
|
51
62
|
}
|
|
52
|
-
return fields
|
|
53
|
-
}
|
|
63
|
+
return fields;
|
|
64
|
+
};
|
|
54
65
|
|
|
55
|
-
export function parseTSV(input: string): {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
export const parseTSV = function parseTSV(input: string): {
|
|
67
|
+
columns: ColumnDef[];
|
|
68
|
+
rows: TableRow[];
|
|
69
|
+
} {
|
|
70
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
71
|
+
const lines = splitLines(input);
|
|
72
|
+
if (lines.length === 0) {
|
|
73
|
+
return { columns: [], rows: [] };
|
|
74
|
+
}
|
|
75
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
76
|
+
const columns = createColumnDefs(lines[0].split("\t"));
|
|
77
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): parser helpers are kept below the public entry points
|
|
59
78
|
const rows = buildRows(
|
|
60
79
|
columns,
|
|
61
80
|
lines.slice(1).map((line) => line.split("\t")),
|
|
62
|
-
)
|
|
63
|
-
return { columns, rows }
|
|
64
|
-
}
|
|
81
|
+
);
|
|
82
|
+
return { columns, rows };
|
|
83
|
+
};
|
|
65
84
|
|
|
66
|
-
export function parseJSON(input: string): {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
85
|
+
export const parseJSON = function parseJSON(input: string): {
|
|
86
|
+
columns: ColumnDef[];
|
|
87
|
+
rows: TableRow[];
|
|
88
|
+
} {
|
|
89
|
+
const data: unknown = JSON.parse(input);
|
|
90
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
91
|
+
return { columns: [], rows: [] };
|
|
92
|
+
}
|
|
93
|
+
const keys = [...new Set(data.flatMap((item: Record<string, unknown>) => Object.keys(item)))];
|
|
94
|
+
const columns: ColumnDef[] = keys.map((key) => ({ header: key, key }));
|
|
73
95
|
const rows = data.map((item: Record<string, unknown>) => {
|
|
74
|
-
const row: TableRow = {}
|
|
96
|
+
const row: TableRow = {};
|
|
75
97
|
for (const column of columns) {
|
|
76
|
-
row[column.key] = item[column.key] ?? ""
|
|
98
|
+
row[column.key] = item[column.key] ?? "";
|
|
77
99
|
}
|
|
78
|
-
return row
|
|
79
|
-
})
|
|
80
|
-
return { columns, rows }
|
|
81
|
-
}
|
|
100
|
+
return row;
|
|
101
|
+
});
|
|
102
|
+
return { columns, rows };
|
|
103
|
+
};
|
|
82
104
|
|
|
83
|
-
export type Format = "csv" | "tsv" | "json" | "unknown"
|
|
105
|
+
export type Format = "csv" | "tsv" | "json" | "unknown";
|
|
84
106
|
|
|
85
|
-
export function detectFormat(input: string): Format {
|
|
86
|
-
const trimmed = input.trimStart()
|
|
107
|
+
export const detectFormat = function detectFormat(input: string): Format {
|
|
108
|
+
const trimmed = input.trimStart();
|
|
87
109
|
if (trimmed.startsWith("[")) {
|
|
88
110
|
try {
|
|
89
|
-
const parsed = JSON.parse(trimmed)
|
|
90
|
-
if (Array.isArray(parsed))
|
|
91
|
-
|
|
111
|
+
const parsed: unknown = JSON.parse(trimmed);
|
|
112
|
+
if (Array.isArray(parsed)) {
|
|
113
|
+
return "json";
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
// Not valid JSON — fall through to the delimiter sniffing below.
|
|
117
|
+
}
|
|
92
118
|
}
|
|
93
|
-
const firstLine = input.split("\n")[0] ?? ""
|
|
94
|
-
if (firstLine.includes("\t"))
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
const firstLine = input.split("\n")[0] ?? "";
|
|
120
|
+
if (firstLine.includes("\t")) {
|
|
121
|
+
return "tsv";
|
|
122
|
+
}
|
|
123
|
+
if (firstLine.includes(",")) {
|
|
124
|
+
return "csv";
|
|
125
|
+
}
|
|
126
|
+
return "unknown";
|
|
127
|
+
};
|
|
98
128
|
|
|
99
|
-
export function parseAuto(input: string): ParsedTable {
|
|
100
|
-
const format = detectFormat(input)
|
|
101
|
-
let columns: ColumnDef[]
|
|
102
|
-
let rows: TableRow[]
|
|
129
|
+
export const parseAuto = function parseAuto(input: string): ParsedTable {
|
|
130
|
+
const format = detectFormat(input);
|
|
131
|
+
let columns: ColumnDef[];
|
|
132
|
+
let rows: TableRow[];
|
|
103
133
|
switch (format) {
|
|
104
|
-
case "csv":
|
|
105
|
-
|
|
106
|
-
break
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
134
|
+
case "csv": {
|
|
135
|
+
({ columns, rows } = parseCSV(input));
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case "tsv": {
|
|
139
|
+
({ columns, rows } = parseTSV(input));
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case "json": {
|
|
143
|
+
({ columns, rows } = parseJSON(input));
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case "unknown": {
|
|
114
147
|
// Fall back to CSV
|
|
115
|
-
|
|
116
|
-
break
|
|
148
|
+
({ columns, rows } = parseCSV(input));
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
default: {
|
|
152
|
+
({ columns, rows } = parseCSV(input));
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
117
155
|
}
|
|
118
|
-
return { columns,
|
|
119
|
-
}
|
|
156
|
+
return { columns, format, rows };
|
|
157
|
+
};
|
|
120
158
|
|
|
121
|
-
function splitLines(input: string): string[] {
|
|
122
|
-
return input.split("\n").filter((line) => line.trim().length > 0)
|
|
123
|
-
}
|
|
159
|
+
const splitLines = function splitLines(input: string): string[] {
|
|
160
|
+
return input.split("\n").filter((line) => line.trim().length > 0);
|
|
161
|
+
};
|
|
124
162
|
|
|
125
|
-
function createColumnDefs(rawHeaders: string[]): ColumnDef[] {
|
|
126
|
-
const seen = new Map<string, number>()
|
|
163
|
+
const createColumnDefs = function createColumnDefs(rawHeaders: string[]): ColumnDef[] {
|
|
164
|
+
const seen = new Map<string, number>();
|
|
127
165
|
return rawHeaders.map((header, index) => {
|
|
128
|
-
const trimmed = header.trim()
|
|
129
|
-
const fallback = `column_${index + 1}
|
|
130
|
-
const base = trimmed === "" ? fallback : trimmed
|
|
131
|
-
const count = seen.get(base) ?? 0
|
|
132
|
-
seen.set(base, count + 1)
|
|
133
|
-
const key = count === 0 ? base : `${base}_${count + 1}
|
|
166
|
+
const trimmed = header.trim();
|
|
167
|
+
const fallback = `column_${index + 1}`;
|
|
168
|
+
const base = trimmed === "" ? fallback : trimmed;
|
|
169
|
+
const count = seen.get(base) ?? 0;
|
|
170
|
+
seen.set(base, count + 1);
|
|
171
|
+
const key = count === 0 ? base : `${base}_${count + 1}`;
|
|
134
172
|
return {
|
|
135
|
-
key,
|
|
136
173
|
header: trimmed || undefined,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
174
|
+
key,
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
};
|
|
140
178
|
|
|
141
|
-
function buildRows(columns: ColumnDef[], rawRows: string[][]): TableRow[] {
|
|
179
|
+
const buildRows = function buildRows(columns: ColumnDef[], rawRows: string[][]): TableRow[] {
|
|
142
180
|
return rawRows.map((row) => {
|
|
143
|
-
const record: TableRow = {}
|
|
144
|
-
columns.
|
|
145
|
-
record[column.key] = row[index] ?? ""
|
|
146
|
-
}
|
|
147
|
-
return record
|
|
148
|
-
})
|
|
149
|
-
}
|
|
181
|
+
const record: TableRow = {};
|
|
182
|
+
for (const [index, column] of columns.entries()) {
|
|
183
|
+
record[column.key] = row[index] ?? "";
|
|
184
|
+
}
|
|
185
|
+
return record;
|
|
186
|
+
});
|
|
187
|
+
};
|