@tooee/renderers 0.7.3 → 0.9.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.
Files changed (171) hide show
  1. package/dist/chooser/choose-filter.d.ts +13 -0
  2. package/dist/chooser/choose-filter.d.ts.map +1 -0
  3. package/dist/chooser/choose-filter.js +22 -0
  4. package/dist/chooser/choose-filter.js.map +1 -0
  5. package/dist/chooser/choose-highlighted-text.d.ts +9 -0
  6. package/dist/chooser/choose-highlighted-text.d.ts.map +1 -0
  7. package/dist/chooser/choose-highlighted-text.js +32 -0
  8. package/dist/chooser/choose-highlighted-text.js.map +1 -0
  9. package/dist/chooser/choose-list.d.ts +29 -0
  10. package/dist/chooser/choose-list.d.ts.map +1 -0
  11. package/dist/chooser/choose-list.js +66 -0
  12. package/dist/chooser/choose-list.js.map +1 -0
  13. package/dist/chooser/choose-store.d.ts +58 -0
  14. package/dist/chooser/choose-store.d.ts.map +1 -0
  15. package/dist/chooser/choose-store.js +102 -0
  16. package/dist/chooser/choose-store.js.map +1 -0
  17. package/dist/chooser/chooser.d.ts +18 -0
  18. package/dist/chooser/chooser.d.ts.map +1 -0
  19. package/dist/chooser/chooser.js +25 -0
  20. package/dist/chooser/chooser.js.map +1 -0
  21. package/dist/chooser/fuzzy.d.ts +9 -0
  22. package/dist/chooser/fuzzy.d.ts.map +1 -0
  23. package/dist/chooser/fuzzy.js +5 -0
  24. package/dist/chooser/fuzzy.js.map +1 -0
  25. package/dist/chooser/source.d.ts +5 -0
  26. package/dist/chooser/source.d.ts.map +1 -0
  27. package/dist/chooser/source.js +26 -0
  28. package/dist/chooser/source.js.map +1 -0
  29. package/dist/chooser/types.d.ts +22 -0
  30. package/dist/chooser/types.d.ts.map +1 -0
  31. package/dist/chooser/types.js +2 -0
  32. package/dist/chooser/types.js.map +1 -0
  33. package/dist/chooser/use-choose.d.ts +84 -0
  34. package/dist/chooser/use-choose.d.ts.map +1 -0
  35. package/dist/chooser/use-choose.js +382 -0
  36. package/dist/chooser/use-choose.js.map +1 -0
  37. package/dist/code-blocks.d.ts +2 -1
  38. package/dist/code-blocks.d.ts.map +1 -1
  39. package/dist/code-blocks.js +20 -18
  40. package/dist/code-blocks.js.map +1 -1
  41. package/dist/code-view.d.ts +2 -1
  42. package/dist/code-view.d.ts.map +1 -1
  43. package/dist/code-view.js.map +1 -1
  44. package/dist/command-palette.d.ts +2 -1
  45. package/dist/command-palette.d.ts.map +1 -1
  46. package/dist/command-palette.js +15 -76
  47. package/dist/command-palette.js.map +1 -1
  48. package/dist/context-menu.d.ts +2 -1
  49. package/dist/context-menu.d.ts.map +1 -1
  50. package/dist/context-menu.js +50 -15
  51. package/dist/context-menu.js.map +1 -1
  52. package/dist/index.d.ts +19 -3
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +9 -1
  55. package/dist/index.js.map +1 -1
  56. package/dist/markdown/blocks/blockquote.d.ts +11 -0
  57. package/dist/markdown/blocks/blockquote.d.ts.map +1 -0
  58. package/dist/markdown/blocks/blockquote.js +26 -0
  59. package/dist/markdown/blocks/blockquote.js.map +1 -0
  60. package/dist/markdown/blocks/flat-block.d.ts +20 -0
  61. package/dist/markdown/blocks/flat-block.d.ts.map +1 -0
  62. package/dist/markdown/blocks/flat-block.js +52 -0
  63. package/dist/markdown/blocks/flat-block.js.map +1 -0
  64. package/dist/markdown/blocks/heading.d.ts +11 -0
  65. package/dist/markdown/blocks/heading.d.ts.map +1 -0
  66. package/dist/markdown/blocks/heading.js +23 -0
  67. package/dist/markdown/blocks/heading.js.map +1 -0
  68. package/dist/markdown/blocks/horizontal-rule.d.ts +7 -0
  69. package/dist/markdown/blocks/horizontal-rule.d.ts.map +1 -0
  70. package/dist/markdown/blocks/horizontal-rule.js +5 -0
  71. package/dist/markdown/blocks/horizontal-rule.js.map +1 -0
  72. package/dist/markdown/blocks/list-line.d.ts +10 -0
  73. package/dist/markdown/blocks/list-line.d.ts.map +1 -0
  74. package/dist/markdown/blocks/list-line.js +21 -0
  75. package/dist/markdown/blocks/list-line.js.map +1 -0
  76. package/dist/markdown/blocks/paragraph.d.ts +12 -0
  77. package/dist/markdown/blocks/paragraph.d.ts.map +1 -0
  78. package/dist/markdown/blocks/paragraph.js +27 -0
  79. package/dist/markdown/blocks/paragraph.js.map +1 -0
  80. package/dist/markdown/blocks/table.d.ts +7 -0
  81. package/dist/markdown/blocks/table.d.ts.map +1 -0
  82. package/dist/markdown/blocks/table.js +30 -0
  83. package/dist/markdown/blocks/table.js.map +1 -0
  84. package/dist/markdown/chunks.d.ts +5 -0
  85. package/dist/markdown/chunks.d.ts.map +1 -0
  86. package/dist/markdown/chunks.js +29 -0
  87. package/dist/markdown/chunks.js.map +1 -0
  88. package/dist/markdown/inline.d.ts +8 -0
  89. package/dist/markdown/inline.d.ts.map +1 -0
  90. package/dist/markdown/inline.js +27 -0
  91. package/dist/markdown/inline.js.map +1 -0
  92. package/dist/markdown/links.d.ts +11 -0
  93. package/dist/markdown/links.d.ts.map +1 -0
  94. package/dist/markdown/links.js +107 -0
  95. package/dist/markdown/links.js.map +1 -0
  96. package/dist/markdown/plain-text.d.ts +4 -0
  97. package/dist/markdown/plain-text.d.ts.map +1 -0
  98. package/dist/markdown/plain-text.js +21 -0
  99. package/dist/markdown/plain-text.js.map +1 -0
  100. package/dist/markdown/tokens.d.ts +32 -0
  101. package/dist/markdown/tokens.d.ts.map +1 -0
  102. package/dist/markdown/tokens.js +25 -0
  103. package/dist/markdown/tokens.js.map +1 -0
  104. package/dist/markdown/visit-inline.d.ts +17 -0
  105. package/dist/markdown/visit-inline.d.ts.map +1 -0
  106. package/dist/markdown/visit-inline.js +61 -0
  107. package/dist/markdown/visit-inline.js.map +1 -0
  108. package/dist/markdown-blocks.d.ts.map +1 -1
  109. package/dist/markdown-blocks.js +6 -4
  110. package/dist/markdown-blocks.js.map +1 -1
  111. package/dist/markdown-images.d.ts.map +1 -1
  112. package/dist/markdown-images.js +14 -5
  113. package/dist/markdown-images.js.map +1 -1
  114. package/dist/markdown-view.d.ts +10 -32
  115. package/dist/markdown-view.d.ts.map +1 -1
  116. package/dist/markdown-view.js +9 -468
  117. package/dist/markdown-view.js.map +1 -1
  118. package/dist/mermaid.d.ts +5 -4
  119. package/dist/mermaid.d.ts.map +1 -1
  120. package/dist/mermaid.js +5 -5
  121. package/dist/mermaid.js.map +1 -1
  122. package/dist/parsers.d.ts +6 -13
  123. package/dist/parsers.d.ts.map +1 -1
  124. package/dist/parsers.js +66 -46
  125. package/dist/parsers.js.map +1 -1
  126. package/dist/row-document-renderable.d.ts +7 -6
  127. package/dist/row-document-renderable.d.ts.map +1 -1
  128. package/dist/row-document-renderable.js +2 -7
  129. package/dist/row-document-renderable.js.map +1 -1
  130. package/dist/table-types.d.ts +4 -0
  131. package/dist/table-types.d.ts.map +1 -1
  132. package/dist/table.d.ts +4 -1
  133. package/dist/table.d.ts.map +1 -1
  134. package/dist/table.js +30 -23
  135. package/dist/table.js.map +1 -1
  136. package/package.json +9 -5
  137. package/src/chooser/choose-filter.tsx +66 -0
  138. package/src/chooser/choose-highlighted-text.tsx +49 -0
  139. package/src/chooser/choose-list.tsx +159 -0
  140. package/src/chooser/choose-store.ts +147 -0
  141. package/src/chooser/chooser.tsx +80 -0
  142. package/src/chooser/fuzzy.ts +13 -0
  143. package/src/chooser/source.ts +36 -0
  144. package/src/chooser/types.ts +28 -0
  145. package/src/chooser/use-choose.ts +540 -0
  146. package/src/code-blocks.tsx +28 -26
  147. package/src/code-view.tsx +2 -1
  148. package/src/command-palette.tsx +49 -137
  149. package/src/context-menu.tsx +54 -15
  150. package/src/index.ts +46 -3
  151. package/src/markdown/blocks/blockquote.tsx +53 -0
  152. package/src/markdown/blocks/flat-block.tsx +119 -0
  153. package/src/markdown/blocks/heading.tsx +53 -0
  154. package/src/markdown/blocks/horizontal-rule.tsx +16 -0
  155. package/src/markdown/blocks/list-line.tsx +52 -0
  156. package/src/markdown/blocks/paragraph.tsx +90 -0
  157. package/src/markdown/blocks/table.tsx +55 -0
  158. package/src/markdown/chunks.ts +42 -0
  159. package/src/markdown/inline.tsx +45 -0
  160. package/src/markdown/links.ts +133 -0
  161. package/src/markdown/plain-text.ts +22 -0
  162. package/src/markdown/tokens.ts +53 -0
  163. package/src/markdown/visit-inline.ts +82 -0
  164. package/src/markdown-blocks.ts +6 -4
  165. package/src/markdown-images.ts +15 -6
  166. package/src/markdown-view.tsx +20 -845
  167. package/src/mermaid.ts +11 -9
  168. package/src/parsers.ts +86 -62
  169. package/src/row-document-renderable.ts +13 -15
  170. package/src/table-types.ts +5 -0
  171. package/src/table.tsx +36 -30
