@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
@@ -0,0 +1,279 @@
1
+ /**
2
+ * Spoken-form normalization primitives.
3
+ *
4
+ * Ported from the Valeon blog's audio pipeline so the plugin's
5
+ * build-time tokenization reproduces VocaSync's alignment token stream
6
+ * exactly. Two transforms:
7
+ *
8
+ * expandSpokenForms(text) — expands currencies, numbers, percentages
9
+ * into spoken word equivalents. Preserves
10
+ * punctuation/casing so it reads naturally
11
+ * for TTS. This is the `speechtext` POSTed
12
+ * to synthesis.
13
+ *
14
+ * stripForAlignment(text) — derives the alignment-friendly form:
15
+ * lowercase, punctuation removed, whitespace
16
+ * collapsed, contractions preserved. This is
17
+ * the `normalisedSpeechtext` POSTed as the
18
+ * alignment transcript AND the basis for the
19
+ * rehype plugin's `data-i`/`data-n` indices.
20
+ *
21
+ * Because the alignment transcript IS `stripForAlignment(expandSpokenForms(text))`,
22
+ * the worker's own normalizer is a no-op over it and word indices line up
23
+ * token-for-token with the rehype output.
24
+ */
25
+ const CURRENCY_SYMBOL_WORDS = {
26
+ "£": "pound",
27
+ $: "dollar",
28
+ "€": "euro",
29
+ "¥": "yen",
30
+ "₹": "rupee",
31
+ "₽": "ruble",
32
+ "₩": "won",
33
+ "₪": "shekel",
34
+ "₺": "lira",
35
+ "฿": "baht",
36
+ "₫": "dong",
37
+ "₴": "hryvnia",
38
+ "₦": "naira",
39
+ "₱": "peso",
40
+ "₲": "guarani",
41
+ "₡": "colon",
42
+ "₭": "kip",
43
+ "₮": "tugrik",
44
+ "₿": "bitcoin",
45
+ "₣": "franc",
46
+ "¢": "cent",
47
+ "₤": "lira",
48
+ };
49
+ const CURRENCY_SYMBOL_LIST = Object.keys(CURRENCY_SYMBOL_WORDS);
50
+ const CURRENCY_SYMBOL_CLASS = CURRENCY_SYMBOL_LIST.join("");
51
+ const CURRENCY_SYMBOL_RANGE = `[${CURRENCY_SYMBOL_CLASS}]`;
52
+ const CURRENCY_SYMBOL_BEFORE_NUMBER_REGEX = new RegExp(`(${CURRENCY_SYMBOL_RANGE})\\s*([0-9]+(?:[.,][0-9]+)*)`, "gu");
53
+ const CURRENCY_SYMBOL_AFTER_NUMBER_REGEX = new RegExp(`([0-9]+(?:[.,][0-9]+)*)\\s*(${CURRENCY_SYMBOL_RANGE})`, "gu");
54
+ const CURRENCY_SYMBOL_REGEX = new RegExp(CURRENCY_SYMBOL_RANGE, "gu");
55
+ const DIGIT_WORDS = [
56
+ "zero",
57
+ "one",
58
+ "two",
59
+ "three",
60
+ "four",
61
+ "five",
62
+ "six",
63
+ "seven",
64
+ "eight",
65
+ "nine",
66
+ ];
67
+ const SMALL_WORDS = [
68
+ "zero",
69
+ "one",
70
+ "two",
71
+ "three",
72
+ "four",
73
+ "five",
74
+ "six",
75
+ "seven",
76
+ "eight",
77
+ "nine",
78
+ "ten",
79
+ "eleven",
80
+ "twelve",
81
+ "thirteen",
82
+ "fourteen",
83
+ "fifteen",
84
+ "sixteen",
85
+ "seventeen",
86
+ "eighteen",
87
+ "nineteen",
88
+ ];
89
+ const TENS_WORDS = [
90
+ "",
91
+ "",
92
+ "twenty",
93
+ "thirty",
94
+ "forty",
95
+ "fifty",
96
+ "sixty",
97
+ "seventy",
98
+ "eighty",
99
+ "ninety",
100
+ ];
101
+ const SCALE_WORDS = [
102
+ "",
103
+ "thousand",
104
+ "million",
105
+ "billion",
106
+ "trillion",
107
+ "quadrillion",
108
+ "quintillion",
109
+ "sextillion",
110
+ ];
111
+ function threeDigitWords(value) {
112
+ const parts = [];
113
+ let v = value;
114
+ if (v >= 100) {
115
+ parts.push(SMALL_WORDS[Math.floor(v / 100)]);
116
+ parts.push("hundred");
117
+ v %= 100;
118
+ }
119
+ if (v >= 20) {
120
+ parts.push(TENS_WORDS[Math.floor(v / 10)]);
121
+ v %= 10;
122
+ if (v > 0)
123
+ parts.push(SMALL_WORDS[v]);
124
+ }
125
+ else if (v > 0 || parts.length === 0) {
126
+ parts.push(SMALL_WORDS[v]);
127
+ }
128
+ return parts.join(" ").trim();
129
+ }
130
+ const BIG_ZERO = BigInt(0);
131
+ const BIG_THOUSAND = BigInt(1000);
132
+ function integerToWords(value) {
133
+ if (value === BIG_ZERO)
134
+ return "zero";
135
+ const parts = [];
136
+ let remaining = value;
137
+ let scale = 0;
138
+ while (remaining > BIG_ZERO) {
139
+ const chunk = Number(remaining % BIG_THOUSAND);
140
+ if (chunk > 0) {
141
+ const words = threeDigitWords(chunk);
142
+ const scaleWord = SCALE_WORDS[scale];
143
+ parts.unshift(scaleWord ? `${words} ${scaleWord}`.trim() : words);
144
+ }
145
+ remaining /= BIG_THOUSAND;
146
+ scale += 1;
147
+ }
148
+ return parts.join(" ").trim();
149
+ }
150
+ function expandNumberToken(token) {
151
+ let working = token;
152
+ const result = [];
153
+ let negative = false;
154
+ if (working.startsWith("-")) {
155
+ negative = true;
156
+ working = working.slice(1);
157
+ }
158
+ if (!working)
159
+ working = "0";
160
+ const [intPart, decPart] = working.split(".");
161
+ const integerValue = intPart ? BigInt(intPart) : BIG_ZERO;
162
+ const integerWords = integerToWords(integerValue);
163
+ if (negative)
164
+ result.push("minus");
165
+ result.push(integerWords);
166
+ if (decPart) {
167
+ result.push("point");
168
+ for (const digit of decPart) {
169
+ const value = Number(digit);
170
+ if (!Number.isNaN(value) && value >= 0 && value <= 9) {
171
+ result.push(DIGIT_WORDS[value]);
172
+ }
173
+ }
174
+ }
175
+ return result.join(" ");
176
+ }
177
+ function expandNumbers(input) {
178
+ return input.replace(/-?\d+(\.\d+)?/g, expandNumberToken);
179
+ }
180
+ function expandCurrencySymbols(input) {
181
+ let text = input.replace(CURRENCY_SYMBOL_BEFORE_NUMBER_REGEX, (_, symbol, digits) => {
182
+ const word = CURRENCY_SYMBOL_WORDS[symbol];
183
+ return word ? `${digits} ${word}` : `${symbol}${digits}`;
184
+ });
185
+ text = text.replace(CURRENCY_SYMBOL_AFTER_NUMBER_REGEX, (_, digits, symbol) => {
186
+ const word = CURRENCY_SYMBOL_WORDS[symbol];
187
+ return word ? `${digits} ${word}` : `${digits}${symbol}`;
188
+ });
189
+ return text.replace(CURRENCY_SYMBOL_REGEX, (symbol) => {
190
+ const word = CURRENCY_SYMBOL_WORDS[symbol];
191
+ return word ? ` ${word} ` : " ";
192
+ });
193
+ }
194
+ function expandPercents(input) {
195
+ // Match number-followed-by-% (with optional space). "50%" -> "50 percent".
196
+ // Runs before number expansion so the digits still parse cleanly.
197
+ return input.replace(/([0-9]+(?:\.[0-9]+)?)\s*%/g, "$1 percent");
198
+ }
199
+ /**
200
+ * Expand currency symbols, numbers, and percentages into their spoken
201
+ * word forms. Preserves punctuation and surrounding text — this is the
202
+ * surface that becomes `speechtext`.
203
+ */
204
+ export function expandSpokenForms(input) {
205
+ let text = input.normalize("NFKC");
206
+ text = expandPercents(text);
207
+ text = expandCurrencySymbols(text);
208
+ text = text.replace(/(?<=\d),(?=\d)/g, "");
209
+ text = expandNumbers(text);
210
+ // Collapse any double-spaces introduced by symbol expansion.
211
+ return text.replace(/[ \t]+/g, " ");
212
+ }
213
+ /**
214
+ * Strip a speechtext-form string into the alignment form: lowercase,
215
+ * punctuation removed, whitespace collapsed. Contractions are preserved
216
+ * (e.g. "don't" stays one token).
217
+ */
218
+ export function stripForAlignment(input) {
219
+ let text = input.normalize("NFKC");
220
+ // Strip control (C0/C1) + DEL, then zero-width / BiDi formatting chars.
221
+ // Code-point filtering avoids embedding raw control bytes in a regex literal.
222
+ text = Array.from(text)
223
+ .map((ch) => {
224
+ const c = ch.codePointAt(0) ?? 0;
225
+ if (c <= 0x1f || (c >= 0x7f && c <= 0x9f))
226
+ return " ";
227
+ if (c === 0xa0)
228
+ return " ";
229
+ if ((c >= 0x200b && c <= 0x200f) ||
230
+ (c >= 0x202a && c <= 0x202e) ||
231
+ (c >= 0x2060 && c <= 0x2064)) {
232
+ return "";
233
+ }
234
+ return ch;
235
+ })
236
+ .join("");
237
+ // Quote / dash normalisation.
238
+ text = text.replace(/[“”„‟]/g, '"');
239
+ text = text.replace(/[‘’‚‛]/g, "'");
240
+ text = text.replace(/…/g, " ");
241
+ text = text.replace(/[-‐‑⁃–—−]/g, " ");
242
+ // Strip emoji + URLs + emails + handles before the catch-all punctuation pass.
243
+ text = text.replace(/\p{Extended_Pictographic}/gu, " ");
244
+ text = text.replace(/https?:\/\/\S+/gi, " ");
245
+ text = text.replace(/\bwww\.[^\s/]+\b/gi, " ");
246
+ text = text.replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/gi, " ");
247
+ text = text.replace(/@\w+/g, " ");
248
+ // NFKD-then-strip-marks to drop combining diacritics.
249
+ text = text.normalize("NFKD").replace(/\p{M}/gu, "").normalize("NFKC");
250
+ // Strip stray apostrophes that aren't part of a contraction.
251
+ text = text.replace(/(^|[^A-Za-z0-9])'(?=[^A-Za-z0-9]|$)/g, "$1");
252
+ text = text.replace(/(^|[^A-Za-z0-9])'(?=[A-Za-z0-9])/g, "$1");
253
+ // Everything that's not a contraction-letter, digit, or whitespace -> space.
254
+ text = text.replace(/[^A-Za-z0-9'\s]/g, " ");
255
+ text = text.replace(/\s+/g, " ").trim();
256
+ return text.toLowerCase();
257
+ }
258
+ /**
259
+ * Produce both the speechtext (POSTed to synthesis) and
260
+ * normalisedSpeechtext (POSTed as the alignment transcript) forms from a
261
+ * single plain-text input.
262
+ */
263
+ export function buildSpeechtextPair(plainText) {
264
+ const speechtext = expandSpokenForms(plainText);
265
+ const normalisedSpeechtext = stripForAlignment(speechtext);
266
+ return { speechtext, normalisedSpeechtext };
267
+ }
268
+ /**
269
+ * How many alignment tokens a visible string expands to. Used by the
270
+ * rehype plugin to compute `data-n` for each wrapped word / math unit.
271
+ * "$50" -> 2 ("fifty dollar"); "1995" -> 6; unambiguous words -> 1.
272
+ */
273
+ export function spokenTokenCount(visible) {
274
+ const normalised = stripForAlignment(expandSpokenForms(visible));
275
+ if (!normalised)
276
+ return 0;
277
+ return normalised.split(/\s+/).filter(Boolean).length;
278
+ }
279
+ //# sourceMappingURL=spoken-form.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spoken-form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,OAAe;IAC9C,MAAM,UAAU,GAAG,iBAAiB,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC;IACjE,IAAI,CAAC,UAAU;QAAE,OAAO,CAAC,CAAC;IAC1B,OAAO,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC;AACxD,CAAC"}
@@ -14,13 +14,20 @@ export interface SREOptions {
14
14
  modality?: string;
15
15
  speech?: string;
16
16
  }
17
+ /**
18
+ * A single LaTeX expression with its display context.
19
+ */
20
+ export interface LaTeXExpression {
21
+ latex: string;
22
+ display: boolean;
23
+ }
17
24
  /**
18
25
  * Convert an array of LaTeX expressions to speech.
19
26
  *
20
- * @param exprs - Array of LaTeX expressions to convert
27
+ * @param exprs - Array of LaTeX expressions (with display flag) to convert
21
28
  * @param options - SRE configuration options
22
29
  * @returns Promise resolving to array of speech strings
23
30
  */
24
- export declare function latexToSpeech(exprs: string[], options?: SREOptions): Promise<string[]>;
31
+ export declare function latexToSpeech(exprs: LaTeXExpression[], options?: SREOptions): Promise<string[]>;
25
32
  export default latexToSpeech;
26
33
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAqEH;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CA+BhG;AAED,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAoEH;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,KAAK,EAAE,eAAe,EAAE,EACxB,OAAO,GAAE,UAAe,GACvB,OAAO,CAAC,MAAM,EAAE,CAAC,CA+BnB;AAED,eAAe,aAAa,CAAC"}
@@ -7,7 +7,7 @@
7
7
  // biome-ignore lint/suspicious/noExplicitAny: speech-rule-engine is an optional peer dep with no bundled types
8
8
  let SRE = null;
9
9
  let tex2mml = null;
10
- let engineSetup = false;
10
+ let lastEngineKey = "";
11
11
  let initPromise = null;
12
12
  /**
13
13
  * Initialize the speech-rule-engine and MathJax tex2mml converter.
@@ -18,25 +18,23 @@ async function init() {
18
18
  return initPromise;
19
19
  initPromise = (async () => {
20
20
  try {
21
- // Dynamically import speech-rule-engine
22
- SRE = await import("speech-rule-engine");
21
+ // Dynamically import speech-rule-engine. Normalize CJS/ESM interop:
22
+ // bun hoists named exports onto the namespace, but Vite's SSR loader
23
+ // (used during `astro build`) puts them under `.default`.
24
+ // biome-ignore lint/suspicious/noExplicitAny: optional peer dep, no bundled types
25
+ const sreMod = await import("speech-rule-engine");
26
+ SRE = sreMod.default ?? sreMod;
23
27
  }
24
28
  catch {
25
29
  console.warn("[vocasync] speech-rule-engine not installed. Math-to-speech disabled.");
26
30
  return false;
27
31
  }
28
32
  try {
29
- // Dynamically import mathjax-full components for tex2mml
30
- // @ts-expect-error - mathjax-full is an optional peer dependency
33
+ // Dynamically import mathjax-full components for tex2mml.
31
34
  const { TeX } = await import("mathjax-full/js/input/tex.js");
32
- // @ts-expect-error - mathjax-full is an optional peer dependency
33
35
  const { HTMLDocument } = await import("mathjax-full/js/handlers/html/HTMLDocument.js");
34
- // @ts-expect-error - mathjax-full is an optional peer dependency
35
36
  const { liteAdaptor } = await import("mathjax-full/js/adaptors/liteAdaptor.js");
36
- const { SerializedMmlVisitor } = await import(
37
- // @ts-expect-error - mathjax-full is an optional peer dependency
38
- "mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
39
- // @ts-expect-error - mathjax-full is an optional peer dependency
37
+ const { SerializedMmlVisitor } = await import("mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
40
38
  const { AllPackages } = await import("mathjax-full/js/input/tex/AllPackages.js");
41
39
  // Filter out problematic packages
42
40
  const packages = AllPackages.filter((p) => p !== "bussproofs");
@@ -47,16 +45,16 @@ async function init() {
47
45
  const doc = new HTMLDocument("", adaptor, { InputJax: texInput });
48
46
  // Create a visitor to serialize to MathML
49
47
  const visitor = new SerializedMmlVisitor();
50
- // Create the tex2mml function
51
- tex2mml = (latex) => {
52
- const node = doc.convert(latex, { display: true });
48
+ // Create the tex2mml function — display flag controls MathML output
49
+ tex2mml = (latex, display) => {
50
+ const node = doc.convert(latex, { display });
53
51
  return visitor.visitTree(node);
54
52
  };
55
53
  return true;
56
54
  }
57
55
  catch {
58
56
  console.warn("[vocasync] mathjax-full not installed. Math-to-speech disabled.");
59
- console.warn("[vocasync] Install with: npm install mathjax-full speech-rule-engine");
57
+ console.warn("[vocasync] Install with: bun add mathjax-full speech-rule-engine");
60
58
  return false;
61
59
  }
62
60
  })();
@@ -65,7 +63,7 @@ async function init() {
65
63
  /**
66
64
  * Convert an array of LaTeX expressions to speech.
67
65
  *
68
- * @param exprs - Array of LaTeX expressions to convert
66
+ * @param exprs - Array of LaTeX expressions (with display flag) to convert
69
67
  * @param options - SRE configuration options
70
68
  * @returns Promise resolving to array of speech strings
71
69
  */
@@ -75,22 +73,22 @@ export async function latexToSpeech(exprs, options = {}) {
75
73
  // Return empty descriptions when dependencies not available
76
74
  return exprs.map(() => "");
77
75
  }
78
- // Set up the engine if not already done
79
- if (!engineSetup) {
76
+ // Reconfigure the engine when options change
77
+ const engineKey = `${options.locale || "en"}:${options.domain || "mathspeak"}:${options.style || "default"}`;
78
+ if (engineKey !== lastEngineKey) {
80
79
  await SRE.setupEngine({
81
80
  locale: options.locale || "en",
82
81
  domain: options.domain || "mathspeak",
83
82
  style: options.style || "default",
84
83
  modality: options.modality || "speech",
85
84
  speech: options.speech || "deep",
86
- ...options,
87
85
  });
88
- engineSetup = true;
86
+ lastEngineKey = engineKey;
89
87
  }
90
88
  // Convert each expression
91
- return exprs.map((latex) => {
89
+ return exprs.map(({ latex, display }) => {
92
90
  try {
93
- const mml = tex2mml(latex);
91
+ const mml = tex2mml(latex, display);
94
92
  return SRE.toSpeech(mml);
95
93
  }
96
94
  catch {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,+GAA+G;AAC/G,IAAI,GAAG,GAAQ,IAAI,CAAC;AACpB,IAAI,OAAO,GAAuC,IAAI,CAAC;AACvD,IAAI,WAAW,GAAG,KAAK,CAAC;AACxB,IAAI,WAAW,GAA4B,IAAI,CAAC;AAEhD;;;GAGG;AACH,KAAK,UAAU,IAAI;IACjB,IAAI,WAAW;QAAE,OAAO,WAAW,CAAC;IAEpC,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE;QACxB,IAAI,CAAC;YACH,wCAAwC;YACxC,GAAG,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;QAC3C,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,IAAI,CAAC,uEAAuE,CAAC,CAAC;YACtF,O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AS,GAAG,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,OAAO,CAAC,MAAM,IAAI,WAAW,IAAI,OAAO,CAAC,KAAK,IAAI,SAAS,EAAE,CAAC;IAC7G,IAAI,SAAS,KAAK,aAAa,EAAE,CAAC;QAChC,MAAM,GAAG,CAAC,WAAW,CAAC;YACpB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,IAAI;YAC9B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,WAAW;YACrC,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,SAAS;YACjC,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,QAAQ;YACtC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,MAAM;SACjC,CAAC,CAAC;QACH,aAAa,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,0BAA0B;IAC1B,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE;QACtC,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,OAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACrC,OAAO,GAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,IAAI,CAAC,iDAAiD,KAAK,EAAE,CAAC,CAAC;YACvE,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED,eAAe,aAAa,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Convert a LaTeX expression to spoken text.
3
+ *
4
+ * `latexToSpeech` is imported statically (it lazy-loads the optional
5
+ * speech-rule-engine / mathjax-full deps internally), which is robust under
6
+ * Astro's Vite SSR loader where a lazy `import()` of this module returned a
7
+ * namespace without the expected named export.
8
+ *
9
+ * Falls back to the raw LaTeX if the math dependencies are unavailable or
10
+ * conversion fails, so a build never crashes on math content.
11
+ */
12
+ export declare function speakLatex(latex: string, display: boolean, style?: string): Promise<string>;
13
+ //# sourceMappingURL=speak.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"speak.d.ts","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"AASA;;;;;;;;;;GAUG;AACH,wBAAsB,UAAU,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,KAAK,SAAe,GACnB,OAAO,CAAC,MAAM,CAAC,CA8BjB"}
@@ -0,0 +1,47 @@
1
+ import { latexToSpeech } from "./index.js";
2
+ const cache = new Map();
3
+ let warnedFailure = false;
4
+ function normalizeWhitespace(value = "") {
5
+ return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
6
+ }
7
+ /**
8
+ * Convert a LaTeX expression to spoken text.
9
+ *
10
+ * `latexToSpeech` is imported statically (it lazy-loads the optional
11
+ * speech-rule-engine / mathjax-full deps internally), which is robust under
12
+ * Astro's Vite SSR loader where a lazy `import()` of this module returned a
13
+ * namespace without the expected named export.
14
+ *
15
+ * Falls back to the raw LaTeX if the math dependencies are unavailable or
16
+ * conversion fails, so a build never crashes on math content.
17
+ */
18
+ export async function speakLatex(latex, display, style = "clearspeak") {
19
+ const trimmed = String(latex ?? "").trim();
20
+ if (!trimmed)
21
+ return "";
22
+ const cacheKey = `${style}:${display}:${trimmed}`;
23
+ if (!cache.has(cacheKey)) {
24
+ const pending = (async () => {
25
+ try {
26
+ const result = await latexToSpeech([{ latex: trimmed, display }], {
27
+ domain: style,
28
+ style: "default",
29
+ locale: "en",
30
+ modality: "speech",
31
+ });
32
+ const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
33
+ return normalizeWhitespace(spoken) || trimmed;
34
+ }
35
+ catch (error) {
36
+ if (!warnedFailure) {
37
+ console.warn("[vocasync] Failed to convert LaTeX to speech:", error instanceof Error ? error.message : error);
38
+ warnedFailure = true;
39
+ }
40
+ return normalizeWhitespace(trimmed);
41
+ }
42
+ })();
43
+ cache.set(cacheKey, pending);
44
+ }
45
+ return cache.get(cacheKey);
46
+ }
47
+ //# sourceMappingURL=speak.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"speak.js","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;AACjD,IAAI,aAAa,GAAG,KAAK,CAAC;AAE1B,SAAS,mBAAmB,CAAC,KAAK,GAAG,EAAE;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,KAAa,EACb,OAAgB,EAChB,KAAK,GAAG,YAAY;IAEpB,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IAExB,MAAM,QAAQ,GAAG,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE,CAAC;IAClD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;YAC1B,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;oBAChE,MAAM,EAAE,KAAK;oBACb,KAAK,EAAE,SAAS;oBAChB,MAAM,EAAE,IAAI;oBACZ,QAAQ,EAAE,QAAQ;iBACnB,CAAC,CAAC;gBACH,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;gBACxE,OAAO,mBAAmB,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC;YAChD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,aAAa,EAAE,CAAC;oBACnB,OAAO,CAAC,IAAI,CACV,+CAA+C,EAC/C,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAC/C,CAAC;oBACF,aAAa,GAAG,IAAI,CAAC;gBACvB,CAAC;gBACD,OAAO,mBAAmB,CAAC,OAAO,CAAC,CAAC;YACtC,CAAC;QACH,CAAC,CAAC,EAAE,CAAC;QACL,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC;AAC9B,CAAC"}
@@ -5,44 +5,34 @@ import type { Plugin } from "unified";
5
5
  */
6
6
  export interface RehypeAudioWordsOptions {
7
7
  /**
8
- * Path to the audio map JSON file.
9
- * Can be absolute or relative to the project root.
8
+ * Path to the audio map JSON file (absolute or relative to project root).
10
9
  * @default ".vocasync/audio-map.json"
11
10
  */
12
11
  audioMapPath?: string;
13
12
  /**
14
- * CSS class prefix for generated elements.
15
- * @default "vocasync"
13
+ * CSS class for generated word spans.
14
+ * @default "vocasync" (spans get the class `${classPrefix}-word`)
16
15
  */
17
16
  classPrefix?: string;
18
17
  /**
19
- * The name of the content collection folder to extract slugs from.
18
+ * Content collection folder name used to extract slugs.
20
19
  * @default "articles"
21
20
  */
22
21
  collectionName?: string;
23
22
  }
24
23
  /**
25
- * Rehype plugin that wraps words with timing spans for audio sync.
24
+ * Rehype plugin that wraps each visible word in
25
+ * `<span class="vocasync-word" data-i="N" data-n="K">…</span>`:
26
26
  *
27
- * For each text node in the document, this plugin:
28
- * 1. Tokenizes the text into words
29
- * 2. Matches words against alignment data
30
- * 3. Wraps matched words in <span> elements with data-start and data-end attributes
27
+ * data-i = the word's starting index in the alignment word stream
28
+ * (the normalisedSpeechtext POSTed to VocaSync).
29
+ * data-n = how many alignment tokens this visible word expands to
30
+ * (via spokenTokenCount): "$50" -> 2, "1995" -> 6, words -> 1.
31
31
  *
32
- * @example
33
- * ```typescript
34
- * // astro.config.mjs
35
- * import { defineConfig } from "astro/config";
36
- * import { rehypeAudioWords } from "@vocasync/astro/rehype";
37
- *
38
- * export default defineConfig({
39
- * markdown: {
40
- * rehypePlugins: [
41
- * [rehypeAudioWords, { collectionName: "articles" }]
42
- * ]
43
- * }
44
- * });
45
- * ```
32
+ * No alignment data is fetched here — tokenization is deterministic and runs
33
+ * the same `spokenTokenCount` used to build the transcript, so indices line
34
+ * up token-for-token. Math containers carrying `data-speech` (attached by
35
+ * rehypeMathSpeech) are wrapped as a single unit with their token count.
46
36
  */
47
37
  declare const rehypeAudioWords: Plugin<[RehypeAudioWordsOptions?], Root>;
48
38
  export default rehypeAudioWords;
@@ -1 +1 @@
1
- {"version":3,"file":"audio-words.d.ts","sourceRoot":"","sources":["../../src/rehype/audio-words.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAW,IAAI,EAAQ,MAAM,MAAM,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAItC;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AA6ED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,QAAA,MAAM,gBAAgB,EAAE,MAAM,CAAC,CAAC,uBAAuB,CAAC,CAAC,EAAE,IAAI,CA4F9D,CAAC;AA0IF,eAAe,gBAAgB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"audio-words.d.ts","sourceRoot":"","sources":["../../src/rehype/audio-words.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,IAAI,EAAQ,MAAM,MAAM,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAuBD;;;;;;;;;;;;;GAaG;AACH,QAAA,MAAM,gBAAgB,EAAE,MAAM,CAAC,CAAC,uBAAuB,CAAC,CAAC,EAAE,IAAI,CAmF9D,CAAC;AAqDF,eAAe,gBAAgB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,CAAC"}