@vvedantb/ui 0.84.1 → 0.85.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.
@@ -1,8 +1,6 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { memo } from "react";
3
3
  import { cjk } from "@streamdown/cjk";
4
- import { code } from "@streamdown/code";
5
- import { math } from "@streamdown/math";
6
4
  import { Streamdown, defaultRehypePlugins, defaultRemarkPlugins } from "streamdown";
7
5
  import { useCn } from "../ui/ui-config.js";
8
6
  import { parseMarkdownIntoBlocksIncremental } from "./incrementalBlocks.js";
@@ -10,18 +8,25 @@ import { MarkdownBlockquote } from "./MarkdownBlockquote.js";
10
8
  import { MarkdownPre } from "./MarkdownCodeBlock.js";
11
9
  import { MarkdownInlineCode } from "./MarkdownInlineCode.js";
12
10
  import { MarkdownLink } from "./MarkdownLink.js";
11
+ import { useMathPlugin } from "./lazyMath.js";
13
12
  import { mermaidPlugin } from "./mermaid.js";
14
13
  import { copyMarkdownSelection } from "./markdownClipboard.js";
15
14
  import { rehypeGithubAlerts } from "./rehypeGithubAlerts.js";
16
- import "katex/dist/katex.min.css";
17
15
  import { useSmoothText } from "./useSmoothText.js";
18
16
  import { jsx as _jsx } from "react/jsx-runtime";
19
17
  const PLUGINS = {
20
18
  cjk,
21
- code,
22
- math,
23
19
  mermaid: mermaidPlugin
24
20
  };
21
+ let pluginsWithMath = null;
22
+ function pluginsFor(math) {
23
+ if (math === null) return PLUGINS;
24
+ pluginsWithMath ??= {
25
+ ...PLUGINS,
26
+ math
27
+ };
28
+ return pluginsWithMath;
29
+ }
25
30
  const REMARK_PLUGINS = Object.values(defaultRemarkPlugins);
26
31
  const REHYPE_PLUGINS = [...Object.values(defaultRehypePlugins), rehypeGithubAlerts];
