@vocasync/astro 0.1.9 → 1.0.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 (78) hide show
  1. package/README.md +77 -41
  2. package/dist/api/client.d.ts +63 -50
  3. package/dist/api/client.d.ts.map +1 -1
  4. package/dist/api/client.js +132 -145
  5. package/dist/api/client.js.map +1 -1
  6. package/dist/api/index.d.ts +1 -1
  7. package/dist/api/index.d.ts.map +1 -1
  8. package/dist/api/index.js +1 -1
  9. package/dist/api/index.js.map +1 -1
  10. package/dist/cli.js +7 -7
  11. package/dist/cli.js.map +1 -1
  12. package/dist/components/AudioPlayer.astro +136 -137
  13. package/dist/config/schema.d.ts +21 -20
  14. package/dist/config/schema.d.ts.map +1 -1
  15. package/dist/config/schema.js +15 -4
  16. package/dist/config/schema.js.map +1 -1
  17. package/dist/core/audio-map.js +1 -1
  18. package/dist/core/speech-builder.d.ts +10 -7
  19. package/dist/core/speech-builder.d.ts.map +1 -1
  20. package/dist/core/speech-builder.js +73 -168
  21. package/dist/core/speech-builder.js.map +1 -1
  22. package/dist/core/spoken-form.d.ts +52 -0
  23. package/dist/core/spoken-form.d.ts.map +1 -0
  24. package/dist/core/spoken-form.js +279 -0
  25. package/dist/core/spoken-form.js.map +1 -0
  26. package/dist/lib/latex-to-speech/index.d.ts +9 -2
  27. package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
  28. package/dist/lib/latex-to-speech/index.js +20 -22
  29. package/dist/lib/latex-to-speech/index.js.map +1 -1
  30. package/dist/lib/latex-to-speech/speak.d.ts +13 -0
  31. package/dist/lib/latex-to-speech/speak.d.ts.map +1 -0
  32. package/dist/lib/latex-to-speech/speak.js +47 -0
  33. package/dist/lib/latex-to-speech/speak.js.map +1 -0
  34. package/dist/rehype/audio-words.d.ts +14 -24
  35. package/dist/rehype/audio-words.d.ts.map +1 -1
  36. package/dist/rehype/audio-words.js +131 -252
  37. package/dist/rehype/audio-words.js.map +1 -1
  38. package/dist/rehype/index.d.ts +1 -2
  39. package/dist/rehype/index.d.ts.map +1 -1
  40. package/dist/rehype/index.js +0 -1
  41. package/dist/rehype/index.js.map +1 -1
  42. package/dist/rehype/rehype-math-speech.d.ts +26 -10
  43. package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
  44. package/dist/rehype/rehype-math-speech.js +59 -92
  45. package/dist/rehype/rehype-math-speech.js.map +1 -1
  46. package/dist/rehype/shared.d.ts +9 -0
  47. package/dist/rehype/shared.d.ts.map +1 -0
  48. package/dist/rehype/shared.js +31 -0
  49. package/dist/rehype/shared.js.map +1 -0
  50. package/dist/sync/orchestrator.d.ts +4 -22
  51. package/dist/sync/orchestrator.d.ts.map +1 -1
  52. package/dist/sync/orchestrator.js +123 -123
  53. package/dist/sync/orchestrator.js.map +1 -1
  54. package/dist/types/index.d.ts +82 -46
  55. package/dist/types/index.d.ts.map +1 -1
  56. package/package.json +14 -11
  57. package/src/api/client.ts +175 -190
  58. package/src/api/index.ts +8 -1
  59. package/src/cli.ts +7 -14
  60. package/src/components/AudioPlayer.astro +136 -137
  61. package/src/config/schema.ts +15 -4
  62. package/src/core/audio-map.ts +1 -1
  63. package/src/core/speech-builder.ts +75 -192
  64. package/src/core/spoken-form.ts +298 -0
  65. package/src/lib/latex-to-speech/index.ts +32 -22
  66. package/src/lib/latex-to-speech/speak.ts +55 -0
  67. package/src/rehype/audio-words.ts +145 -307
  68. package/src/rehype/index.ts +1 -2
  69. package/src/rehype/rehype-math-speech.ts +64 -117
  70. package/src/rehype/shared.ts +36 -0
  71. package/src/sync/orchestrator.ts +150 -157
  72. package/src/types/index.ts +85 -47
  73. package/src/types/modules.d.ts +5 -2
  74. package/dist/rehype/remark-math-speech.d.ts +0 -13
  75. package/dist/rehype/remark-math-speech.d.ts.map +0 -1
  76. package/dist/rehype/remark-math-speech.js +0 -26
  77. package/dist/rehype/remark-math-speech.js.map +0 -1
  78. package/src/rehype/remark-math-speech.ts +0 -31
