@tooee/renderers 0.12.0 → 0.14.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/chooser/choose-filter.d.ts.map +1 -1
- package/dist/chooser/choose-filter.js.map +1 -1
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
- package/dist/chooser/choose-highlighted-text.js.map +1 -1
- package/dist/chooser/choose-list.d.ts.map +1 -1
- package/dist/chooser/choose-list.js.map +1 -1
- package/dist/chooser/choose-store.d.ts +3 -3
- package/dist/chooser/choose-store.d.ts.map +1 -1
- package/dist/chooser/choose-store.js.map +1 -1
- package/dist/chooser/chooser.d.ts.map +1 -1
- package/dist/chooser/chooser.js.map +1 -1
- package/dist/chooser/source.d.ts.map +1 -1
- package/dist/chooser/source.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.js.map +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js +38 -33
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts.map +1 -1
- package/dist/markdown/blocks/heading.js.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts +2 -2
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js +2 -3
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
- package/dist/markdown/blocks/paragraph.js.map +1 -1
- package/dist/markdown/blocks/table.d.ts +17 -3
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js +29 -29
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts +10 -0
- package/dist/markdown/links.d.ts.map +1 -1
- package/dist/markdown/links.js +30 -18
- package/dist/markdown/links.js.map +1 -1
- package/dist/markdown/plain-text.d.ts.map +1 -1
- package/dist/markdown/plain-text.js.map +1 -1
- package/dist/markdown/table-layout.d.ts +28 -0
- package/dist/markdown/table-layout.d.ts.map +1 -0
- package/dist/markdown/table-layout.js +100 -0
- package/dist/markdown/table-layout.js.map +1 -0
- package/dist/markdown/table-links.d.ts +5 -4
- package/dist/markdown/table-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js +6 -23
- package/dist/markdown/table-links.js.map +1 -1
- package/dist/markdown/tokens.d.ts +2 -0
- package/dist/markdown/tokens.d.ts.map +1 -1
- package/dist/markdown/tokens.js.map +1 -1
- package/dist/markdown/visit-inline.d.ts.map +1 -1
- package/dist/markdown/visit-inline.js.map +1 -1
- package/dist/markdown-blocks.d.ts +72 -14
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +137 -77
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js +5 -8
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts +0 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js +0 -1
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +8 -4
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js.map +1 -1
- package/dist/row-document-renderable.d.ts.map +1 -1
- package/dist/row-document-renderable.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js.map +1 -1
- package/package.json +6 -6
- package/src/chooser/choose-filter.tsx +1 -0
- package/src/chooser/choose-highlighted-text.tsx +2 -0
- package/src/chooser/choose-list.tsx +6 -0
- package/src/chooser/choose-store.ts +20 -3
- package/src/chooser/chooser.tsx +2 -0
- package/src/chooser/source.ts +5 -0
- package/src/chooser/use-choose.ts +44 -0
- package/src/code-blocks.tsx +14 -0
- package/src/command-palette.tsx +4 -0
- package/src/context-menu.tsx +13 -0
- package/src/index.ts +56 -1
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +113 -80
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +6 -6
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +84 -49
- package/src/markdown/links.ts +69 -25
- package/src/markdown/plain-text.ts +5 -0
- package/src/markdown/table-layout.ts +156 -0
- package/src/markdown/table-links.ts +17 -25
- package/src/markdown/tokens.ts +4 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +298 -100
- package/src/markdown-images.ts +30 -9
- package/src/markdown-view.tsx +4 -1
- package/src/mermaid.ts +21 -4
- package/src/parsers.ts +39 -0
- package/src/row-document-renderable.ts +53 -0
- package/src/source.ts +18 -0
- package/src/table.tsx +32 -0
- package/dist/text-table-internals.d.ts +0 -34
- package/dist/text-table-internals.d.ts.map +0 -1
- package/dist/text-table-internals.js +0 -68
- package/dist/text-table-internals.js.map +0 -1
- package/dist/text-table.d.ts +0 -7
- package/dist/text-table.d.ts.map +0 -1
- package/dist/text-table.js +0 -4
- package/dist/text-table.js.map +0 -1
- package/src/text-table-internals.ts +0 -124
- package/src/text-table.ts +0 -10
package/src/markdown-images.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
|
|
3
|
-
import type { Token
|
|
3
|
+
import type { Token } from "marked";
|
|
4
|
+
|
|
5
|
+
import { narrowToken } from "./markdown/tokens.js";
|
|
4
6
|
|
|
5
7
|
const OBSIDIAN_IMAGE_EMBED = /!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]/gu;
|
|
8
|
+
|
|
6
9
|
const IMAGE_DIMENSIONS = /^(?<width>\d+)(?:x(?<height>\d+))?$/u;
|
|
10
|
+
|
|
7
11
|
const URL_SCHEME = /^[a-z][a-z\d+.-]*:/iu;
|
|
8
12
|
|
|
9
13
|
export interface MarkdownImageEmbed {
|
|
@@ -23,6 +27,7 @@ const obsidianEmbed = function obsidianEmbed(
|
|
|
23
27
|
): MarkdownImageEmbed {
|
|
24
28
|
const source = target.trim();
|
|
25
29
|
const dimensions = display?.trim().match(IMAGE_DIMENSIONS);
|
|
30
|
+
|
|
26
31
|
if (dimensions?.groups?.width !== undefined) {
|
|
27
32
|
return {
|
|
28
33
|
height: dimensions.groups.height === undefined ? undefined : Number(dimensions.groups.height),
|
|
@@ -30,7 +35,9 @@ const obsidianEmbed = function obsidianEmbed(
|
|
|
30
35
|
width: Number(dimensions.groups.width),
|
|
31
36
|
};
|
|
32
37
|
}
|
|
38
|
+
|
|
33
39
|
const alt = display?.trim();
|
|
40
|
+
|
|
34
41
|
return { alt: alt === undefined || alt === "" ? undefined : alt, source };
|
|
35
42
|
};
|
|
36
43
|
|
|
@@ -39,17 +46,21 @@ export const parseObsidianImageEmbed = function parseObsidianImageEmbed(
|
|
|
39
46
|
value: string
|
|
40
47
|
): MarkdownImageEmbed | null {
|
|
41
48
|
const match = /^!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]$/u.exec(value.trim());
|
|
49
|
+
|
|
42
50
|
if (match?.groups?.target === undefined) {
|
|
43
51
|
return null;
|
|
44
52
|
}
|
|
53
|
+
|
|
45
54
|
return obsidianEmbed(match.groups.target, match.groups.display);
|
|
46
55
|
};
|
|
47
56
|
|
|
48
57
|
const splitTextToken = function splitTextToken(text: string): MarkdownInlineSegment[] {
|
|
49
58
|
const segments: MarkdownInlineSegment[] = [];
|
|
50
59
|
let cursor = 0;
|
|
60
|
+
|
|
51
61
|
for (const match of text.matchAll(OBSIDIAN_IMAGE_EMBED)) {
|
|
52
62
|
const { index } = match;
|
|
63
|
+
|
|
53
64
|
if (index > cursor) {
|
|
54
65
|
segments.push({
|
|
55
66
|
tokens: [
|
|
@@ -62,18 +73,23 @@ const splitTextToken = function splitTextToken(text: string): MarkdownInlineSegm
|
|
|
62
73
|
type: "text",
|
|
63
74
|
});
|
|
64
75
|
}
|
|
76
|
+
|
|
65
77
|
const target = match.groups?.target;
|
|
78
|
+
|
|
66
79
|
if (target !== undefined) {
|
|
67
80
|
segments.push({ ...obsidianEmbed(target, match.groups?.display), type: "image" });
|
|
68
81
|
}
|
|
82
|
+
|
|
69
83
|
cursor = index + match[0].length;
|
|
70
84
|
}
|
|
85
|
+
|
|
71
86
|
if (cursor < text.length) {
|
|
72
87
|
segments.push({
|
|
73
88
|
tokens: [{ raw: text.slice(cursor), text: text.slice(cursor), type: "text" }],
|
|
74
89
|
type: "text",
|
|
75
90
|
});
|
|
76
91
|
}
|
|
92
|
+
|
|
77
93
|
return segments.length > 0
|
|
78
94
|
? segments
|
|
79
95
|
: [{ tokens: [{ raw: text, text, type: "text" }], type: "text" }];
|
|
@@ -94,10 +110,9 @@ export const splitMarkdownImages = function splitMarkdownImages(
|
|
|
94
110
|
};
|
|
95
111
|
|
|
96
112
|
for (const token of tokens) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const imageToken = token as Tokens.Image;
|
|
113
|
+
const imageToken = narrowToken(token, "image");
|
|
114
|
+
|
|
115
|
+
if (imageToken !== null) {
|
|
101
116
|
flushText();
|
|
102
117
|
result.push({
|
|
103
118
|
alt: imageToken.text === "" ? undefined : imageToken.text,
|
|
@@ -106,11 +121,12 @@ export const splitMarkdownImages = function splitMarkdownImages(
|
|
|
106
121
|
});
|
|
107
122
|
continue;
|
|
108
123
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
124
|
+
|
|
125
|
+
const textToken = narrowToken(token, "text");
|
|
126
|
+
|
|
127
|
+
if (textToken !== null) {
|
|
113
128
|
const parts = splitTextToken(textToken.text);
|
|
129
|
+
|
|
114
130
|
for (const part of parts) {
|
|
115
131
|
if (part.type === "text") {
|
|
116
132
|
pendingText.push(...part.tokens);
|
|
@@ -119,11 +135,15 @@ export const splitMarkdownImages = function splitMarkdownImages(
|
|
|
119
135
|
result.push(part);
|
|
120
136
|
}
|
|
121
137
|
}
|
|
138
|
+
|
|
122
139
|
continue;
|
|
123
140
|
}
|
|
141
|
+
|
|
124
142
|
pendingText.push(token);
|
|
125
143
|
}
|
|
144
|
+
|
|
126
145
|
flushText();
|
|
146
|
+
|
|
127
147
|
return result;
|
|
128
148
|
};
|
|
129
149
|
|
|
@@ -135,5 +155,6 @@ export const resolveMarkdownImageSource = function resolveMarkdownImageSource(
|
|
|
135
155
|
if (path.isAbsolute(source) || URL_SCHEME.test(source)) {
|
|
136
156
|
return source;
|
|
137
157
|
}
|
|
158
|
+
|
|
138
159
|
return path.resolve(basePath ?? process.cwd(), source);
|
|
139
160
|
};
|
package/src/markdown-view.tsx
CHANGED
|
@@ -17,9 +17,9 @@ import {
|
|
|
17
17
|
} from "./row-document-renderable.js";
|
|
18
18
|
import { useGutterPalette } from "./use-gutter-palette.js";
|
|
19
19
|
import "./row-document.js";
|
|
20
|
-
import "./text-table.js";
|
|
21
20
|
|
|
22
21
|
export { inlineLinkAtPosition } from "./markdown/links.js";
|
|
22
|
+
|
|
23
23
|
export type { InlineLinkPosition, MarkdownLinkHandler } from "./markdown/links.js";
|
|
24
24
|
|
|
25
25
|
type CodeBlockRendererRegistry = Record<string, CodeBlockRenderer>;
|
|
@@ -91,6 +91,7 @@ export const MarkdownView = function MarkdownView({
|
|
|
91
91
|
const { theme, syntax } = useTheme();
|
|
92
92
|
const palette = useGutterPalette();
|
|
93
93
|
const { width: terminalWidth } = useTerminalDimensions();
|
|
94
|
+
|
|
94
95
|
const blocks = useMemo(
|
|
95
96
|
() => providedBlocks ?? flattenMarkdown(content),
|
|
96
97
|
[providedBlocks, content]
|
|
@@ -110,9 +111,11 @@ export const MarkdownView = function MarkdownView({
|
|
|
110
111
|
|
|
111
112
|
const mergedCodeBlockRenderers = useMemo(() => {
|
|
112
113
|
const entries = new Map(Object.entries(DEFAULT_CODE_BLOCK_RENDERERS));
|
|
114
|
+
|
|
113
115
|
for (const [key, renderer] of Object.entries(codeBlockRenderers ?? {})) {
|
|
114
116
|
entries.set(key.trim().toLowerCase(), renderer);
|
|
115
117
|
}
|
|
118
|
+
|
|
116
119
|
return Object.fromEntries(entries);
|
|
117
120
|
}, [codeBlockRenderers]);
|
|
118
121
|
|
package/src/mermaid.ts
CHANGED
|
@@ -34,8 +34,11 @@ export interface AnsiStyledText {
|
|
|
34
34
|
// an edge cannot be routed. Keep synchronous work within a conservative budget;
|
|
35
35
|
// MarkdownView will visibly fall back to the original Mermaid source instead.
|
|
36
36
|
const MAX_SOURCE_LENGTH = 20_000;
|
|
37
|
+
|
|
37
38
|
const MAX_SOURCE_LINES = 300;
|
|
39
|
+
|
|
38
40
|
const MAX_FLOWCHART_EDGES = 32;
|
|
41
|
+
|
|
39
42
|
const MIN_UNSAFE_CYCLIC_FAN_DEGREE = 3;
|
|
40
43
|
|
|
41
44
|
// oxlint-disable-next-line no-control-regex -- ANSI SGR sequences start with the ESC control character
|
|
@@ -59,12 +62,15 @@ const hasDirectedPath = function hasDirectedPath(
|
|
|
59
62
|
|
|
60
63
|
while (pending.length > 0) {
|
|
61
64
|
const node = pending.pop();
|
|
65
|
+
|
|
62
66
|
if (node === undefined) {
|
|
63
67
|
continue;
|
|
64
68
|
}
|
|
69
|
+
|
|
65
70
|
if (node === target) {
|
|
66
71
|
return true;
|
|
67
72
|
}
|
|
73
|
+
|
|
68
74
|
if (visited.has(node)) {
|
|
69
75
|
continue;
|
|
70
76
|
}
|
|
@@ -91,12 +97,17 @@ const hasUnsafeCyclicFanTopology = function hasUnsafeCyclicFanTopology(
|
|
|
91
97
|
outgoingDegree.set(edge.source, (outgoingDegree.get(edge.source) ?? 0) + 1);
|
|
92
98
|
}
|
|
93
99
|
|
|
94
|
-
const fanInNodes =
|
|
100
|
+
const fanInNodes = incomingDegree
|
|
101
|
+
.entries()
|
|
95
102
|
.filter(([, degree]) => degree >= MIN_UNSAFE_CYCLIC_FAN_DEGREE)
|
|
96
|
-
.map(([node]) => node)
|
|
97
|
-
|
|
103
|
+
.map(([node]) => node)
|
|
104
|
+
.toArray();
|
|
105
|
+
|
|
106
|
+
const fanOutNodes = outgoingDegree
|
|
107
|
+
.entries()
|
|
98
108
|
.filter(([, degree]) => degree >= MIN_UNSAFE_CYCLIC_FAN_DEGREE)
|
|
99
|
-
.map(([node]) => node)
|
|
109
|
+
.map(([node]) => node)
|
|
110
|
+
.toArray();
|
|
100
111
|
|
|
101
112
|
return fanInNodes.some((fanIn) =>
|
|
102
113
|
fanOutNodes.some(
|
|
@@ -121,6 +132,7 @@ const exceedsSynchronousRenderBudget = function exceedsSynchronousRenderBudget(
|
|
|
121
132
|
}
|
|
122
133
|
|
|
123
134
|
const { edges } = parseMermaid(source);
|
|
135
|
+
|
|
124
136
|
return edges.length > MAX_FLOWCHART_EDGES || hasUnsafeCyclicFanTopology(edges);
|
|
125
137
|
};
|
|
126
138
|
|
|
@@ -134,9 +146,11 @@ const appendStyledChunk = function appendStyledChunk(
|
|
|
134
146
|
}
|
|
135
147
|
|
|
136
148
|
const chunk: TextChunk = { __isChunk: true, text };
|
|
149
|
+
|
|
137
150
|
if (fg !== undefined && fg !== "") {
|
|
138
151
|
chunk.fg = parseColor(fg);
|
|
139
152
|
}
|
|
153
|
+
|
|
140
154
|
chunks.push(chunk);
|
|
141
155
|
};
|
|
142
156
|
|
|
@@ -144,6 +158,7 @@ const sgrParams = function sgrParams(rawParams: string): number[] {
|
|
|
144
158
|
if (rawParams === "") {
|
|
145
159
|
return [0];
|
|
146
160
|
}
|
|
161
|
+
|
|
147
162
|
return rawParams.split(";").map((param) => (param === "" ? 0 : Number(param)));
|
|
148
163
|
};
|
|
149
164
|
|
|
@@ -232,10 +247,12 @@ export const renderMermaidForTerminal = function renderMermaidForTerminal(
|
|
|
232
247
|
}
|
|
233
248
|
|
|
234
249
|
const mode = options.mode ?? "plain";
|
|
250
|
+
|
|
235
251
|
const rendered = renderMermaidASCII(source, {
|
|
236
252
|
colorMode: mode === "ansi" ? "truecolor" : "none",
|
|
237
253
|
theme: options.theme,
|
|
238
254
|
}).trimEnd();
|
|
255
|
+
|
|
239
256
|
const { text, content } = ansiToStyledText(rendered);
|
|
240
257
|
|
|
241
258
|
if (text.trim().length === 0) {
|
package/src/parsers.ts
CHANGED
|
@@ -29,6 +29,7 @@ const jsonEntryKeys = function jsonEntryKeys(value: JsonValue): string[] {
|
|
|
29
29
|
if (value === null) {
|
|
30
30
|
throw new TypeError("A JSON table row cannot be null");
|
|
31
31
|
}
|
|
32
|
+
|
|
32
33
|
return Object.keys(value);
|
|
33
34
|
};
|
|
34
35
|
|
|
@@ -36,12 +37,15 @@ const readJsonEntry = function readJsonEntry(value: JsonValue, key: string): Jso
|
|
|
36
37
|
if (isJsonArray(value)) {
|
|
37
38
|
return value[Number(key)];
|
|
38
39
|
}
|
|
40
|
+
|
|
39
41
|
if (value instanceof Object) {
|
|
40
42
|
return value[key];
|
|
41
43
|
}
|
|
44
|
+
|
|
42
45
|
if (Object.prototype.toString.call(value) === "[object String]") {
|
|
43
46
|
return String.prototype.charAt.call(value, Number(key));
|
|
44
47
|
}
|
|
48
|
+
|
|
45
49
|
return undefined;
|
|
46
50
|
};
|
|
47
51
|
|
|
@@ -51,6 +55,7 @@ const splitLines = function splitLines(input: string): string[] {
|
|
|
51
55
|
|
|
52
56
|
const createColumnDefs = function createColumnDefs(rawHeaders: string[]): ColumnDef[] {
|
|
53
57
|
const seen = new Map<string, number>();
|
|
58
|
+
|
|
54
59
|
return rawHeaders.map((header, index) => {
|
|
55
60
|
const trimmed = header.trim();
|
|
56
61
|
const fallback = `column_${index + 1}`;
|
|
@@ -58,6 +63,7 @@ const createColumnDefs = function createColumnDefs(rawHeaders: string[]): Column
|
|
|
58
63
|
const count = seen.get(base) ?? 0;
|
|
59
64
|
seen.set(base, count + 1);
|
|
60
65
|
const key = count === 0 ? base : `${base}_${count + 1}`;
|
|
66
|
+
|
|
61
67
|
return {
|
|
62
68
|
header: trimmed || undefined,
|
|
63
69
|
key,
|
|
@@ -68,9 +74,11 @@ const createColumnDefs = function createColumnDefs(rawHeaders: string[]): Column
|
|
|
68
74
|
const buildRows = function buildRows(columns: ColumnDef[], rawRows: string[][]): TableRow[] {
|
|
69
75
|
return rawRows.map((row) => {
|
|
70
76
|
const record: TableRow = {};
|
|
77
|
+
|
|
71
78
|
for (const [index, column] of columns.entries()) {
|
|
72
79
|
record[column.key] = row[index] ?? "";
|
|
73
80
|
}
|
|
81
|
+
|
|
74
82
|
return record;
|
|
75
83
|
});
|
|
76
84
|
};
|
|
@@ -78,11 +86,13 @@ const buildRows = function buildRows(columns: ColumnDef[], rawRows: string[][]):
|
|
|
78
86
|
const parseCSVLine = function parseCSVLine(line: string): string[] {
|
|
79
87
|
const fields: string[] = [];
|
|
80
88
|
let i = 0;
|
|
89
|
+
|
|
81
90
|
while (i < line.length) {
|
|
82
91
|
if (line[i] === '"') {
|
|
83
92
|
// Quoted field
|
|
84
93
|
i += 1;
|
|
85
94
|
let field = "";
|
|
95
|
+
|
|
86
96
|
while (i < line.length) {
|
|
87
97
|
if (line[i] === '"') {
|
|
88
98
|
if (i + 1 < line.length && line[i + 1] === '"') {
|
|
@@ -97,13 +107,16 @@ const parseCSVLine = function parseCSVLine(line: string): string[] {
|
|
|
97
107
|
i += 1;
|
|
98
108
|
}
|
|
99
109
|
}
|
|
110
|
+
|
|
100
111
|
fields.push(field);
|
|
112
|
+
|
|
101
113
|
// Skip the comma after a quoted field.
|
|
102
114
|
if (i < line.length && line[i] === ",") {
|
|
103
115
|
i += 1;
|
|
104
116
|
}
|
|
105
117
|
} else {
|
|
106
118
|
const nextComma = line.indexOf(",", i);
|
|
119
|
+
|
|
107
120
|
if (nextComma === -1) {
|
|
108
121
|
fields.push(line.slice(i));
|
|
109
122
|
break;
|
|
@@ -113,46 +126,60 @@ const parseCSVLine = function parseCSVLine(line: string): string[] {
|
|
|
113
126
|
}
|
|
114
127
|
}
|
|
115
128
|
}
|
|
129
|
+
|
|
116
130
|
return fields;
|
|
117
131
|
};
|
|
118
132
|
|
|
119
133
|
export const parseCSV = function parseCSV(input: string): TableData {
|
|
120
134
|
const lines = splitLines(input);
|
|
135
|
+
|
|
121
136
|
if (lines.length === 0) {
|
|
122
137
|
return { columns: [], rows: [] };
|
|
123
138
|
}
|
|
139
|
+
|
|
124
140
|
const columns = createColumnDefs(parseCSVLine(lines[0]));
|
|
125
141
|
const rows = buildRows(columns, lines.slice(1).map(parseCSVLine));
|
|
142
|
+
|
|
126
143
|
return { columns, rows };
|
|
127
144
|
};
|
|
128
145
|
|
|
129
146
|
export const parseTSV = function parseTSV(input: string): TableData {
|
|
130
147
|
const lines = splitLines(input);
|
|
148
|
+
|
|
131
149
|
if (lines.length === 0) {
|
|
132
150
|
return { columns: [], rows: [] };
|
|
133
151
|
}
|
|
152
|
+
|
|
134
153
|
const columns = createColumnDefs(lines[0].split("\t"));
|
|
154
|
+
|
|
135
155
|
const rows = buildRows(
|
|
136
156
|
columns,
|
|
137
157
|
lines.slice(1).map((line) => line.split("\t"))
|
|
138
158
|
);
|
|
159
|
+
|
|
139
160
|
return { columns, rows };
|
|
140
161
|
};
|
|
141
162
|
|
|
142
163
|
export const parseJSON = function parseJSON(input: string): TableData {
|
|
143
164
|
const data = parseJsonDocument(input);
|
|
165
|
+
|
|
144
166
|
if (!isJsonArray(data) || data.length === 0) {
|
|
145
167
|
return { columns: [], rows: [] };
|
|
146
168
|
}
|
|
169
|
+
|
|
147
170
|
const keys = [...new Set(data.flatMap(jsonEntryKeys))];
|
|
148
171
|
const columns: ColumnDef[] = keys.map((key) => ({ header: key, key }));
|
|
172
|
+
|
|
149
173
|
const rows = data.map((item) => {
|
|
150
174
|
const row: TableRow = {};
|
|
175
|
+
|
|
151
176
|
for (const column of columns) {
|
|
152
177
|
row[column.key] = readJsonEntry(item, column.key) ?? "";
|
|
153
178
|
}
|
|
179
|
+
|
|
154
180
|
return row;
|
|
155
181
|
});
|
|
182
|
+
|
|
156
183
|
return { columns, rows };
|
|
157
184
|
};
|
|
158
185
|
|
|
@@ -160,9 +187,11 @@ export type Format = "csv" | "tsv" | "json" | "unknown";
|
|
|
160
187
|
|
|
161
188
|
export const detectFormat = function detectFormat(input: string): Format {
|
|
162
189
|
const trimmed = input.trimStart();
|
|
190
|
+
|
|
163
191
|
if (trimmed.startsWith("[")) {
|
|
164
192
|
try {
|
|
165
193
|
const parsed = parseJsonDocument(trimmed);
|
|
194
|
+
|
|
166
195
|
if (isJsonArray(parsed)) {
|
|
167
196
|
return "json";
|
|
168
197
|
}
|
|
@@ -170,13 +199,17 @@ export const detectFormat = function detectFormat(input: string): Format {
|
|
|
170
199
|
// Not valid JSON — fall through to the delimiter sniffing below.
|
|
171
200
|
}
|
|
172
201
|
}
|
|
202
|
+
|
|
173
203
|
const firstLine = input.split("\n")[0] ?? "";
|
|
204
|
+
|
|
174
205
|
if (firstLine.includes("\t")) {
|
|
175
206
|
return "tsv";
|
|
176
207
|
}
|
|
208
|
+
|
|
177
209
|
if (firstLine.includes(",")) {
|
|
178
210
|
return "csv";
|
|
179
211
|
}
|
|
212
|
+
|
|
180
213
|
return "unknown";
|
|
181
214
|
};
|
|
182
215
|
|
|
@@ -184,28 +217,34 @@ export const parseAuto = function parseAuto(input: string): ParsedTable {
|
|
|
184
217
|
const format = detectFormat(input);
|
|
185
218
|
let columns: ColumnDef[];
|
|
186
219
|
let rows: TableRow[];
|
|
220
|
+
|
|
187
221
|
switch (format) {
|
|
188
222
|
case "csv": {
|
|
189
223
|
({ columns, rows } = parseCSV(input));
|
|
190
224
|
break;
|
|
191
225
|
}
|
|
226
|
+
|
|
192
227
|
case "tsv": {
|
|
193
228
|
({ columns, rows } = parseTSV(input));
|
|
194
229
|
break;
|
|
195
230
|
}
|
|
231
|
+
|
|
196
232
|
case "json": {
|
|
197
233
|
({ columns, rows } = parseJSON(input));
|
|
198
234
|
break;
|
|
199
235
|
}
|
|
236
|
+
|
|
200
237
|
case "unknown": {
|
|
201
238
|
// Fall back to CSV
|
|
202
239
|
({ columns, rows } = parseCSV(input));
|
|
203
240
|
break;
|
|
204
241
|
}
|
|
242
|
+
|
|
205
243
|
default: {
|
|
206
244
|
({ columns, rows } = parseCSV(input));
|
|
207
245
|
break;
|
|
208
246
|
}
|
|
209
247
|
}
|
|
248
|
+
|
|
210
249
|
return { columns, format, rows };
|
|
211
250
|
};
|