27
32
  const MARKDOWN_COMPONENTS = {
@@ -55,7 +60,7 @@ const MARKDOWN_COMPONENTS = {
55
60
  * (mention chips, line breaks).
56
61
  */
57
62
  export const Markdown = /*#__PURE__*/memo(function Markdown(t0) {
58
- const $ = _c(20);
63
+ const $ = _c(23);
59
64
  let children;
60
65
  let className;
61
66
  let components;
@@ -89,6 +94,7 @@ export const Markdown = /*#__PURE__*/memo(function Markdown(t0) {
89
94
  const smooth = t1 === undefined ? true : t1;
90
95
  const merge = useCn();
91
96
  const text = useSmoothText(children, smooth && props.mode === "streaming");
97
+ const math = useMathPlugin(text);
92
98
  let t2;
93
99
  if ($[7] !== className || $[8] !== merge) {
94
100
  t2 = merge("markdown", className);
@@ -99,48 +105,57 @@ export const Markdown = /*#__PURE__*/memo(function Markdown(t0) {
99
105
  t2 = $[9];
100
106
  }
101
107
  let t3;
102
- if ($[10] !== components) {
103
- t3 = components ? {
104
- ...MARKDOWN_COMPONENTS,
105
- ...components
106
- } : MARKDOWN_COMPONENTS;
107
- $[10] = components;
108
+ if ($[10] !== math) {
109
+ t3 = pluginsFor(math);
110
+ $[10] = math;
108
111
  $[11] = t3;
109
112
  } else {
110
113
  t3 = $[11];
111
114
  }
112
115
  let t4;
113
- if ($[12] !== remarkPlugins) {
114
- t4 = remarkPlugins ? [...REMARK_PLUGINS, ...remarkPlugins] : REMARK_PLUGINS;
115
- $[12] = remarkPlugins;
116
+ if ($[12] !== components) {
117
+ t4 = components ? {
118
+ ...MARKDOWN_COMPONENTS,
119
+ ...components
120
+ } : MARKDOWN_COMPONENTS;
121
+ $[12] = components;
116
122
  $[13] = t4;
117
123
  } else {
118
124
  t4 = $[13];
119
125
  }
120
126
  let t5;
121
- if ($[14] !== props || $[15] !== t2 || $[16] !== t3 || $[17] !== t4 || $[18] !== text) {
122
- t5 = /*#__PURE__*/_jsx("div", {
127
+ if ($[14] !== remarkPlugins) {
128
+ t5 = remarkPlugins ? [...REMARK_PLUGINS, ...remarkPlugins] : REMARK_PLUGINS;
129
+ $[14] = remarkPlugins;
130
+ $[15] = t5;
131
+ } else {
132
+ t5 = $[15];
133
+ }
134
+ let t6;
135
+ if ($[16] !== props || $[17] !== t2 || $[18] !== t3 || $[19] !== t4 || $[20] !== t5 || $[21] !== text) {
136
+ t6 = /*#__PURE__*/_jsx("div", {
123
137
  className: "contents",
124
138
  onCopy: copyMarkdownSelection,
125
139
  children: /*#__PURE__*/_jsx(Streamdown, {
126
140
  className: t2,
127
- plugins: PLUGINS,
128
- components: t3,
129
- remarkPlugins: t4,
141
+ plugins: t3,
142
+ components: t4,
143
+ remarkPlugins: t5,
130
144
  rehypePlugins: REHYPE_PLUGINS,
131
145
  parseMarkdownIntoBlocksFn: parseMarkdownIntoBlocksIncremental,
132
146
  ...props,
133
147
  children: text
134
148
  })
135
149
  });
136
- $[14] = props;
137
- $[15] = t2;
138
- $[16] = t3;
139
- $[17] = t4;
140
- $[18] = text;
141
- $[19] = t5;
150
+ $[16] = props;
151
+ $[17] = t2;
152
+ $[18] = t3;
153
+ $[19] = t4;
154
+ $[20] = t5;
155
+ $[21] = text;
156
+ $[22] = t6;
142
157
  } else {
143
- t5 = $[19];
158
+ t6 = $[22];
144
159
  }
145
- return t5;
160
+ return t6;
146
161
  });
@@ -1,8 +1,26 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
- import { code as shiki } from "@streamdown/code";
3
2
  import { useState, useSyncExternalStore } from "react";
4
- const THEMES = shiki.getThemes();
5
- const LANGUAGES = new Map(shiki.getSupportedLanguages().map(id => [id, id]));
3
+ let engine = null;
4
+ let loading = null;
5
+ const engineListeners = new Set();
6
+ function subscribeEngine(notify) {
7
+ engineListeners.add(notify);
8
+ return () => engineListeners.delete(notify);
9
+ }
10
+ function loadEngine() {
11
+ loading ??= import("@streamdown/code").then(({
12
+ code: shiki
13
+ }) => {
14
+ engine = {
15
+ shiki,
16
+ themes: shiki.getThemes(),
17
+ languages: new Map(shiki.getSupportedLanguages().map(id => [id, id]))
18
+ };
19
+ for (const notify of engineListeners) notify();
20
+ }, () => {
21
+ loading = null;
22
+ });
23
+ }
6
24
  const CACHE_LIMIT = 200;
7
25
  const cache = new Map();
8
26
  const listeners = new Map();
@@ -16,7 +34,10 @@ function store(key, entry) {
16
34
  const oldest = cache.keys().next().value;
17
35
  if (oldest !== undefined && !listeners.has(oldest)) cache.delete(oldest);
18
36
  }
19
- function request(language, source) {
37
+ function request({
38
+ shiki,
39
+ themes
40
+ }, language, source) {
20
41
  const key = cacheKey(language, source);
21
42
  const hit = cache.get(key);
22
43
  if (hit !== undefined) return hit.status === "done" ? hit.result : null;
@@ -26,7 +47,7 @@ function request(language, source) {
26
47
  const sync = shiki.highlight({
27
48
  code: source,
28
49
  language,
29
- themes: THEMES
50
+ themes
30
51
  }, result => {
31
52
  store(key, {
32
53
  status: "done",
@@ -82,42 +103,48 @@ export function buildCodeLines(code, highlighted) {
82
103
  * for), so streaming never flashes back to plain text.
83
104
  */
84
105
  export function useHighlightedLines(code, language, isIncomplete) {
85
- const $ = _c(18);
106
+ const $ = _c(20);
107
+ const loaded = useSyncExternalStore(subscribeEngine, _temp, _temp2);
108
+ if (loaded === null && language !== "") {
109
+ loadEngine();
110
+ }
86
111
  let bundled;
87
112
  let source;
88
113
  let t0;
89
- if ($[0] !== code || $[1] !== isIncomplete || $[2] !== language) {
90
- bundled = LANGUAGES.get(language.toLowerCase()) ?? null;
114
+ if ($[0] !== code || $[1] !== isIncomplete || $[2] !== language || $[3] !== loaded?.languages) {
115
+ bundled = loaded?.languages.get(language.toLowerCase()) ?? null;
91
116
  source = bundled === null ? "" : highlightSource(code, isIncomplete);
92
117
  t0 = bundled === null ? "" : cacheKey(bundled, source);
93
118
  $[0] = code;
94
119
  $[1] = isIncomplete;
95
120
  $[2] = language;
96
- $[3] = bundled;
97
- $[4] = source;
98
- $[5] = t0;
121
+ $[3] = loaded?.languages;
122
+ $[4] = bundled;
123
+ $[5] = source;
124
+ $[6] = t0;
99
125
  } else {
100
- bundled = $[3];
101
- source = $[4];
102
- t0 = $[5];
126
+ bundled = $[4];
127
+ source = $[5];
128
+ t0 = $[6];
103
129
  }
104
130
  const key = t0;
105
131
  let t1;
106
- if ($[6] !== key) {
107
- t1 = notify => key === "" ? _temp : subscribe(key, notify);
108
- $[6] = key;
109
- $[7] = t1;
132
+ if ($[7] !== key) {
133
+ t1 = notify => key === "" ? _temp3 : subscribe(key, notify);
134
+ $[7] = key;
135
+ $[8] = t1;
110
136
  } else {
111
- t1 = $[7];
137
+ t1 = $[8];
112
138
  }
113
139
  let t2;
114
- if ($[8] !== bundled || $[9] !== source) {
115
- t2 = () => bundled === null || source === "" ? null : request(bundled, source);
116
- $[8] = bundled;
117
- $[9] = source;
118
- $[10] = t2;
140
+ if ($[9] !== bundled || $[10] !== loaded || $[11] !== source) {
141
+ t2 = () => loaded === null || bundled === null || source === "" ? null : request(loaded, bundled, source);
142
+ $[9] = bundled;
143
+ $[10] = loaded;
144
+ $[11] = source;
145
+ $[12] = t2;
119
146
  } else {
120
- t2 = $[10];
147
+ t2 = $[12];
121
148
  }
122
149
  const result = useSyncExternalStore(t1, t2);
123
150
  const [shown, setShown] = useState(null);
@@ -128,27 +155,33 @@ export function useHighlightedLines(code, language, isIncomplete) {
128
155
  });
129
156
  }
130
157
  let t3;
131
- if ($[11] !== result || $[12] !== shown || $[13] !== source) {
158
+ if ($[13] !== result || $[14] !== shown || $[15] !== source) {
132
159
  t3 = result !== null ? {
133
160
  source,
134
161
  result
135
162
  } : shown;
136
- $[11] = result;
137
- $[12] = shown;
138
- $[13] = source;
139
- $[14] = t3;
163
+ $[13] = result;
164
+ $[14] = shown;
165
+ $[15] = source;
166
+ $[16] = t3;
140
167
  } else {
141
- t3 = $[14];
168
+ t3 = $[16];
142
169
  }
143
170
  let t4;
144
- if ($[15] !== code || $[16] !== t3) {
171
+ if ($[17] !== code || $[18] !== t3) {
145
172
  t4 = buildCodeLines(code, t3);
146
- $[15] = code;
147
- $[16] = t3;
148
- $[17] = t4;
173
+ $[17] = code;
174
+ $[18] = t3;
175
+ $[19] = t4;
149
176
  } else {
150
- t4 = $[17];
177
+ t4 = $[19];
151
178
  }
152
179
  return t4;
153
180
  }
154
- function _temp() {}
181
+ function _temp3() {}
182
+ function _temp2() {
183
+ return engine;
184
+ }
185
+ function _temp() {
186
+ return engine;
187
+ }
@@ -0,0 +1,36 @@
1
+ import { useSyncExternalStore } from "react";
2
+ const MATH_HINT = /\$\$|```math/;
3
+ let math = null;
4
+ let loading = null;
5
+ const listeners = new Set();
6
+ function subscribe(notify) {
7
+ listeners.add(notify);
8
+ return () => listeners.delete(notify);
9
+ }
10
+ function load() {
11
+ loading ??= import("./mathPlugin.js").then(module => {
12
+ math = module.math;
13
+ for (const notify of listeners) notify();
14
+ }, () => {
15
+ loading = null;
16
+ });
17
+ }
18
+
19
+ /**
20
+ * KaTeX and its CSS load on the first text that holds `$$` or a `math`
21
+ * fence (single `$` is off), so pages without maths never fetch them.
22
+ */
23
+ export function useMathPlugin(text) {
24
+ const loaded = useSyncExternalStore(subscribe, _temp, _temp2);
25
+ const needed = MATH_HINT.test(text);
26
+ if (needed && loaded === null) {
27
+ load();
28
+ }
29
+ return needed ? loaded : null;
30
+ }
31
+ function _temp2() {
32
+ return math;
33
+ }
34
+ function _temp() {
35
+ return math;
36
+ }
@@ -0,0 +1,2 @@
1
+ import "katex/dist/katex.min.css";
2
+ export { math } from "@streamdown/math";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/ui",
3
- "version": "0.84.1",
3
+ "version": "0.85.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React 19 UI for web apps: one main JavaScript entry (primitives, shell pieces, app helpers including the Convex and Clerk provider, VITE env parsing and ensure-user, artifact viewers over the @vvedantb/server artifacts core, native tokens), third-party brand logos (also @vvedantb/ui/logos), motion-driven animated SVG icons (@vvedantb/ui/icons/animated), a client entry without @vvedantb/server, convex or Clerk for Chrome extensions, design-token CSS, shared Tailwind v4 CSS, a Tailwind preset and packaged Instrument Sans fonts.",
6
6
  "type": "module",
@@ -1,9 +1,7 @@
1
1
  import type { ComponentProps } from "react";
2
2
  import { memo } from "react";
3
3
  import { cjk } from "@streamdown/cjk";
4
- import { code } from "@streamdown/code";
5
- import { math } from "@streamdown/math";
6
- import type { Components } from "streamdown";
4
+ import type { Components, MathPlugin } from "streamdown";
7
5
  import {
8
6
  Streamdown,
9
7
  defaultRehypePlugins,
@@ -16,13 +14,20 @@ import { MarkdownBlockquote } from "./MarkdownBlockquote";
16
14
  import { MarkdownPre } from "./MarkdownCodeBlock";
17
15
  import { MarkdownInlineCode } from "./MarkdownInlineCode";
18
16
  import { MarkdownLink } from "./MarkdownLink";
17
+ import { useMathPlugin } from "./lazyMath";
19
18
  import { mermaidPlugin } from "./mermaid";
20
19
  import { copyMarkdownSelection } from "./markdownClipboard";
21
20
  import { rehypeGithubAlerts } from "./rehypeGithubAlerts";
22
- import "katex/dist/katex.min.css";
23
21
  import { useSmoothText } from "./useSmoothText";
24
22
 
25
- const PLUGINS = { cjk, code, math, mermaid: mermaidPlugin };
23
+ const PLUGINS = { cjk, mermaid: mermaidPlugin };
24
+ let pluginsWithMath: typeof PLUGINS & { math: MathPlugin } | null = null;
25
+
26
+ function pluginsFor(math: MathPlugin | null) {
27
+ if (math === null) return PLUGINS;
28
+ pluginsWithMath ??= { ...PLUGINS, math };
29
+ return pluginsWithMath;
30
+ }
26
31
 
27
32
  const REMARK_PLUGINS = Object.values(defaultRemarkPlugins);
28
33
  const REHYPE_PLUGINS = [...Object.values(defaultRehypePlugins), rehypeGithubAlerts];
@@ -76,11 +81,12 @@ export const Markdown = memo(function Markdown({
76
81
  }: MarkdownProps) {
77
82
  const merge = useCn();
78
83
  const text = useSmoothText(children, smooth && props.mode === "streaming");
84
+ const math = useMathPlugin(text);
79
85
  return (
80
86
  <div className="contents" onCopy={copyMarkdownSelection}>
81
87
  <Streamdown
82
88
  className={merge("markdown", className)}
83
- plugins={PLUGINS}
89
+ plugins={pluginsFor(math)}
84
90
  components={
85
91
  components ? { ...MARKDOWN_COMPONENTS, ...components } : MARKDOWN_COMPONENTS
86
92
  }
@@ -1,15 +1,42 @@
1
- import { code as shiki } from "@streamdown/code";
2
1
  import { useState, useSyncExternalStore } from "react";
3
2
  import type { CodeHighlighterPlugin } from "streamdown";
4
3
 
5
- type BundledLanguage = ReturnType<typeof shiki.getSupportedLanguages>[number];
4
+ type Shiki = (typeof import("@streamdown/code"))["code"];
5
+ type BundledLanguage = ReturnType<Shiki["getSupportedLanguages"]>[number];
6
6
  type HighlightResult = NonNullable<ReturnType<CodeHighlighterPlugin["highlight"]>>;
7
7
  export type HighlightToken = HighlightResult["tokens"][number][number];
8
8
 
9
- const THEMES = shiki.getThemes();
10
- const LANGUAGES = new Map<string, BundledLanguage>(
11
- shiki.getSupportedLanguages().map((id) => [id, id]),
12
- );
9
+ type Engine = {
10
+ shiki: Shiki;
11
+ themes: ReturnType<Shiki["getThemes"]>;
12
+ languages: Map<string, BundledLanguage>;
13
+ };
14
+
15
+ let engine: Engine | null = null;
16
+ let loading: Promise<void> | null = null;
17
+ const engineListeners = new Set<() => void>();
18
+
19
+ function subscribeEngine(notify: () => void): () => void {
20
+ engineListeners.add(notify);
21
+ return () => engineListeners.delete(notify);
22
+ }
23
+
24
+ function loadEngine(): void {
25
+ loading ??= import("@streamdown/code").then(
26
+ ({ code: shiki }) => {
27
+ engine = {
28
+ shiki,
29
+ themes: shiki.getThemes(),
30
+ languages: new Map(shiki.getSupportedLanguages().map((id) => [id, id])),
31
+ };
32
+ for (const notify of engineListeners) notify();
33
+ },
34
+ () => {
35
+ loading = null;
36
+ },
37
+ );
38
+ }
39
+
13
40
  const CACHE_LIMIT = 200;
14
41
 
15
42
  type Entry = { status: "pending" } | { status: "done"; result: HighlightResult };
@@ -29,14 +56,18 @@ function store(key: string, entry: Entry): void {
29
56
  if (oldest !== undefined && !listeners.has(oldest)) cache.delete(oldest);
30
57
  }
31
58
 
32
- function request(language: BundledLanguage, source: string): HighlightResult | null {
59
+ function request(
60
+ { shiki, themes }: Engine,
61
+ language: BundledLanguage,
62
+ source: string,
63
+ ): HighlightResult | null {
33
64
  const key = cacheKey(language, source);
34
65
  const hit = cache.get(key);
35
66
  if (hit !== undefined) return hit.status === "done" ? hit.result : null;
36
67
 
37
68
  store(key, { status: "pending" });
38
69
  const sync = shiki.highlight(
39
- { code: source, language, themes: THEMES },
70
+ { code: source, language, themes },
40
71
  (result) => {
41
72
  store(key, { status: "done", result });
42
73
  for (const notify of listeners.get(key) ?? []) notify();
@@ -101,13 +132,18 @@ export function useHighlightedLines(
101
132
  language: string,
102
133
  isIncomplete: boolean,
103
134
  ): CodeLine[] {
104
- const bundled = LANGUAGES.get(language.toLowerCase()) ?? null;
135
+ const loaded = useSyncExternalStore(subscribeEngine, () => engine, () => engine);
136
+ if (loaded === null && language !== "") loadEngine();
137
+ const bundled = loaded?.languages.get(language.toLowerCase()) ?? null;
105
138
  const source = bundled === null ? "" : highlightSource(code, isIncomplete);
106
139
  const key = bundled === null ? "" : cacheKey(bundled, source);
107
140
 
108
141
  const result = useSyncExternalStore(
109
142
  (notify) => (key === "" ? () => {} : subscribe(key, notify)),
110
- () => (bundled === null || source === "" ? null : request(bundled, source)),
143
+ () =>
144
+ loaded === null || bundled === null || source === ""
145
+ ? null
146
+ : request(loaded, bundled, source),
111
147
  );
112
148
 
113
149
  const [shown, setShown] = useState<{
@@ -0,0 +1,36 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import type { MathPlugin } from "streamdown";
3
+
4
+ const MATH_HINT = /\$\$|```math/;
5
+
6
+ let math: MathPlugin | null = null;
7
+ let loading: Promise<void> | null = null;
8
+ const listeners = new Set<() => void>();
9
+
10
+ function subscribe(notify: () => void): () => void {
11
+ listeners.add(notify);
12
+ return () => listeners.delete(notify);
13
+ }
14
+
15
+ function load(): void {
16
+ loading ??= import("./mathPlugin").then(
17
+ (module) => {
18
+ math = module.math;
19
+ for (const notify of listeners) notify();
20
+ },
21
+ () => {
22
+ loading = null;
23
+ },
24
+ );
25
+ }
26
+
27
+ /**
28
+ * KaTeX and its CSS load on the first text that holds `$$` or a `math`
29
+ * fence (single `$` is off), so pages without maths never fetch them.
30
+ */
31
+ export function useMathPlugin(text: string): MathPlugin | null {
32
+ const loaded = useSyncExternalStore(subscribe, () => math, () => math);
33
+ const needed = MATH_HINT.test(text);
34
+ if (needed && loaded === null) load();
35
+ return needed ? loaded : null;
36
+ }
@@ -0,0 +1,3 @@
1
+ import "katex/dist/katex.min.css";
2
+
3
+ export { math } from "@streamdown/math";