@@ -7,8 +7,8 @@
7
7
 
8
8
  // biome-ignore lint/suspicious/noExplicitAny: speech-rule-engine is an optional peer dep with no bundled types
9
9
  let SRE: any = null;
10
- let tex2mml: ((latex: string) => string) | null = null;
11
- let engineSetup = false;
10
+ let tex2mml: ((latex: string, display: boolean) => string) | null = null;
11
+ let lastEngineKey = "";
12
12
  let initPromise: Promise<boolean> | null = null;
13
13
 
14
14
  /**
@@ -20,26 +20,25 @@ async function init(): Promise<boolean> {
20
20
 
21
21
  initPromise = (async () => {
22
22
  try {
23
- // Dynamically import speech-rule-engine
24
- SRE = await import("speech-rule-engine");
23
+ // Dynamically import speech-rule-engine. Normalize CJS/ESM interop:
24
+ // bun hoists named exports onto the namespace, but Vite's SSR loader
25
+ // (used during `astro build`) puts them under `.default`.
26
+ // biome-ignore lint/suspicious/noExplicitAny: optional peer dep, no bundled types
27
+ const sreMod: any = await import("speech-rule-engine");
28
+ SRE = sreMod.default ?? sreMod;
25
29
  } catch {
26
30
  console.warn("[vocasync] speech-rule-engine not installed. Math-to-speech disabled.");
27
31
  return false;
28
32
  }
29
33
 
30
34
  try {
31
- // Dynamically import mathjax-full components for tex2mml
32
- // @ts-expect-error - mathjax-full is an optional peer dependency
35
+ // Dynamically import mathjax-full components for tex2mml.
33
36
  const { TeX } = await import("mathjax-full/js/input/tex.js");
34
- // @ts-expect-error - mathjax-full is an optional peer dependency
35
37
  const { HTMLDocument } = await import("mathjax-full/js/handlers/html/HTMLDocument.js");
36
- // @ts-expect-error - mathjax-full is an optional peer dependency
37
38
  const { liteAdaptor } = await import("mathjax-full/js/adaptors/liteAdaptor.js");
38
39
  const { SerializedMmlVisitor } = await import(
39
- // @ts-expect-error - mathjax-full is an optional peer dependency
40
40
  "mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js"
41
41
  );
42
- // @ts-expect-error - mathjax-full is an optional peer dependency
43
42
  const { AllPackages } = await import("mathjax-full/js/input/tex/AllPackages.js");
44
43
 
45
44
  // Filter out problematic packages
@@ -55,16 +54,16 @@ async function init(): Promise<boolean> {
55
54
  // Create a visitor to serialize to MathML
56
55
  const visitor = new SerializedMmlVisitor();
57
56
 
58
- // Create the tex2mml function
59
- tex2mml = (latex: string): string => {
60
- const node = doc.convert(latex, { display: true });
57
+ // Create the tex2mml function — display flag controls MathML output
58
+ tex2mml = (latex: string, display: boolean): string => {
59
+ const node = doc.convert(latex, { display });
61
60
  return visitor.visitTree(node);
62
61
  };
63
62
 
64
63
  return true;
65
64
  } catch {
66
65
  console.warn("[vocasync] mathjax-full not installed. Math-to-speech disabled.");
67
- console.warn("[vocasync] Install with: npm install mathjax-full speech-rule-engine");
66
+ console.warn("[vocasync] Install with: bun add mathjax-full speech-rule-engine");
68
67
  return false;
69
68
  }
70
69
  })();
@@ -83,14 +82,25 @@ export interface SREOptions {
83
82
  speech?: string;
84
83
  }
85
84
 
85
+ /**
86
+ * A single LaTeX expression with its display context.
87
+ */
88
+ export interface LaTeXExpression {
89
+ latex: string;
90
+ display: boolean;
91
+ }
92
+
86
93
  /**
87
94
  * Convert an array of LaTeX expressions to speech.
88
95
  *
89
- * @param exprs - Array of LaTeX expressions to convert
96
+ * @param exprs - Array of LaTeX expressions (with display flag) to convert
90
97
  * @param options - SRE configuration options
91
98
  * @returns Promise resolving to array of speech strings
92
99
  */
