@tooee/renderers 0.12.0 → 0.13.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.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.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.map +1 -1
- 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.map +1 -1
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts.map +1 -1
- 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-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js.map +1 -1
- 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.map +1 -1
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts.map +1 -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/dist/text-table-internals.d.ts.map +1 -1
- package/dist/text-table-internals.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 +42 -0
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +13 -0
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +3 -0
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +7 -0
- package/src/markdown/links.ts +26 -0
- package/src/markdown/plain-text.ts +5 -0
- package/src/markdown/table-links.ts +12 -0
- package/src/markdown/tokens.ts +2 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +31 -0
- package/src/markdown-images.ts +21 -0
- package/src/markdown-view.tsx +4 -0
- 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/src/text-table-internals.ts +12 -0
|
@@ -47,6 +47,7 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
const heading = narrowToken(token, "heading");
|
|
50
|
+
|
|
50
51
|
if (heading !== null) {
|
|
51
52
|
return (
|
|
52
53
|
<HeadingRenderer
|
|
@@ -58,7 +59,9 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
58
59
|
/>
|
|
59
60
|
);
|
|
60
61
|
}
|
|
62
|
+
|
|
61
63
|
const paragraph = narrowToken(token, "paragraph");
|
|
64
|
+
|
|
62
65
|
if (paragraph !== null) {
|
|
63
66
|
return (
|
|
64
67
|
<ParagraphRenderer
|
|
@@ -70,7 +73,9 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
70
73
|
/>
|
|
71
74
|
);
|
|
72
75
|
}
|
|
76
|
+
|
|
73
77
|
const code = narrowToken(token, "code");
|
|
78
|
+
|
|
74
79
|
if (code !== null) {
|
|
75
80
|
return (
|
|
76
81
|
<CodeBlock
|
|
@@ -85,7 +90,9 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
85
90
|
/>
|
|
86
91
|
);
|
|
87
92
|
}
|
|
93
|
+
|
|
88
94
|
const blockquote = narrowToken(token, "blockquote");
|
|
95
|
+
|
|
89
96
|
if (blockquote !== null) {
|
|
90
97
|
return (
|
|
91
98
|
<BlockquoteRenderer
|
|
@@ -96,19 +103,25 @@ export const FlatBlockRenderer = function FlatBlockRenderer({
|
|
|
96
103
|
/>
|
|
97
104
|
);
|
|
98
105
|
}
|
|
106
|
+
|
|
99
107
|
const table = narrowToken(token, "table");
|
|
108
|
+
|
|
100
109
|
if (table !== null) {
|
|
101
110
|
return <MarkdownTableRenderer token={table} indent={indent} onLinkActivate={onLinkActivate} />;
|
|
102
111
|
}
|
|
112
|
+
|
|
103
113
|
if (token.type === "hr") {
|
|
104
114
|
return <HorizontalRule theme={theme} indent={indent} />;
|
|
105
115
|
}
|
|
116
|
+
|
|
106
117
|
if (token.type === "space" || token.type === "html") {
|
|
107
118
|
return null;
|
|
108
119
|
}
|
|
120
|
+
|
|
109
121
|
if (!hasMarkedText(token)) {
|
|
110
122
|
return null;
|
|
111
123
|
}
|
|
124
|
+
|
|
112
125
|
return (
|
|
113
126
|
<text
|
|
114
127
|
content={token.text}
|
|
@@ -20,12 +20,15 @@ export const ListLineRenderer = function ListLineRenderer({
|
|
|
20
20
|
}): ReactNode {
|
|
21
21
|
const { token, indent, bullet, checked } = block;
|
|
22
22
|
const checkboxPrefix = checkboxMarker(checked);
|
|
23
|
+
|
|
23
24
|
const inlineTokens: Token[] =
|
|
24
25
|
"tokens" in token && Array.isArray(token.tokens) ? token.tokens : [];
|
|
26
|
+
|
|
25
27
|
const tokenText = hasMarkedText(token) ? token.text : "";
|
|
26
28
|
const hasContent = inlineTokens.length > 0 || tokenText.length > 0;
|
|
27
29
|
|
|
28
30
|
let content: ReactNode = null;
|
|
31
|
+
|
|
29
32
|
if (inlineTokens.length > 0) {
|
|
30
33
|
content = <InlineTokens tokens={inlineTokens} theme={theme} />;
|
|
31
34
|
} else if (tokenText.length > 0) {
|
|
@@ -24,24 +24,31 @@ export const MarkdownTableRenderer = function MarkdownTableRenderer({
|
|
|
24
24
|
const content: TextTableContent = useMemo(() => {
|
|
25
25
|
const headerRow: TextTableCellContent[] = token.header.map((cell) => {
|
|
26
26
|
const chunks = inlineTokensToChunks(cell.tokens, theme);
|
|
27
|
+
|
|
27
28
|
return chunks.length > 0
|
|
28
29
|
? chunks.map((item) => boldChunk(item))
|
|
29
30
|
: [boldChunk(getPlainText(cell.tokens).trim())];
|
|
30
31
|
});
|
|
32
|
+
|
|
31
33
|
const dataRows = token.rows.map((row) =>
|
|
32
34
|
row.map((cell) => {
|
|
33
35
|
const chunks = inlineTokensToChunks(cell.tokens, theme);
|
|
36
|
+
|
|
34
37
|
if (chunks.length > 0) {
|
|
35
38
|
return chunks;
|
|
36
39
|
}
|
|
40
|
+
|
|
37
41
|
const fallbackCell: TextTableCellContent = [
|
|
38
42
|
{ __isChunk: true, text: getPlainText(cell.tokens) },
|
|
39
43
|
];
|
|
44
|
+
|
|
40
45
|
return fallbackCell;
|
|
41
46
|
})
|
|
42
47
|
);
|
|
48
|
+
|
|
43
49
|
return [headerRow, ...dataRows];
|
|
44
50
|
}, [token, theme]);
|
|
51
|
+
|
|
45
52
|
const onMouseDown = useMemo(
|
|
46
53
|
() => tableLinkMouseHandler(content, onLinkActivate),
|
|
47
54
|
[content, onLinkActivate]
|
package/src/markdown/links.ts
CHANGED
|
@@ -23,8 +23,10 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
|
23
23
|
|
|
24
24
|
const advanceText = (value: string, href?: string): void => {
|
|
25
25
|
const parts = value.split("\n");
|
|
26
|
+
|
|
26
27
|
for (const [index, part] of parts.entries()) {
|
|
27
28
|
const width = Bun.stringWidth(part);
|
|
29
|
+
|
|
28
30
|
if (
|
|
29
31
|
href !== undefined &&
|
|
30
32
|
line === position.line &&
|
|
@@ -33,7 +35,9 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
|
33
35
|
) {
|
|
34
36
|
found = href;
|
|
35
37
|
}
|
|
38
|
+
|
|
36
39
|
column += width;
|
|
40
|
+
|
|
37
41
|
if (index < parts.length - 1) {
|
|
38
42
|
line += 1;
|
|
39
43
|
column = 0;
|
|
@@ -46,38 +50,51 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
|
46
50
|
if (found !== null) {
|
|
47
51
|
return;
|
|
48
52
|
}
|
|
53
|
+
|
|
49
54
|
const text = narrowToken(token, "text");
|
|
55
|
+
|
|
50
56
|
if (text !== null) {
|
|
51
57
|
advanceText(normalizeSoftLineEndings(text.text), href);
|
|
52
58
|
continue;
|
|
53
59
|
}
|
|
60
|
+
|
|
54
61
|
const link = narrowToken(token, "link");
|
|
62
|
+
|
|
55
63
|
if (link !== null) {
|
|
56
64
|
visit(link.tokens, link.href);
|
|
57
65
|
continue;
|
|
58
66
|
}
|
|
67
|
+
|
|
59
68
|
const strong = narrowToken(token, "strong");
|
|
69
|
+
|
|
60
70
|
if (strong !== null) {
|
|
61
71
|
visit(strong.tokens, href);
|
|
62
72
|
continue;
|
|
63
73
|
}
|
|
74
|
+
|
|
64
75
|
const em = narrowToken(token, "em");
|
|
76
|
+
|
|
65
77
|
if (em !== null) {
|
|
66
78
|
visit(em.tokens, href);
|
|
67
79
|
continue;
|
|
68
80
|
}
|
|
81
|
+
|
|
69
82
|
const del = narrowToken(token, "del");
|
|
83
|
+
|
|
70
84
|
if (del !== null) {
|
|
71
85
|
advanceText("~", href);
|
|
72
86
|
visit(del.tokens, href);
|
|
73
87
|
advanceText("~", href);
|
|
74
88
|
continue;
|
|
75
89
|
}
|
|
90
|
+
|
|
76
91
|
const codespan = narrowToken(token, "codespan");
|
|
92
|
+
|
|
77
93
|
if (codespan !== null) {
|
|
78
94
|
advanceText(` ${codespan.text} `, href);
|
|
79
95
|
continue;
|
|
80
96
|
}
|
|
97
|
+
|
|
81
98
|
if (narrowToken(token, "br") !== null) {
|
|
82
99
|
advanceText("\n", href);
|
|
83
100
|
} else if (narrowToken(token, "space") !== null) {
|
|
@@ -89,6 +106,7 @@ export const inlineLinkAtPosition = function inlineLinkAtPosition(
|
|
|
89
106
|
};
|
|
90
107
|
|
|
91
108
|
visit(tokens);
|
|
109
|
+
|
|
92
110
|
return found;
|
|
93
111
|
};
|
|
94
112
|
|
|
@@ -100,23 +118,29 @@ export const linkMouseHandler = function linkMouseHandler(
|
|
|
100
118
|
if (onLinkActivate === undefined) {
|
|
101
119
|
return undefined;
|
|
102
120
|
}
|
|
121
|
+
|
|
103
122
|
return (event: MouseEvent): void => {
|
|
104
123
|
if (event.button !== 0) {
|
|
105
124
|
return;
|
|
106
125
|
}
|
|
126
|
+
|
|
107
127
|
const { target } = event;
|
|
128
|
+
|
|
108
129
|
if (target === null || target === undefined || !("lineInfo" in target)) {
|
|
109
130
|
return;
|
|
110
131
|
}
|
|
132
|
+
|
|
111
133
|
// SAFETY: OpenTUI supplies a renderable target, and the lineInfo guard proves this text-buffer member.
|
|
112
134
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- OpenTUI supplies renderable mouse targets
|
|
113
135
|
const text = target as TextBufferRenderable;
|
|
114
136
|
const visualLine = event.y - text.y;
|
|
115
137
|
const sourceLine = text.lineInfo.lineSources[visualLine];
|
|
116
138
|
const startColumn = text.lineInfo.lineStartCols[visualLine];
|
|
139
|
+
|
|
117
140
|
if (sourceLine === undefined || startColumn === undefined) {
|
|
118
141
|
return;
|
|
119
142
|
}
|
|
143
|
+
|
|
120
144
|
const href = inlineLinkAtPosition(
|
|
121
145
|
tokens,
|
|
122
146
|
{
|
|
@@ -125,9 +149,11 @@ export const linkMouseHandler = function linkMouseHandler(
|
|
|
125
149
|
},
|
|
126
150
|
initialColumn
|
|
127
151
|
);
|
|
152
|
+
|
|
128
153
|
if (href === null || onLinkActivate(href) !== true) {
|
|
129
154
|
return;
|
|
130
155
|
}
|
|
156
|
+
|
|
131
157
|
event.preventDefault();
|
|
132
158
|
event.stopPropagation();
|
|
133
159
|
};
|
|
@@ -7,16 +7,21 @@ export const getPlainText = function getPlainText(tokens: readonly Token[]): str
|
|
|
7
7
|
return tokens
|
|
8
8
|
.map((token) => {
|
|
9
9
|
const text = narrowToken(token, "text");
|
|
10
|
+
|
|
10
11
|
if (text !== null) {
|
|
11
12
|
return text.text;
|
|
12
13
|
}
|
|
14
|
+
|
|
13
15
|
const codespan = narrowToken(token, "codespan");
|
|
16
|
+
|
|
14
17
|
if (codespan !== null) {
|
|
15
18
|
return codespan.text;
|
|
16
19
|
}
|
|
20
|
+
|
|
17
21
|
if ("tokens" in token && Array.isArray(token.tokens)) {
|
|
18
22
|
return getPlainText(token.tokens);
|
|
19
23
|
}
|
|
24
|
+
|
|
20
25
|
return hasMarkedText(token) ? token.text : "";
|
|
21
26
|
})
|
|
22
27
|
.join("");
|
|
@@ -11,20 +11,26 @@ export const chunkLinkAt = function chunkLinkAt(
|
|
|
11
11
|
): string | null {
|
|
12
12
|
let currentLine = 0;
|
|
13
13
|
let column = 0;
|
|
14
|
+
|
|
14
15
|
for (const chunk of chunks) {
|
|
15
16
|
const parts = chunk.text.split("\n");
|
|
17
|
+
|
|
16
18
|
for (const [index, part] of parts.entries()) {
|
|
17
19
|
const width = Bun.stringWidth(part);
|
|
20
|
+
|
|
18
21
|
if (currentLine === line && offset >= column && offset < column + width) {
|
|
19
22
|
return chunk.link?.url ?? null;
|
|
20
23
|
}
|
|
24
|
+
|
|
21
25
|
column += width;
|
|
26
|
+
|
|
22
27
|
if (index < parts.length - 1) {
|
|
23
28
|
currentLine += 1;
|
|
24
29
|
column = 0;
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
}
|
|
33
|
+
|
|
28
34
|
return null;
|
|
29
35
|
};
|
|
30
36
|
|
|
@@ -39,19 +45,25 @@ export const tableLinkMouseHandler = function tableLinkMouseHandler(
|
|
|
39
45
|
if (onLinkActivate === undefined) {
|
|
40
46
|
return undefined;
|
|
41
47
|
}
|
|
48
|
+
|
|
42
49
|
return (event: MouseEvent): void => {
|
|
43
50
|
if (event.button !== 0 || event.target === null) {
|
|
44
51
|
return;
|
|
45
52
|
}
|
|
53
|
+
|
|
46
54
|
const hit = textTableCellAt(event.target, event.x, event.y);
|
|
47
55
|
const chunks = hit === null ? null : content[hit.row]?.[hit.column];
|
|
56
|
+
|
|
48
57
|
if (hit === null || chunks === null || chunks === undefined) {
|
|
49
58
|
return;
|
|
50
59
|
}
|
|
60
|
+
|
|
51
61
|
const href = chunkLinkAt(chunks, hit.line, hit.offset);
|
|
62
|
+
|
|
52
63
|
if (href === null || onLinkActivate(href) !== true) {
|
|
53
64
|
return;
|
|
54
65
|
}
|
|
66
|
+
|
|
55
67
|
event.preventDefault();
|
|
56
68
|
event.stopPropagation();
|
|
57
69
|
};
|
package/src/markdown/tokens.ts
CHANGED
|
@@ -33,6 +33,7 @@ export const narrowToken = function narrowToken<Type extends keyof KnownTokenByT
|
|
|
33
33
|
if (token.type !== type) {
|
|
34
34
|
return null;
|
|
35
35
|
}
|
|
36
|
+
|
|
36
37
|
// SAFETY: Marked creates the token member selected by the checked type discriminator.
|
|
37
38
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the Generic fallback prevents discriminator narrowing
|
|
38
39
|
return token as KnownTokenByType[Type];
|
|
@@ -43,6 +44,7 @@ export const hasMarkedText = function hasMarkedText(token: Token): token is Toke
|
|
|
43
44
|
if (!("text" in token)) {
|
|
44
45
|
return false;
|
|
45
46
|
}
|
|
47
|
+
|
|
46
48
|
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- primitive check stays at the Marked token boundary
|
|
47
49
|
return typeof token.text === "string";
|
|
48
50
|
};
|
|
@@ -27,55 +27,75 @@ export const visitInline = function visitInline<Result>(
|
|
|
27
27
|
for (const [index, token] of tokens.entries()) {
|
|
28
28
|
const key = `${keyPrefix}${index}`;
|
|
29
29
|
const text = narrowToken(token, "text");
|
|
30
|
+
|
|
30
31
|
if (text !== null) {
|
|
31
32
|
result.push(...sink.text(text, key));
|
|
32
33
|
continue;
|
|
33
34
|
}
|
|
35
|
+
|
|
34
36
|
const strong = narrowToken(token, "strong");
|
|
37
|
+
|
|
35
38
|
if (strong !== null) {
|
|
36
39
|
result.push(...sink.strong(strong, visitInline(strong.tokens, sink, `${key}.`), key));
|
|
37
40
|
continue;
|
|
38
41
|
}
|
|
42
|
+
|
|
39
43
|
const em = narrowToken(token, "em");
|
|
44
|
+
|
|
40
45
|
if (em !== null) {
|
|
41
46
|
result.push(...sink.em(em, visitInline(em.tokens, sink, `${key}.`), key));
|
|
42
47
|
continue;
|
|
43
48
|
}
|
|
49
|
+
|
|
44
50
|
const codespan = narrowToken(token, "codespan");
|
|
51
|
+
|
|
45
52
|
if (codespan !== null) {
|
|
46
53
|
result.push(...sink.codespan(codespan, key));
|
|
47
54
|
continue;
|
|
48
55
|
}
|
|
56
|
+
|
|
49
57
|
const link = narrowToken(token, "link");
|
|
58
|
+
|
|
50
59
|
if (link !== null) {
|
|
51
60
|
result.push(...sink.link(link, visitInline(link.tokens, sink, `${key}.`), key));
|
|
52
61
|
continue;
|
|
53
62
|
}
|
|
63
|
+
|
|
54
64
|
const del = narrowToken(token, "del");
|
|
65
|
+
|
|
55
66
|
if (del !== null) {
|
|
56
67
|
result.push(...sink.del(del, visitInline(del.tokens, sink, `${key}.`), key));
|
|
57
68
|
continue;
|
|
58
69
|
}
|
|
70
|
+
|
|
59
71
|
const image = narrowToken(token, "image");
|
|
72
|
+
|
|
60
73
|
if (image !== null) {
|
|
61
74
|
result.push(...sink.image(image, key));
|
|
62
75
|
continue;
|
|
63
76
|
}
|
|
77
|
+
|
|
64
78
|
const br = narrowToken(token, "br");
|
|
79
|
+
|
|
65
80
|
if (br !== null) {
|
|
66
81
|
result.push(...sink.br(br, key));
|
|
67
82
|
continue;
|
|
68
83
|
}
|
|
84
|
+
|
|
69
85
|
const escape = narrowToken(token, "escape");
|
|
86
|
+
|
|
70
87
|
if (escape !== null) {
|
|
71
88
|
result.push(...sink.escape(escape, key));
|
|
72
89
|
continue;
|
|
73
90
|
}
|
|
91
|
+
|
|
74
92
|
const space = narrowToken(token, "space");
|
|
93
|
+
|
|
75
94
|
if (space !== null) {
|
|
76
95
|
result.push(...sink.space(space, key));
|
|
77
96
|
continue;
|
|
78
97
|
}
|
|
98
|
+
|
|
79
99
|
result.push(...sink.fallback(token, key));
|
|
80
100
|
}
|
|
81
101
|
|
package/src/markdown-blocks.ts
CHANGED
|
@@ -45,6 +45,7 @@ export const checkboxMarker = function checkboxMarker(checked: boolean | undefin
|
|
|
45
45
|
if (checked === undefined) {
|
|
46
46
|
return "";
|
|
47
47
|
}
|
|
48
|
+
|
|
48
49
|
return checked ? "[x] " : "[ ] ";
|
|
49
50
|
};
|
|
50
51
|
|
|
@@ -56,12 +57,15 @@ export const checkboxMarker = function checkboxMarker(checked: boolean | undefin
|
|
|
56
57
|
*/
|
|
57
58
|
export const getFlatBlockText = function getFlatBlockText(block: FlatBlock): string {
|
|
58
59
|
const { raw } = block.token;
|
|
60
|
+
|
|
59
61
|
if (raw.length > 0) {
|
|
60
62
|
return raw;
|
|
61
63
|
}
|
|
64
|
+
|
|
62
65
|
if (block.bullet !== undefined) {
|
|
63
66
|
return block.bullet + checkboxMarker(block.checked);
|
|
64
67
|
}
|
|
68
|
+
|
|
65
69
|
return block.source?.primary.text ?? "";
|
|
66
70
|
};
|
|
67
71
|
|
|
@@ -89,10 +93,13 @@ class MarkdownResolver {
|
|
|
89
93
|
/** Resolve `raw` at/after the cursor within `[cursor, bound]`, then advance. */
|
|
90
94
|
resolveRaw(raw: string, bound: number): DocumentRowSource | null {
|
|
91
95
|
const match = this.findRaw(raw, bound);
|
|
96
|
+
|
|
92
97
|
if (!match) {
|
|
93
98
|
return null;
|
|
94
99
|
}
|
|
100
|
+
|
|
95
101
|
this.cursor = match.end;
|
|
102
|
+
|
|
96
103
|
return { primary: this.index.span(match.start, match.end) };
|
|
97
104
|
}
|
|
98
105
|
|
|
@@ -112,9 +119,11 @@ class MarkdownResolver {
|
|
|
112
119
|
if (raw.length === 0) {
|
|
113
120
|
return null;
|
|
114
121
|
}
|
|
122
|
+
|
|
115
123
|
const md = this.markdown;
|
|
116
124
|
|
|
117
125
|
const exact = md.indexOf(raw, this.cursor);
|
|
126
|
+
|
|
118
127
|
if (exact !== -1 && exact + raw.length <= bound) {
|
|
119
128
|
return { end: exact + raw.length, start: exact };
|
|
120
129
|
}
|
|
@@ -124,20 +133,26 @@ class MarkdownResolver {
|
|
|
124
133
|
}
|
|
125
134
|
|
|
126
135
|
const firstSegment = raw.slice(0, raw.indexOf("\n"));
|
|
136
|
+
|
|
127
137
|
if (firstSegment.length === 0) {
|
|
128
138
|
return null;
|
|
129
139
|
}
|
|
130
140
|
|
|
131
141
|
let from = this.cursor;
|
|
142
|
+
|
|
132
143
|
for (;;) {
|
|
133
144
|
const start = md.indexOf(firstSegment, from);
|
|
145
|
+
|
|
134
146
|
if (start === -1 || start >= bound) {
|
|
135
147
|
return null;
|
|
136
148
|
}
|
|
149
|
+
|
|
137
150
|
const end = this.matchFlexibleNewlines(raw, start, bound);
|
|
151
|
+
|
|
138
152
|
if (end !== -1) {
|
|
139
153
|
return { end, start };
|
|
140
154
|
}
|
|
155
|
+
|
|
141
156
|
from = start + 1;
|
|
142
157
|
}
|
|
143
158
|
}
|
|
@@ -148,8 +163,10 @@ class MarkdownResolver {
|
|
|
148
163
|
const { length } = md;
|
|
149
164
|
let h = start;
|
|
150
165
|
let n = 0;
|
|
166
|
+
|
|
151
167
|
while (n < raw.length) {
|
|
152
168
|
const code = raw.codePointAt(n);
|
|
169
|
+
|
|
153
170
|
if (code === LINE_FEED_CODE) {
|
|
154
171
|
if (h + 1 < length && md.codePointAt(h) === 13 && md.codePointAt(h + 1) === 10) {
|
|
155
172
|
h += 2;
|
|
@@ -158,6 +175,7 @@ class MarkdownResolver {
|
|
|
158
175
|
} else {
|
|
159
176
|
return -1;
|
|
160
177
|
}
|
|
178
|
+
|
|
161
179
|
n += 1;
|
|
162
180
|
} else if (h < length && md.codePointAt(h) === code) {
|
|
163
181
|
h += 1;
|
|
@@ -166,6 +184,7 @@ class MarkdownResolver {
|
|
|
166
184
|
return -1;
|
|
167
185
|
}
|
|
168
186
|
}
|
|
187
|
+
|
|
169
188
|
return h <= bound ? h : -1;
|
|
170
189
|
}
|
|
171
190
|
|
|
@@ -175,19 +194,25 @@ class MarkdownResolver {
|
|
|
175
194
|
const lineEnd = nl === -1 ? itemEnd : Math.min(nl, itemEnd);
|
|
176
195
|
const firstLine = this.markdown.slice(itemStart, lineEnd);
|
|
177
196
|
const match = LIST_MARKER.exec(firstLine);
|
|
197
|
+
|
|
178
198
|
if (!match) {
|
|
179
199
|
return { primary: this.index.span(itemStart, itemStart, false) };
|
|
180
200
|
}
|
|
201
|
+
|
|
181
202
|
const markerStart = itemStart + (match.groups?.indent ?? "").length;
|
|
182
203
|
let spanEnd = itemStart + match[0].length;
|
|
204
|
+
|
|
183
205
|
while (spanEnd > markerStart) {
|
|
184
206
|
const code = this.markdown.codePointAt(spanEnd - 1);
|
|
207
|
+
|
|
185
208
|
// Continue while the character is a space or tab.
|
|
186
209
|
if (code !== 32 && code !== 9) {
|
|
187
210
|
break;
|
|
188
211
|
}
|
|
212
|
+
|
|
189
213
|
spanEnd -= 1;
|
|
190
214
|
}
|
|
215
|
+
|
|
191
216
|
return { primary: this.index.span(markerStart, spanEnd, false) };
|
|
192
217
|
}
|
|
193
218
|
}
|
|
@@ -215,8 +240,10 @@ const flattenList = function flattenList(
|
|
|
215
240
|
|
|
216
241
|
let itemStart: number | null = null;
|
|
217
242
|
let itemEnd = bound;
|
|
243
|
+
|
|
218
244
|
if (res) {
|
|
219
245
|
const loc = res.locate(item.raw, bound);
|
|
246
|
+
|
|
220
247
|
if (loc) {
|
|
221
248
|
itemStart = loc.start;
|
|
222
249
|
itemEnd = loc.end;
|
|
@@ -276,6 +303,7 @@ const flattenListItem = function flattenListItem(
|
|
|
276
303
|
if (!bulletUsed) {
|
|
277
304
|
emitBulletMarker();
|
|
278
305
|
}
|
|
306
|
+
|
|
279
307
|
// SAFETY: Marked creates a List token for the adjacent token.type branch.
|
|
280
308
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
|
|
281
309
|
flattenList(token as Tokens.List, indent + bullet.length, out, res, itemEnd);
|
|
@@ -284,6 +312,7 @@ const flattenListItem = function flattenListItem(
|
|
|
284
312
|
if (!bulletUsed) {
|
|
285
313
|
emitBulletMarker();
|
|
286
314
|
}
|
|
315
|
+
|
|
287
316
|
out.push({
|
|
288
317
|
indent: indent + bullet.length,
|
|
289
318
|
source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
|
|
@@ -309,6 +338,7 @@ const flattenWalk = function flattenWalk(
|
|
|
309
338
|
if (token.type === "space") {
|
|
310
339
|
continue;
|
|
311
340
|
}
|
|
341
|
+
|
|
312
342
|
if (token.type === "list") {
|
|
313
343
|
// SAFETY: Marked creates a List token for the adjacent token.type branch.
|
|
314
344
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
|
|
@@ -337,5 +367,6 @@ export const flattenMarkdown = function flattenMarkdown(
|
|
|
337
367
|
const res = new MarkdownResolver(markdown, options?.sourceId);
|
|
338
368
|
const out: FlatBlock[] = [];
|
|
339
369
|
flattenWalk(marked.lexer(markdown), 0, out, res, markdown.length);
|
|
370
|
+
|
|
340
371
|
return out.filter((block) => block.token.type !== "html");
|
|
341
372
|
};
|
package/src/markdown-images.ts
CHANGED
|
@@ -3,7 +3,9 @@ import path from "node:path";
|
|
|
3
3
|
import type { Token, Tokens } from "marked";
|
|
4
4
|
|
|
5
5
|
const OBSIDIAN_IMAGE_EMBED = /!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]/gu;
|
|
6
|
+
|
|
6
7
|
const IMAGE_DIMENSIONS = /^(?<width>\d+)(?:x(?<height>\d+))?$/u;
|
|
8
|
+
|
|
7
9
|
const URL_SCHEME = /^[a-z][a-z\d+.-]*:/iu;
|
|
8
10
|
|
|
9
11
|
export interface MarkdownImageEmbed {
|
|
@@ -23,6 +25,7 @@ const obsidianEmbed = function obsidianEmbed(
|
|
|
23
25
|
): MarkdownImageEmbed {
|
|
24
26
|
const source = target.trim();
|
|
25
27
|
const dimensions = display?.trim().match(IMAGE_DIMENSIONS);
|
|
28
|
+
|
|
26
29
|
if (dimensions?.groups?.width !== undefined) {
|
|
27
30
|
return {
|
|
28
31
|
height: dimensions.groups.height === undefined ? undefined : Number(dimensions.groups.height),
|
|
@@ -30,7 +33,9 @@ const obsidianEmbed = function obsidianEmbed(
|
|
|
30
33
|
width: Number(dimensions.groups.width),
|
|
31
34
|
};
|
|
32
35
|
}
|
|
36
|
+
|
|
33
37
|
const alt = display?.trim();
|
|
38
|
+
|
|
34
39
|
return { alt: alt === undefined || alt === "" ? undefined : alt, source };
|
|
35
40
|
};
|
|
36
41
|
|
|
@@ -39,17 +44,21 @@ export const parseObsidianImageEmbed = function parseObsidianImageEmbed(
|
|
|
39
44
|
value: string
|
|
40
45
|
): MarkdownImageEmbed | null {
|
|
41
46
|
const match = /^!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]$/u.exec(value.trim());
|
|
47
|
+
|
|
42
48
|
if (match?.groups?.target === undefined) {
|
|
43
49
|
return null;
|
|
44
50
|
}
|
|
51
|
+
|
|
45
52
|
return obsidianEmbed(match.groups.target, match.groups.display);
|
|
46
53
|
};
|
|
47
54
|
|
|
48
55
|
const splitTextToken = function splitTextToken(text: string): MarkdownInlineSegment[] {
|
|
49
56
|
const segments: MarkdownInlineSegment[] = [];
|
|
50
57
|
let cursor = 0;
|
|
58
|
+
|
|
51
59
|
for (const match of text.matchAll(OBSIDIAN_IMAGE_EMBED)) {
|
|
52
60
|
const { index } = match;
|
|
61
|
+
|
|
53
62
|
if (index > cursor) {
|
|
54
63
|
segments.push({
|
|
55
64
|
tokens: [
|
|
@@ -62,18 +71,23 @@ const splitTextToken = function splitTextToken(text: string): MarkdownInlineSegm
|
|
|
62
71
|
type: "text",
|
|
63
72
|
});
|
|
64
73
|
}
|
|
74
|
+
|
|
65
75
|
const target = match.groups?.target;
|
|
76
|
+
|
|
66
77
|
if (target !== undefined) {
|
|
67
78
|
segments.push({ ...obsidianEmbed(target, match.groups?.display), type: "image" });
|
|
68
79
|
}
|
|
80
|
+
|
|
69
81
|
cursor = index + match[0].length;
|
|
70
82
|
}
|
|
83
|
+
|
|
71
84
|
if (cursor < text.length) {
|
|
72
85
|
segments.push({
|
|
73
86
|
tokens: [{ raw: text.slice(cursor), text: text.slice(cursor), type: "text" }],
|
|
74
87
|
type: "text",
|
|
75
88
|
});
|
|
76
89
|
}
|
|
90
|
+
|
|
77
91
|
return segments.length > 0
|
|
78
92
|
? segments
|
|
79
93
|
: [{ tokens: [{ raw: text, text, type: "text" }], type: "text" }];
|
|
@@ -106,11 +120,13 @@ export const splitMarkdownImages = function splitMarkdownImages(
|
|
|
106
120
|
});
|
|
107
121
|
continue;
|
|
108
122
|
}
|
|
123
|
+
|
|
109
124
|
if (token.type === "text") {
|
|
110
125
|
// SAFETY: Marked creates a Text token for the adjacent token.type branch.
|
|
111
126
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
|
|
112
127
|
const textToken = token as Tokens.Text;
|
|
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
|
};
|