@@ -0,0 +1,90 @@
1
+ import type { Tokens } from "marked";
2
+ import { useEffect, useState } from "react";
3
+ import type { ReactNode } from "react";
4
+ import type { ResolvedTheme } from "@tooee/themes";
5
+ import { resolveMarkdownImageSource, splitMarkdownImages } from "../../markdown-images.js";
6
+ import type { MarkdownImageEmbed } from "../../markdown-images.js";
7
+ import { InlineTokens } from "../inline.js";
8
+ import { linkMouseHandler } from "../links.js";
9
+ import type { MarkdownLinkHandler } from "../links.js";
10
+
11
+ const MarkdownImage = function MarkdownImage({
12
+ image,
13
+ basePath,
14
+ theme,
15
+ }: {
16
+ image: MarkdownImageEmbed;
17
+ basePath?: string;
18
+ theme: ResolvedTheme;
19
+ }): ReactNode {
20
+ const source = resolveMarkdownImageSource(image.source, basePath);
21
+ const [state, setState] = useState<"loading" | "loaded" | "error">("loading");
22
+
23
+ useEffect(() => {
24
+ setState("loading");
25
+ }, [source]);
26
+
27
+ return (
28
+ <box style={{ flexDirection: "column" }}>
29
+ {state === "loading" && (
30
+ <text content={`Loading image: ${image.alt ?? image.source}`} fg={theme.textMuted} />
31
+ )}
32
+ {state === "error" && (
33
+ <text content={`Image failed to load: ${image.alt ?? image.source}`} fg={theme.error} />
34
+ )}
35
+ <image
36
+ source={source}
37
+ fit="fit"
38
+ protocol="auto"
39
+ onLoad={() => {
40
+ setState("loaded");
41
+ }}
42
+ onError={() => {
43
+ setState("error");
44
+ }}
45
+ style={{ height: image.height ?? 12, width: image.width ?? "100%" }}
46
+ />
47
+ </box>
48
+ );
49
+ };
50
+
51
+ export const ParagraphRenderer = function ParagraphRenderer({
52
+ token,
53
+ theme,
54
+ indent,
55
+ onLinkActivate,
56
+ imageBasePath,
57
+ }: {
58
+ token: Tokens.Paragraph;
59
+ theme: ResolvedTheme;
60
+ indent: number;
61
+ onLinkActivate?: MarkdownLinkHandler;
62
+ imageBasePath?: string;
63
+ }): ReactNode {
64
+ const segments = splitMarkdownImages(token.tokens);
65
+ return (
66
+ <box
67
+ style={{
68
+ flexDirection: "column",
69
+ marginBottom: 1,
70
+ marginLeft: 1 + indent,
71
+ marginRight: 1,
72
+ }}
73
+ >
74
+ {segments.map(
75
+ (segment, index): ReactNode =>
76
+ segment.type === "image" ? (
77
+ <MarkdownImage key={index} image={segment} basePath={imageBasePath} theme={theme} />
78
+ ) : (
79
+ <text
80
+ key={index}
81
+ style={{ fg: theme.markdownText }}
82
+ onMouseDown={linkMouseHandler(segment.tokens, onLinkActivate)}
83
+ >
84
+ <InlineTokens tokens={segment.tokens} theme={theme} />
85
+ </text>
86
+ ),
87
+ )}
88
+ </box>
89
+ );
90
+ };
@@ -0,0 +1,55 @@
1
+ import type { Tokens } from "marked";
2
+ import { useMemo } from "react";
3
+ import type { ReactNode } from "react";
4
+ import { bold as boldChunk } from "@opentui/core";
5
+ import type { TextTableCellContent, TextTableContent } from "@opentui/core";
6
+ import { useTheme } from "@tooee/themes";
7
+ import { inlineTokensToChunks } from "../chunks.js";
8
+ import { getPlainText } from "../plain-text.js";
9
+
10
+ export const MarkdownTableRenderer = function MarkdownTableRenderer({
11
+ token,
12
+ indent,
13
+ }: {
14
+ token: Tokens.Table;
15
+ indent: number;
16
+ }): ReactNode {
17
+ const { theme } = useTheme();
18
+
19
+ const content: TextTableContent = useMemo(() => {
20
+ const headerRow: TextTableCellContent[] = token.header.map((cell) => {
21
+ const chunks = inlineTokensToChunks(cell.tokens, theme);
22
+ return chunks.length > 0
23
+ ? chunks.map((item) => boldChunk(item))
24
+ : [boldChunk(getPlainText(cell.tokens).trim())];
25
+ });
26
+ const dataRows = token.rows.map((row) =>
27
+ row.map((cell) => {
28
+ const chunks = inlineTokensToChunks(cell.tokens, theme);
29
+ if (chunks.length > 0) {
30
+ return chunks;
31
+ }
32
+ const fallbackCell: TextTableCellContent = [
33
+ { __isChunk: true, text: getPlainText(cell.tokens) },
34
+ ];
35
+ return fallbackCell;
36
+ }),
37
+ );
38
+ return [headerRow, ...dataRows];
39
+ }, [token, theme]);
40
+
41
+ return (
42
+ <box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1 }}>
43
+ <text-table
44
+ content={content}
45
+ wrapMode="word"
46
+ columnWidthMode="content"
47
+ cellPadding={0}
48
+ border={true}
49
+ borderStyle="single"
50
+ borderColor={theme.border}
51
+ fg={theme.text}
52
+ />
53
+ </box>
54
+ );
55
+ };
@@ -0,0 +1,42 @@
1
+ import type { Token } from "marked";
2
+ import {
3
+ bold as boldChunk,
4
+ italic as italicChunk,
5
+ parseColor,
6
+ underline as underlineChunk,
7
+ } from "@opentui/core";
8
+ import type { TextChunk } from "@opentui/core";
9
+ import type { ResolvedTheme } from "@tooee/themes";
10
+ import { hasMarkedText } from "./tokens.js";
11
+ import { visitInline } from "./visit-inline.js";
12
+
13
+ const chunk = function chunk(text: string): TextChunk {
14
+ return { __isChunk: true, text };
15
+ };
16
+
17
+ export const inlineTokensToChunks = function inlineTokensToChunks(
18
+ tokens: readonly Token[],
19
+ theme: ResolvedTheme,
20
+ ): TextChunk[] {
21
+ return visitInline<TextChunk>(tokens, {
22
+ br: () => [],
23
+ codespan: (token) => [
24
+ {
25
+ __isChunk: true,
26
+ bg: parseColor(theme.backgroundPanel),
27
+ fg: parseColor(theme.markdownCode),
28
+ text: ` ${token.text} `,
29
+ },
30
+ ],
31
+ del: (token) => [chunk(token.text)],
32
+ em: (_token, children) => children.map((child) => italicChunk(child)),
33
+ escape: (token) => [chunk(token.text)],
34
+ fallback: (token) => (hasMarkedText(token) ? [chunk(token.text)] : []),
35
+ image: (token) => [chunk(token.text)],
36
+ link: (_token, children) =>
37
+ children.map((child) => underlineChunk({ ...child, fg: parseColor(theme.markdownLink) })),
38
+ space: () => [],
39
+ strong: (_token, children) => children.map((child) => boldChunk(child)),
40
+ text: (token) => [chunk(token.text)],
41
+ });
42
+ };
@@ -0,0 +1,45 @@
1
+ import type { Token } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { hasMarkedText, normalizeSoftLineEndings } from "./tokens.js";
5
+ import { visitInline } from "./visit-inline.js";
6
+
7
+ export const InlineTokens = function InlineTokens({
8
+ tokens,
9
+ theme,
10
+ }: {
11
+ tokens: readonly Token[];
12
+ theme: ResolvedTheme;
13
+ }): ReactNode {
14
+ return visitInline<ReactNode>(tokens, {
15
+ br: () => ["\n"],
16
+ codespan: (token, key) => [
17
+ <span key={key} fg={theme.markdownCode} bg={theme.backgroundPanel}>
18
+ {` ${token.text} `}
19
+ </span>,
20
+ ],
21
+ del: (_token, children, key) => [
22
+ <span key={key} fg={theme.textMuted}>
23
+ ~{children}~
24
+ </span>,
25
+ ],
26
+ em: (_token, children, key) => [<em key={key}>{children}</em>],
27
+ escape: (token) => [token.text],
28
+ fallback: (token) => (hasMarkedText(token) ? [token.text] : []),
29
+ image: (token, key) => [
30
+ <span key={key} fg={theme.textMuted}>
31
+ {token.text || token.href}
32
+ </span>,
33
+ ],
34
+ link: (token, children, key) => [
35
+ <u key={key}>
36
+ <a href={token.href} fg={theme.markdownLink}>
37
+ {children}
38
+ </a>
39
+ </u>,
40
+ ],
41
+ space: () => [" "],
42
+ strong: (_token, children, key) => [<strong key={key}>{children}</strong>],
43
+ text: (token) => [normalizeSoftLineEndings(token.text)],
44
+ });
45
+ };
@@ -0,0 +1,133 @@
1
+ import type { Token } from "marked";
2
+ import type { MouseEvent, TextBufferRenderable } from "@opentui/core";
3
+ import { hasMarkedText, narrowToken, normalizeSoftLineEndings } from "./tokens.js";
4
+
5
+ // oxlint-disable-next-line anti-slop/no-unknown-returns -- public host callback accepts any ignored result; only literal true marks a handled link
6
+ export type MarkdownLinkHandler = (href: string) => unknown;
7
+
8
+ export interface InlineLinkPosition {
9
+ line: number;
10
+ column: number;
11
+ }
12
+
13
+ /** Resolve a rendered text position to the Markdown link occupying that cell. */
14
+ export const inlineLinkAtPosition = function inlineLinkAtPosition(
15
+ tokens: readonly Token[],
16
+ position: InlineLinkPosition,
17
+ initialColumn = 0,
18
+ ): string | null {
19
+ let line = 0;
20
+ let column = initialColumn;
21
+ let found: string | null = null;
22
+
23
+ const advanceText = (value: string, href?: string): void => {
24
+ const parts = value.split("\n");
25
+ for (const [index, part] of parts.entries()) {
26
+ const width = Bun.stringWidth(part);
27
+ if (
28
+ href !== undefined &&
29
+ line === position.line &&
30
+ position.column >= column &&
31
+ position.column < column + width
32
+ ) {
33
+ found = href;
34
+ }
35
+ column += width;
36
+ if (index < parts.length - 1) {
37
+ line += 1;
38
+ column = 0;
39
+ }
40
+ }
41
+ };
42
+
43
+ const visit = (items: readonly Token[], href?: string): void => {
44
+ for (const token of items) {
45
+ if (found !== null) {
46
+ return;
47
+ }
48
+ const text = narrowToken(token, "text");
49
+ if (text !== null) {
50
+ advanceText(normalizeSoftLineEndings(text.text), href);
51
+ continue;
52
+ }
53
+ const link = narrowToken(token, "link");
54
+ if (link !== null) {
55
+ visit(link.tokens, link.href);
56
+ continue;
57
+ }
58
+ const strong = narrowToken(token, "strong");
59
+ if (strong !== null) {
60
+ visit(strong.tokens, href);
61
+ continue;
62
+ }
63
+ const em = narrowToken(token, "em");
64
+ if (em !== null) {
65
+ visit(em.tokens, href);
66
+ continue;
67
+ }
68
+ const del = narrowToken(token, "del");
69
+ if (del !== null) {
70
+ advanceText("~", href);
71
+ visit(del.tokens, href);
72
+ advanceText("~", href);
73
+ continue;
74
+ }
75
+ const codespan = narrowToken(token, "codespan");
76
+ if (codespan !== null) {
77
+ advanceText(` ${codespan.text} `, href);
78
+ continue;
79
+ }
80
+ if (narrowToken(token, "br") !== null) {
81
+ advanceText("\n", href);
82
+ } else if (narrowToken(token, "space") !== null) {
83
+ advanceText(" ", href);
84
+ } else if (hasMarkedText(token)) {
85
+ advanceText(token.text, href);
86
+ }
87
+ }
88
+ };
89
+
90
+ visit(tokens);
91
+ return found;
92
+ };
93
+
94
+ export const linkMouseHandler = function linkMouseHandler(
95
+ tokens: readonly Token[],
96
+ onLinkActivate: MarkdownLinkHandler | undefined,
97
+ initialColumn = 0,
98
+ ): ((event: MouseEvent) => void) | undefined {
99
+ if (onLinkActivate === undefined) {
100
+ return undefined;
101
+ }
102
+ return (event: MouseEvent): void => {
103
+ if (event.button !== 0) {
104
+ return;
105
+ }
106
+ const { target } = event;
107
+ if (target === null || target === undefined || !("lineInfo" in target)) {
108
+ return;
109
+ }
110
+ // SAFETY: OpenTUI supplies a renderable target, and the lineInfo guard proves this text-buffer member.
111
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- OpenTUI supplies renderable mouse targets
112
+ const text = target as TextBufferRenderable;
113
+ const visualLine = event.y - text.y;
114
+ const sourceLine = text.lineInfo.lineSources[visualLine];
115
+ const startColumn = text.lineInfo.lineStartCols[visualLine];
116
+ if (sourceLine === undefined || startColumn === undefined) {
117
+ return;
118
+ }
119
+ const href = inlineLinkAtPosition(
120
+ tokens,
121
+ {
122
+ column: startColumn + event.x - text.x,
123
+ line: sourceLine,
124
+ },
125
+ initialColumn,
126
+ );
127
+ if (href === null || onLinkActivate(href) !== true) {
128
+ return;
129
+ }
130
+ event.preventDefault();
131
+ event.stopPropagation();
132
+ };
133
+ };
@@ -0,0 +1,22 @@
1
+ import type { Token } from "marked";
2
+ import { hasMarkedText, narrowToken } from "./tokens.js";
3
+
4
+ /** Extract unstyled inline text for table-cell fallback content. */
5
+ export const getPlainText = function getPlainText(tokens: readonly Token[]): string {
6
+ return tokens
7
+ .map((token) => {
8
+ const text = narrowToken(token, "text");
9
+ if (text !== null) {
10
+ return text.text;
11
+ }
12
+ const codespan = narrowToken(token, "codespan");
13
+ if (codespan !== null) {
14
+ return codespan.text;
15
+ }
16
+ if ("tokens" in token && Array.isArray(token.tokens)) {
17
+ return getPlainText(token.tokens);
18
+ }
19
+ return hasMarkedText(token) ? token.text : "";
20
+ })
21
+ .join("");
22
+ };
@@ -0,0 +1,53 @@
1
+ import type { Token, Tokens } from "marked";
2
+
3
+ interface KnownTokenByType {
4
+ blockquote: Tokens.Blockquote;
5
+ br: Tokens.Br;
6
+ code: Tokens.Code;
7
+ codespan: Tokens.Codespan;
8
+ del: Tokens.Del;
9
+ em: Tokens.Em;
10
+ escape: Tokens.Escape;
11
+ heading: Tokens.Heading;
12
+ image: Tokens.Image;
13
+ link: Tokens.Link;
14
+ paragraph: Tokens.Paragraph;
15
+ space: Tokens.Space;
16
+ strong: Tokens.Strong;
17
+ table: Tokens.Table;
18
+ text: Tokens.Text;
19
+ }
20
+
21
+ export interface MarkedText {
22
+ text: string;
23
+ }
24
+
25
+ /**
26
+ * Narrow Marked's broad token contract after checking its runtime discriminator.
27
+ * Marked's `Generic` fallback prevents TypeScript from retaining this narrowing.
28
+ */
29
+ export const narrowToken = function narrowToken<Type extends keyof KnownTokenByType>(
30
+ token: Token,
31
+ type: Type,
32
+ ): KnownTokenByType[Type] | null {
33
+ if (token.type !== type) {
34
+ return null;
35
+ }
36
+ // SAFETY: Marked creates the token member selected by the checked type discriminator.
37
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the Generic fallback prevents discriminator narrowing
38
+ return token as KnownTokenByType[Type];
39
+ };
40
+
41
+ /** Decode the optional text field on Marked's broad fallback token contract. */
42
+ export const hasMarkedText = function hasMarkedText(token: Token): token is Token & MarkedText {
43
+ if (!("text" in token)) {
44
+ return false;
45
+ }
46
+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- primitive check stays at the Marked token boundary
47
+ return typeof token.text === "string";
48
+ };
49
+
50
+ /** Markdown soft line endings separate prose words without forcing a rendered break. */
51
+ export const normalizeSoftLineEndings = function normalizeSoftLineEndings(value: string): string {
52
+ return value.replaceAll("\n", " ");
53
+ };
@@ -0,0 +1,82 @@
1
+ import type { Token, Tokens } from "marked";
2
+ import { narrowToken } from "./tokens.js";
3
+
4
+ export interface InlineVisitorSink<Result> {
5
+ text: (token: Tokens.Text, key: string) => readonly Result[];
6
+ strong: (token: Tokens.Strong, children: readonly Result[], key: string) => readonly Result[];
7
+ em: (token: Tokens.Em, children: readonly Result[], key: string) => readonly Result[];
8
+ codespan: (token: Tokens.Codespan, key: string) => readonly Result[];
9
+ link: (token: Tokens.Link, children: readonly Result[], key: string) => readonly Result[];
10
+ del: (token: Tokens.Del, children: readonly Result[], key: string) => readonly Result[];
11
+ image: (token: Tokens.Image, key: string) => readonly Result[];
12
+ br: (token: Tokens.Br, key: string) => readonly Result[];
13
+ escape: (token: Tokens.Escape, key: string) => readonly Result[];
14
+ space: (token: Tokens.Space, key: string) => readonly Result[];
15
+ fallback: (token: Token, key: string) => readonly Result[];
16
+ }
17
+
18
+ /** Walk inline Marked tokens once and delegate output construction to a sink. */
19
+ export const visitInline = function visitInline<Result>(
20
+ tokens: readonly Token[],
21
+ sink: InlineVisitorSink<Result>,
22
+ keyPrefix = "",
23
+ ): Result[] {
24
+ const result: Result[] = [];
25
+
26
+ for (const [index, token] of tokens.entries()) {
27
+ const key = `${keyPrefix}${index}`;
28
+ const text = narrowToken(token, "text");
29
+ if (text !== null) {
30
+ result.push(...sink.text(text, key));
31
+ continue;
32
+ }
33
+ const strong = narrowToken(token, "strong");
34
+ if (strong !== null) {
35
+ result.push(...sink.strong(strong, visitInline(strong.tokens, sink, `${key}.`), key));
36
+ continue;
37
+ }
38
+ const em = narrowToken(token, "em");
39
+ if (em !== null) {
40
+ result.push(...sink.em(em, visitInline(em.tokens, sink, `${key}.`), key));
41
+ continue;
42
+ }
43
+ const codespan = narrowToken(token, "codespan");
44
+ if (codespan !== null) {
45
+ result.push(...sink.codespan(codespan, key));
46
+ continue;
47
+ }
48
+ const link = narrowToken(token, "link");
49
+ if (link !== null) {
50
+ result.push(...sink.link(link, visitInline(link.tokens, sink, `${key}.`), key));
51
+ continue;
52
+ }
53
+ const del = narrowToken(token, "del");
54
+ if (del !== null) {
55
+ result.push(...sink.del(del, visitInline(del.tokens, sink, `${key}.`), key));
56
+ continue;
57
+ }
58
+ const image = narrowToken(token, "image");
59
+ if (image !== null) {
60
+ result.push(...sink.image(image, key));
61
+ continue;
62
+ }
63
+ const br = narrowToken(token, "br");
64
+ if (br !== null) {
65
+ result.push(...sink.br(br, key));
66
+ continue;
67
+ }
68
+ const escape = narrowToken(token, "escape");
69
+ if (escape !== null) {
70
+ result.push(...sink.escape(escape, key));
71
+ continue;
72
+ }
73
+ const space = narrowToken(token, "space");
74
+ if (space !== null) {
75
+ result.push(...sink.space(space, key));
76
+ continue;
77
+ }
78
+ result.push(...sink.fallback(token, key));
79
+ }
80
+
81
+ return result;
82
+ };
@@ -54,7 +54,7 @@ export const checkboxMarker = function checkboxMarker(checked: boolean | undefin
54
54
  * search and copy in agreement with the source mapping on synthetic rows.
55
55
  */
56
56
  export const getFlatBlockText = function getFlatBlockText(block: FlatBlock): string {
57
- const raw = "raw" in block.token && typeof block.token.raw === "string" ? block.token.raw : "";
57
+ const { raw } = block.token;
58
58
  if (raw.length > 0) {
59
59
  return raw;
60
60
  }
@@ -223,7 +223,7 @@ const flattenList = function flattenList(
223
223
  }
224
224
  }
225
225
 
226
- // oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): preserve deliberate top-down list flattening organization
226
+ // oxlint-disable-next-line no-use-before-define -- preserve deliberate top-down list flattening organization
227
227
  flattenListItem(item, indent, bullet, out, res, itemStart, itemEnd);
228
228
 
229
229
  if (res && itemStart !== null) {
@@ -275,7 +275,8 @@ const flattenListItem = function flattenListItem(
275
275
  if (!bulletUsed) {
276
276
  emitBulletMarker();
277
277
  }
278
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Deferred(lint-sweep): marked token narrowing; schema-based validation in a later sweep
278
+ // SAFETY: Marked creates a List token for the adjacent token.type branch.
279
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
279
280
  flattenList(token as Tokens.List, indent + bullet.length, out, res, itemEnd);
280
281
  } else {
281
282
  // Block content (code, table, blockquote, hr, etc.).
@@ -308,7 +309,8 @@ const flattenWalk = function flattenWalk(
308
309
  continue;
309
310
  }
310
311
  if (token.type === "list") {
311
- // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Deferred(lint-sweep): marked token narrowing; schema-based validation in a later sweep
312
+ // SAFETY: Marked creates a List token for the adjacent token.type branch.
313
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
312
314
  flattenList(token as Tokens.List, indent, out, res, bound);
313
315
  } else {
314
316
  out.push({ indent, source: res ? res.resolveRaw(token.raw ?? "", bound) : null, token });
@@ -1,5 +1,5 @@
1
1
  import path from "node:path";
2
- import type { Token } from "marked";
2
+ import type { Token, Tokens } from "marked";
3
3
 
4
4
  const OBSIDIAN_IMAGE_EMBED = /!\[\[(?<target>[^\]|]+)(?:\|(?<display>[^\]]+))?\]\]/gu;
5
5
  const IMAGE_DIMENSIONS = /^(?<width>\d+)(?:x(?<height>\d+))?$/u;
@@ -93,14 +93,23 @@ export const splitMarkdownImages = function splitMarkdownImages(
93
93
  };
94
94
 
95
95
  for (const token of tokens) {
96
- if (token.type === "image" && "href" in token && typeof token.href === "string") {
96
+ if (token.type === "image") {
97
+ // SAFETY: Marked creates an Image token for the adjacent token.type branch.
98
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
99
+ const imageToken = token as Tokens.Image;
97
100
  flushText();
98
- const alt = "text" in token && typeof token.text === "string" ? token.text : undefined;
99
- result.push({ alt: alt === "" ? undefined : alt, source: token.href, type: "image" });
101
+ result.push({
102
+ alt: imageToken.text === "" ? undefined : imageToken.text,
103
+ source: imageToken.href,
104
+ type: "image",
105
+ });
100
106
  continue;
101
107
  }
102
- if (token.type === "text" && "text" in token && typeof token.text === "string") {
103
- const parts = splitTextToken(token.text);
108
+ if (token.type === "text") {
109
+ // SAFETY: Marked creates a Text token for the adjacent token.type branch.
110
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
111
+ const textToken = token as Tokens.Text;
112
+ const parts = splitTextToken(textToken.text);
104
113
  for (const part of parts) {
105
114
  if (part.type === "text") {
106
115
  pendingText.push(...part.tokens);