93
- export async function latexToSpeech(exprs: string[], options: SREOptions = {}): Promise<string[]> {
100
+ export async function latexToSpeech(
101
+ exprs: LaTeXExpression[],
102
+ options: SREOptions = {}
103
+ ): Promise<string[]> {
94
104
  const available = await init();
95
105
 
96
106
  if (!available || !SRE || !tex2mml) {
@@ -98,23 +108,23 @@ export async function latexToSpeech(exprs: string[], options: SREOptions = {}):
98
108
  return exprs.map(() => "");
99
109
  }
100
110
 
101
- // Set up the engine if not already done
102
- if (!engineSetup) {
111
+ // Reconfigure the engine when options change
112
+ const engineKey = `${options.locale || "en"}:${options.domain || "mathspeak"}:${options.style || "default"}`;
113
+ if (engineKey !== lastEngineKey) {
103
114
  await SRE.setupEngine({
104
115
  locale: options.locale || "en",
105
116
  domain: options.domain || "mathspeak",
106
117
  style: options.style || "default",
107
118
  modality: options.modality || "speech",
108
119
  speech: options.speech || "deep",
109
- ...options,
110
120
  });
111
- engineSetup = true;
121
+ lastEngineKey = engineKey;
112
122
  }
113
123
 
114
124
  // Convert each expression
115
- return exprs.map((latex) => {
125
+ return exprs.map(({ latex, display }) => {
116
126
  try {
117
- const mml = tex2mml!(latex);
127
+ const mml = tex2mml!(latex, display);
118
128
  return SRE!.toSpeech(mml);
119
129
  } catch {
120
130
  console.warn(`[vocasync] Failed to convert LaTeX to speech: ${latex}`);
@@ -0,0 +1,55 @@
1
+ import { latexToSpeech } from "./index.js";
2
+
3
+ const cache = new Map<string, Promise<string>>();
4
+ let warnedFailure = false;
5
+
6
+ function normalizeWhitespace(value = ""): string {
7
+ return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
8
+ }
9
+
10
+ /**
11
+ * Convert a LaTeX expression to spoken text.
12
+ *
13
+ * `latexToSpeech` is imported statically (it lazy-loads the optional
14
+ * speech-rule-engine / mathjax-full deps internally), which is robust under
15
+ * Astro's Vite SSR loader where a lazy `import()` of this module returned a
16
+ * namespace without the expected named export.
17
+ *
18
+ * Falls back to the raw LaTeX if the math dependencies are unavailable or
19
+ * conversion fails, so a build never crashes on math content.
20
+ */
21
+ export async function speakLatex(
22
+ latex: string,
23
+ display: boolean,
24
+ style = "clearspeak"
25
+ ): Promise<string> {
26
+ const trimmed = String(latex ?? "").trim();
27
+ if (!trimmed) return "";
28
+
29
+ const cacheKey = `${style}:${display}:${trimmed}`;
30
+ if (!cache.has(cacheKey)) {
31
+ const pending = (async () => {
32
+ try {
33
+ const result = await latexToSpeech([{ latex: trimmed, display }], {
34
+ domain: style,
35
+ style: "default",
36
+ locale: "en",
37
+ modality: "speech",
38
+ });
39
+ const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
40
+ return normalizeWhitespace(spoken) || trimmed;
41
+ } catch (error) {
42
+ if (!warnedFailure) {
43
+ console.warn(
44
+ "[vocasync] Failed to convert LaTeX to speech:",
45
+ error instanceof Error ? error.message : error
46
+ );
47
+ warnedFailure = true;
48
+ }
49
+ return normalizeWhitespace(trimmed);
50
+ }
51
+ })();
52
+ cache.set(cacheKey, pending);
53
+ }
54
+ return cache.get(cacheKey)!;
55
+ }