@vvedantb/ui 0.84.0 → 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,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";