@vocasync/astro 0.1.8 → 0.1.10
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.
- package/README.md +9 -6
- package/dist/components/index.d.ts +14 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/core/speech-builder.d.ts.map +1 -1
- package/dist/core/speech-builder.js +7 -58
- package/dist/core/speech-builder.js.map +1 -1
- package/dist/lib/latex-to-speech/index.d.ts +9 -2
- package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
- package/dist/lib/latex-to-speech/index.js +11 -11
- package/dist/lib/latex-to-speech/index.js.map +1 -1
- package/dist/lib/latex-to-speech/speak.d.ts +8 -0
- package/dist/lib/latex-to-speech/speak.d.ts.map +1 -0
- package/dist/lib/latex-to-speech/speak.js +64 -0
- package/dist/lib/latex-to-speech/speak.js.map +1 -0
- package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
- package/dist/rehype/rehype-math-speech.js +5 -54
- package/dist/rehype/rehype-math-speech.js.map +1 -1
- package/dist/rehype/remark-math-speech.d.ts +3 -2
- package/dist/rehype/remark-math-speech.d.ts.map +1 -1
- package/dist/rehype/remark-math-speech.js +3 -2
- package/dist/rehype/remark-math-speech.js.map +1 -1
- package/package.json +1 -1
- package/src/components/index.ts +10 -0
- package/src/core/speech-builder.ts +7 -68
- package/src/lib/latex-to-speech/index.ts +24 -13
- package/src/lib/latex-to-speech/speak.ts +78 -0
- package/src/rehype/rehype-math-speech.ts +8 -66
- package/src/rehype/remark-math-speech.ts +3 -2
- package/src/types/modules.d.ts +5 -2
package/README.md
CHANGED
|
@@ -115,14 +115,16 @@ In your article layout or page:
|
|
|
115
115
|
```astro
|
|
116
116
|
---
|
|
117
117
|
// src/layouts/ArticleLayout.astro
|
|
118
|
-
import
|
|
118
|
+
import AudioPlayer from "@vocasync/astro/components/AudioPlayer.astro";
|
|
119
|
+
import audioMap from "../data/audio-map.json";
|
|
119
120
|
|
|
120
121
|
const { post } = Astro.props;
|
|
122
|
+
const audioEntry = audioMap.entries[post.slug];
|
|
121
123
|
---
|
|
122
124
|
|
|
123
125
|
<article>
|
|
124
126
|
<!-- Audio player at the top -->
|
|
125
|
-
<AudioPlayer slug={post.slug} label="Listen to this post" />
|
|
127
|
+
<AudioPlayer slug={post.slug} audioEntry={audioEntry} label="Listen to this post" />
|
|
126
128
|
|
|
127
129
|
<!-- Article content - must have data-article-body for word highlighting -->
|
|
128
130
|
<div data-article-body>
|
|
@@ -257,7 +259,7 @@ The main audio player component with word highlighting support.
|
|
|
257
259
|
|
|
258
260
|
```astro
|
|
259
261
|
---
|
|
260
|
-
import
|
|
262
|
+
import AudioPlayer from "@vocasync/astro/components/AudioPlayer.astro";
|
|
261
263
|
---
|
|
262
264
|
|
|
263
265
|
<AudioPlayer
|
|
@@ -276,7 +278,8 @@ import { AudioPlayer } from "@vocasync/astro/components";
|
|
|
276
278
|
| Prop | Type | Default | Description |
|
|
277
279
|
|------|------|---------|-------------|
|
|
278
280
|
| `slug` | `string` | required | Post slug to lookup audio |
|
|
279
|
-
| `
|
|
281
|
+
| `audioEntry` | `object` | `undefined` | Audio entry from audio-map.json (audioUrl, publishableKey, etc.) |
|
|
282
|
+
| `label` | `string` | `"Listen to this article"` | Accessible label |
|
|
280
283
|
| `showPlaceholder` | `boolean` | `true` | Show message when no audio |
|
|
281
284
|
| `class` | `string` | `""` | Additional CSS classes |
|
|
282
285
|
| `articleSelector` | `string` | `"[data-article-body]"` | Selector for word highlighting container |
|
|
@@ -343,10 +346,10 @@ VocaSync supports LaTeX math equations using [Speech Rule Engine](https://github
|
|
|
343
346
|
|
|
344
347
|
### Installation
|
|
345
348
|
|
|
346
|
-
Install the optional
|
|
349
|
+
Install the optional peer dependencies:
|
|
347
350
|
|
|
348
351
|
```bash
|
|
349
|
-
npm install
|
|
352
|
+
npm install speech-rule-engine mathjax-full
|
|
350
353
|
```
|
|
351
354
|
|
|
352
355
|
### Setup
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
export interface AudioPlayerProps {
|
|
2
2
|
/** Post slug to lookup audio in audio-map */
|
|
3
3
|
slug: string;
|
|
4
|
+
/** Audio entry data from audio-map.json */
|
|
5
|
+
audioEntry?: {
|
|
6
|
+
audioUrl: string;
|
|
7
|
+
alignmentUrl?: string;
|
|
8
|
+
words?: {
|
|
9
|
+
word: string;
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
}[];
|
|
13
|
+
duration?: number;
|
|
14
|
+
publishableKey?: string;
|
|
15
|
+
};
|
|
4
16
|
/** Accessible label for the player */
|
|
5
17
|
label?: string;
|
|
6
18
|
/** Show placeholder when no audio available */
|
|
@@ -13,6 +25,8 @@ export interface AudioPlayerProps {
|
|
|
13
25
|
enableMiniPlayer?: boolean;
|
|
14
26
|
/** Enable word highlighting during playback */
|
|
15
27
|
enableHighlighting?: boolean;
|
|
28
|
+
/** Enable click on words to seek to that position */
|
|
29
|
+
enableClickToSeek?: boolean;
|
|
16
30
|
/** Number of trailing highlighted words */
|
|
17
31
|
trailLength?: number;
|
|
18
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,gBAAgB;IAC/B,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,6BAA6B;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,uEAAuE;IACvE,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,+CAA+C;IAC/C,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,2CAA2C;IAC3C,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,gBAAgB;IAC/B,6CAA6C;IAC7C,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,UAAU,CAAC,EAAE;QACX,QAAQ,EAAE,MAAM,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,KAAK,CAAC,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAC;YAAC,GAAG,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;QACvD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;IACF,sCAAsC;IACtC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,6BAA6B;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,uEAAuE;IACvE,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,+CAA+C;IAC/C,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,qDAAqD;IACrD,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,2CAA2C;IAC3C,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speech-builder.d.ts","sourceRoot":"","sources":["../../src/core/speech-builder.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"speech-builder.d.ts","sourceRoot":"","sources":["../../src/core/speech-builder.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AA2BrE;;GAEG;AACH,UAAU,YAAY;IACpB,IAAI,CAAC,EAAE,UAAU,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,WAAW,EACjB,OAAO,GAAE,YAAiB,GACzB,OAAO,CAAC,cAAc,CAAC,CAoMzB"}
|
|
@@ -1,70 +1,19 @@
|
|
|
1
1
|
import remarkMath from "remark-math";
|
|
2
2
|
import remarkParse from "remark-parse";
|
|
3
3
|
import { unified } from "unified";
|
|
4
|
+
import { speakLatex } from "../lib/latex-to-speech/speak.js";
|
|
4
5
|
import { computeHash } from "./hash-manager.js";
|
|
5
|
-
// Cache for latex-to-speech results
|
|
6
|
-
const latexSpeechCache = new Map();
|
|
7
|
-
let latexToSpeech = null;
|
|
8
|
-
let latexToSpeechLoadAttempted = false;
|
|
9
|
-
let warnedLatexSpeechFailure = false;
|
|
10
6
|
/**
|
|
11
|
-
*
|
|
7
|
+
* Create a LaTeX converter that delegates to the shared speakLatex module.
|
|
12
8
|
*/
|
|
13
|
-
function
|
|
14
|
-
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Create a lazy-loaded LaTeX converter using our internal latex-to-speech module.
|
|
18
|
-
* This uses speech-rule-engine and mathjax-full as optional peer dependencies.
|
|
19
|
-
*/
|
|
20
|
-
async function createLatexConverter(config) {
|
|
9
|
+
function createLatexConverter(config) {
|
|
21
10
|
if (!config.enabled) {
|
|
22
11
|
return null;
|
|
23
12
|
}
|
|
24
|
-
|
|
25
|
-
if (!latexToSpeechLoadAttempted) {
|
|
26
|
-
latexToSpeechLoadAttempted = true;
|
|
27
|
-
try {
|
|
28
|
-
const mod = await import("../lib/latex-to-speech/index.js");
|
|
29
|
-
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
console.warn("[vocasync] Math-to-speech initialization failed. Install optional dependencies:", "\n npm install speech-rule-engine mathjax-full");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (!latexToSpeech) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
const options = {
|
|
39
|
-
domain: config.style || "clearspeak",
|
|
40
|
-
style: "default",
|
|
41
|
-
locale: "en",
|
|
42
|
-
modality: "speech",
|
|
43
|
-
};
|
|
13
|
+
const style = config.style || "clearspeak";
|
|
44
14
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (!trimmed)
|
|
48
|
-
return "";
|
|
49
|
-
const cacheKey = `${display}:${trimmed}`;
|
|
50
|
-
if (!latexSpeechCache.has(cacheKey)) {
|
|
51
|
-
const pending = (async () => {
|
|
52
|
-
try {
|
|
53
|
-
const result = await latexToSpeech([trimmed], options);
|
|
54
|
-
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
55
|
-
return normalizeWhitespace(spoken);
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
if (!warnedLatexSpeechFailure) {
|
|
59
|
-
console.warn("[vocasync] Failed to convert LaTeX to speech:", error instanceof Error ? error.message : error);
|
|
60
|
-
warnedLatexSpeechFailure = true;
|
|
61
|
-
}
|
|
62
|
-
return normalizeWhitespace(trimmed);
|
|
63
|
-
}
|
|
64
|
-
})();
|
|
65
|
-
latexSpeechCache.set(cacheKey, pending);
|
|
66
|
-
}
|
|
67
|
-
return latexSpeechCache.get(cacheKey);
|
|
15
|
+
convert(latex, display) {
|
|
16
|
+
return speakLatex(latex, display, style);
|
|
68
17
|
},
|
|
69
18
|
};
|
|
70
19
|
}
|
|
@@ -78,7 +27,7 @@ async function createLatexConverter(config) {
|
|
|
78
27
|
*/
|
|
79
28
|
export async function buildSpeechDocument(item, options = {}) {
|
|
80
29
|
const mathConfig = options.math ?? { enabled: false, style: "clearspeak" };
|
|
81
|
-
const latexConverter =
|
|
30
|
+
const latexConverter = createLatexConverter(mathConfig);
|
|
82
31
|
// Parse markdown with math support
|
|
83
32
|
const processor = unified().use(remarkParse).use(remarkMath);
|
|
84
33
|
const tree = processor.parse(item.content);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speech-builder.js","sourceRoot":"","sources":["../../src/core/speech-builder.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"speech-builder.js","sourceRoot":"","sources":["../../src/core/speech-builder.ts"],"names":[],"mappings":"AACA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAShD;;GAEG;AACH,SAAS,oBAAoB,CAAC,MAAkB;IAC9C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,YAAY,CAAC;IAE3C,OAAO;QACL,OAAO,CAAC,KAAa,EAAE,OAAgB;YACrC,OAAO,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;QAC3C,CAAC;KACF,CAAC;AACJ,CAAC;AASD;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,IAAiB,EACjB,UAAwB,EAAE;IAE1B,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;IAC3E,MAAM,cAAc,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC;IAExD,mCAAmC;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AU,EAAE,MAAM,CAAC,CAAC,4BAA4B;SACxD,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC,0BAA0B;SACtD,IAAI,EAAE,CAAC;IAEV,sEAAsE;IACtE,uEAAuE;IACvE,qEAAqE;IAErE,oCAAoC;IACpC,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAE/B,OAAO;QACL,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,IAAI;QACJ,IAAI;QACJ,MAAM,EAAE,IAAI,CAAC,QAAQ;KACtB,CAAC;AACJ,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:
|
|
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,
|
|
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;;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
|
|
10
|
+
let lastEngineKey = "";
|
|
11
11
|
let initPromise = null;
|
|
12
12
|
/**
|
|
13
13
|
* Initialize the speech-rule-engine and MathJax tex2mml converter.
|
|
@@ -47,9 +47,9 @@ async function init() {
|
|
|
47
47
|
const doc = new HTMLDocument("", adaptor, { InputJax: texInput });
|
|
48
48
|
// Create a visitor to serialize to MathML
|
|
49
49
|
const visitor = new SerializedMmlVisitor();
|
|
50
|
-
// Create the tex2mml function
|
|
51
|
-
tex2mml = (latex) => {
|
|
52
|
-
const node = doc.convert(latex, { display
|
|
50
|
+
// Create the tex2mml function — display flag controls MathML output
|
|
51
|
+
tex2mml = (latex, display) => {
|
|
52
|
+
const node = doc.convert(latex, { display });
|
|
53
53
|
return visitor.visitTree(node);
|
|
54
54
|
};
|
|
55
55
|
return true;
|
|
@@ -65,7 +65,7 @@ async function init() {
|
|
|
65
65
|
/**
|
|
66
66
|
* Convert an array of LaTeX expressions to speech.
|
|
67
67
|
*
|
|
68
|
-
* @param exprs - Array of LaTeX expressions to convert
|
|
68
|
+
* @param exprs - Array of LaTeX expressions (with display flag) to convert
|
|
69
69
|
* @param options - SRE configuration options
|
|
70
70
|
* @returns Promise resolving to array of speech strings
|
|
71
71
|
*/
|
|
@@ -75,22 +75,22 @@ export async function latexToSpeech(exprs, options = {}) {
|
|
|
75
75
|
// Return empty descriptions when dependencies not available
|
|
76
76
|
return exprs.map(() => "");
|
|
77
77
|
}
|
|
78
|
-
//
|
|
79
|
-
|
|
78
|
+
// Reconfigure the engine when options change
|
|
79
|
+
const engineKey = `${options.locale || "en"}:${options.domain || "mathspeak"}:${options.style || "default"}`;
|
|
80
|
+
if (engineKey !== lastEngineKey) {
|
|
80
81
|
await SRE.setupEngine({
|
|
81
82
|
locale: options.locale || "en",
|
|
82
83
|
domain: options.domain || "mathspeak",
|
|
83
84
|
style: options.style || "default",
|
|
84
85
|
modality: options.modality || "speech",
|
|
85
86
|
speech: options.speech || "deep",
|
|
86
|
-
...options,
|
|
87
87
|
});
|
|
88
|
-
|
|
88
|
+
lastEngineKey = engineKey;
|
|
89
89
|
}
|
|
90
90
|
// Convert each expression
|
|
91
|
-
return exprs.map((latex) => {
|
|
91
|
+
return exprs.map(({ latex, display }) => {
|
|
92
92
|
try {
|
|
93
|
-
const mml = tex2mml(latex);
|
|
93
|
+
const mml = tex2mml(latex, display);
|
|
94
94
|
return SRE.toSpeech(mml);
|
|
95
95
|
}
|
|
96
96
|
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,
|
|
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,GAAyD,IAAI,CAAC;AACzE,IAAI,aAAa,GAAG,EAAE,CAAC;AACvB,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,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,CAAC;YACH,yDAAyD;YACzD,iEAAiE;YACjE,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,MAAM,CAAC,8BAA8B,CAAC,CAAC;YAC7D,iEAAiE;YACjE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,MAAM,CAAC,+CAA+C,CAAC,CAAC;YACvF,iEAAiE;YACjE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,MAAM,CAAC,yCAAyC,CAAC,CAAC;YAChF,MAAM,EAAE,oBAAoB,EAAE,GAAG,MAAM,MAAM;YAC3C,iEAAiE;YACjE,sDAAsD,CACvD,CAAC;YACF,iEAAiE;YACjE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,MAAM,CAAC,0CAA0C,CAAC,CAAC;YAEjF,kCAAkC;YAClC,MAAM,QA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;IAC7C,MAAM,SAAS,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,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a LaTeX expression to spoken text.
|
|
3
|
+
*
|
|
4
|
+
* Handles lazy loading, caching, normalization, and error recovery.
|
|
5
|
+
* Cache key includes style and display mode for correctness.
|
|
6
|
+
*/
|
|
7
|
+
export declare function speakLatex(latex: string, display: boolean, style?: string): Promise<string>;
|
|
8
|
+
//# 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":"AAiCA;;;;;GAKG;AACH,wBAAsB,UAAU,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,KAAK,SAAe,GACnB,OAAO,CAAC,MAAM,CAAC,CAkCjB"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
let latexToSpeech = null;
|
|
2
|
+
let loadAttempted = false;
|
|
3
|
+
let warnedFailure = false;
|
|
4
|
+
const cache = new Map();
|
|
5
|
+
function normalizeWhitespace(value = "") {
|
|
6
|
+
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Lazily load the latex-to-speech module.
|
|
10
|
+
* Returns false if optional peer dependencies are not installed.
|
|
11
|
+
*/
|
|
12
|
+
async function ensureLoaded() {
|
|
13
|
+
if (loadAttempted)
|
|
14
|
+
return latexToSpeech !== null;
|
|
15
|
+
loadAttempted = true;
|
|
16
|
+
try {
|
|
17
|
+
const mod = await import("./index.js");
|
|
18
|
+
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
19
|
+
return latexToSpeech !== null;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
console.warn("[vocasync] Math-to-speech initialization failed. Install optional dependencies:", "\n npm install speech-rule-engine mathjax-full");
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convert a LaTeX expression to spoken text.
|
|
28
|
+
*
|
|
29
|
+
* Handles lazy loading, caching, normalization, and error recovery.
|
|
30
|
+
* Cache key includes style and display mode for correctness.
|
|
31
|
+
*/
|
|
32
|
+
export async function speakLatex(latex, display, style = "clearspeak") {
|
|
33
|
+
const trimmed = String(latex ?? "").trim();
|
|
34
|
+
if (!trimmed)
|
|
35
|
+
return "";
|
|
36
|
+
if (!(await ensureLoaded())) {
|
|
37
|
+
return trimmed; // Fallback to raw LaTeX
|
|
38
|
+
}
|
|
39
|
+
const cacheKey = `${style}:${display}:${trimmed}`;
|
|
40
|
+
if (!cache.has(cacheKey)) {
|
|
41
|
+
const pending = (async () => {
|
|
42
|
+
try {
|
|
43
|
+
const result = await latexToSpeech([{ latex: trimmed, display }], {
|
|
44
|
+
domain: style,
|
|
45
|
+
style: "default",
|
|
46
|
+
locale: "en",
|
|
47
|
+
modality: "speech",
|
|
48
|
+
});
|
|
49
|
+
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
50
|
+
return normalizeWhitespace(spoken);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (!warnedFailure) {
|
|
54
|
+
console.warn("[vocasync] Failed to convert LaTeX to speech:", error instanceof Error ? error.message : error);
|
|
55
|
+
warnedFailure = true;
|
|
56
|
+
}
|
|
57
|
+
return normalizeWhitespace(trimmed);
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
cache.set(cacheKey, pending);
|
|
61
|
+
}
|
|
62
|
+
return cache.get(cacheKey);
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=speak.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speak.js","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"AAEA,IAAI,aAAa,GACf,IAAI,CAAC;AACP,IAAI,aAAa,GAAG,KAAK,CAAC;AAC1B,IAAI,aAAa,GAAG,KAAK,CAAC;AAE1B,MAAM,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;AAEjD,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;;;GAGG;AACH,KAAK,UA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AAC,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,CAAC;YACrC,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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehype-math-speech.d.ts","sourceRoot":"","sources":["../../src/rehype/rehype-math-speech.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmB,IAAI,EAAE,MAAM,MAAM,CAAC;AAElD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"rehype-math-speech.d.ts","sourceRoot":"","sources":["../../src/rehype/rehype-math-speech.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAmB,IAAI,EAAE,MAAM,MAAM,CAAC;AAElD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAMnC,UAAU,uBAAuB;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,YAAY,GAAG,WAAW,CAAC;CACpC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,IAG1C,MAAM,IAAI,EAAE,MAAM,KAAK,mBA4D1D"}
|
|
@@ -1,58 +1,6 @@
|
|
|
1
1
|
import { visitParents } from "unist-util-visit-parents";
|
|
2
|
+
import { speakLatex } from "../lib/latex-to-speech/speak.js";
|
|
2
3
|
const SR_SPEECH_CLASS = "vocasync-math-speech";
|
|
3
|
-
let latexToSpeech = null;
|
|
4
|
-
let latexToSpeechLoadAttempted = false;
|
|
5
|
-
const latexSpeechCache = new Map();
|
|
6
|
-
let warnedLatexSpeechFailure = false;
|
|
7
|
-
/**
|
|
8
|
-
* Convert LaTeX to spoken text using speech-rule-engine and mathjax-full.
|
|
9
|
-
*/
|
|
10
|
-
async function speakLatex(value, style = "clearspeak") {
|
|
11
|
-
const trimmed = String(value ?? "").trim();
|
|
12
|
-
if (!trimmed)
|
|
13
|
-
return "";
|
|
14
|
-
// Lazy load our internal latex-to-speech module
|
|
15
|
-
if (!latexToSpeechLoadAttempted) {
|
|
16
|
-
latexToSpeechLoadAttempted = true;
|
|
17
|
-
try {
|
|
18
|
-
const mod = await import("../lib/latex-to-speech/index.js");
|
|
19
|
-
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
20
|
-
}
|
|
21
|
-
catch {
|
|
22
|
-
console.warn("[vocasync] Math-to-speech initialization failed. Install optional dependencies:", "\n npm install speech-rule-engine mathjax-full");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (!latexToSpeech) {
|
|
26
|
-
return trimmed; // Fallback to raw latex if library not available
|
|
27
|
-
}
|
|
28
|
-
const cacheKey = `${style}:${trimmed}`;
|
|
29
|
-
if (!latexSpeechCache.has(cacheKey)) {
|
|
30
|
-
const pending = (async () => {
|
|
31
|
-
try {
|
|
32
|
-
const result = await latexToSpeech([trimmed], {
|
|
33
|
-
domain: style,
|
|
34
|
-
style: "default",
|
|
35
|
-
locale: "en",
|
|
36
|
-
modality: "speech",
|
|
37
|
-
});
|
|
38
|
-
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
39
|
-
return normalizeWhitespace(spoken);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
if (!warnedLatexSpeechFailure) {
|
|
43
|
-
console.warn("[vocasync] Failed to convert LaTeX to speech:", error instanceof Error ? error.message : error);
|
|
44
|
-
warnedLatexSpeechFailure = true;
|
|
45
|
-
}
|
|
46
|
-
return normalizeWhitespace(trimmed);
|
|
47
|
-
}
|
|
48
|
-
})();
|
|
49
|
-
latexSpeechCache.set(cacheKey, pending);
|
|
50
|
-
}
|
|
51
|
-
return latexSpeechCache.get(cacheKey);
|
|
52
|
-
}
|
|
53
|
-
function normalizeWhitespace(value = "") {
|
|
54
|
-
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
55
|
-
}
|
|
56
4
|
/**
|
|
57
5
|
* Rehype plugin that injects hidden spoken text next to math elements.
|
|
58
6
|
* This allows rehypeAudioWords to wrap the spoken words with timing data.
|
|
@@ -82,6 +30,9 @@ export default function rehypeMathSpeech(options = {}) {
|
|
|
82
30
|
});
|
|
83
31
|
if (!occurrences.length)
|
|
84
32
|
return;
|
|
33
|
+
if (mathSpeech.length !== occurrences.length) {
|
|
34
|
+
console.warn(`[vocasync] Math speech mismatch: collected ${mathSpeech.length} expression(s) from markdown but found ${occurrences.length} rendered math element(s). Check that remark-math and rehype-katex/rehype-mathjax are both configured.`);
|
|
35
|
+
}
|
|
85
36
|
let mathIndex = 0;
|
|
86
37
|
for (const occurrence of occurrences) {
|
|
87
38
|
if (mathIndex >= mathSpeech.length)
|
|
@@ -91,7 +42,7 @@ export default function rehypeMathSpeech(options = {}) {
|
|
|
91
42
|
const latex = entry.latex.trim();
|
|
92
43
|
if (!latex)
|
|
93
44
|
continue;
|
|
94
|
-
const spoken = await speakLatex(latex, style);
|
|
45
|
+
const spoken = await speakLatex(latex, entry.display, style);
|
|
95
46
|
if (!spoken)
|
|
96
47
|
continue;
|
|
97
48
|
const parentChildren = occurrence.parent.children;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehype-math-speech.js","sourceRoot":"","sources":["../../src/rehype/rehype-math-speech.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"rehype-math-speech.js","sourceRoot":"","sources":["../../src/rehype/rehype-math-speech.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAG7D,MAAM,eAAe,GAAG,sBAAsB,CAAC;AAU/C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,UAAmC,EAAE;IAC5E,MAAM,EAAE,KAAK,GAAG,YAAY,EAAE,GAAG,OAAO,CAAC;IAEzC,OAAO,KAAK,UAAU,WAAW,CAAC,IAAU,EAAE,IAAW;QACvD,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,UAA2C,CAAC;QACzE,IAAI,CAAC,UAAU,EAAE,MAAM;YAAE,OAAO;QAEhC,4CAA4C;QAC5C,MAAM,WAAW,GAA6C,EAAE,CAAC;QACjE,YAAY,CAAC,IAAI,EAAE,SAAS,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE;YAChD,sDAAsD;YACtD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,KAAK,eAAe,CAAC;YACnD,MAAM,OAAO,GACX,IAAI,CAAC,OAAO,KAAK,MAAM;gBACvB,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC;gBACxC,IAAI,CAAC,UAAU,CAAC,SAAsB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YAE5D,IAAI,CAAC,SAAS,IAAI,CAAC,OAAO;gBAAE,OAAO;YAEnC,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAA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C,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ;oBACxD,aAAa,EAAE,MAAM;iBACtB;gBACD,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;aAC5C,CAAC;YAEF,sDAAsD;YACtD,cAAc,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;QAClD,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -5,8 +5,9 @@ export type MathSpeechEntry = {
|
|
|
5
5
|
display: boolean;
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
|
-
* Remark
|
|
9
|
-
*
|
|
8
|
+
* Remark transformer that collects all math expressions from markdown.
|
|
9
|
+
* Requires remark-math to be registered (order does not matter —
|
|
10
|
+
* remark-math operates at parse time via micromark syntax extensions).
|
|
10
11
|
* Stores entries in file.data.mathSpeech for rehypeMathSpeech to consume.
|
|
11
12
|
*/
|
|
12
13
|
export default function remarkMathSpeechCollector(): (tree: Root, file: VFile) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remark-math-speech.d.ts","sourceRoot":"","sources":["../../src/rehype/remark-math-speech.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"remark-math-speech.d.ts","sourceRoot":"","sources":["../../src/rehype/remark-math-speech.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAElC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB,KACnB,MAAM,IAAI,EAAE,MAAM,KAAK,UAepD"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { visit } from "unist-util-visit";
|
|
2
2
|
/**
|
|
3
|
-
* Remark
|
|
4
|
-
*
|
|
3
|
+
* Remark transformer that collects all math expressions from markdown.
|
|
4
|
+
* Requires remark-math to be registered (order does not matter —
|
|
5
|
+
* remark-math operates at parse time via micromark syntax extensions).
|
|
5
6
|
* Stores entries in file.data.mathSpeech for rehypeMathSpeech to consume.
|
|
6
7
|
*/
|
|
7
8
|
export default function remarkMathSpeechCollector() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remark-math-speech.js","sourceRoot":"","sources":["../../src/rehype/remark-math-speech.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAQzC
|
|
1
|
+
{"version":3,"file":"remark-math-speech.js","sourceRoot":"","sources":["../../src/rehype/remark-math-speech.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAQzC;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,yBAAyB;IAC/C,OAAO,SAAS,WAAW,CAAC,IAAU,EAAE,IAAW;QACjD,MAAM,OAAO,GAAsB,EAAE,CAAC;QACtC,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;YACnB,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY;gBAAE,OAAO;YAC/D,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;YACvE,IAAI,CAAC,KAAK;gBAAE,OAAO;YACnB,OAAO,CAAC,IAAI,CAAC;gBACX,KAAK;gBACL,OAAO,EAAE,IAAI,CAAC,IAAI,KAAK,MAAM;aAC9B,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC;QACjC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/src/components/index.ts
CHANGED
|
@@ -7,6 +7,14 @@
|
|
|
7
7
|
export interface AudioPlayerProps {
|
|
8
8
|
/** Post slug to lookup audio in audio-map */
|
|
9
9
|
slug: string;
|
|
10
|
+
/** Audio entry data from audio-map.json */
|
|
11
|
+
audioEntry?: {
|
|
12
|
+
audioUrl: string;
|
|
13
|
+
alignmentUrl?: string;
|
|
14
|
+
words?: { word: string; start: number; end: number }[];
|
|
15
|
+
duration?: number;
|
|
16
|
+
publishableKey?: string;
|
|
17
|
+
};
|
|
10
18
|
/** Accessible label for the player */
|
|
11
19
|
label?: string;
|
|
12
20
|
/** Show placeholder when no audio available */
|
|
@@ -19,6 +27,8 @@ export interface AudioPlayerProps {
|
|
|
19
27
|
enableMiniPlayer?: boolean;
|
|
20
28
|
/** Enable word highlighting during playback */
|
|
21
29
|
enableHighlighting?: boolean;
|
|
30
|
+
/** Enable click on words to seek to that position */
|
|
31
|
+
enableClickToSeek?: boolean;
|
|
22
32
|
/** Number of trailing highlighted words */
|
|
23
33
|
trailLength?: number;
|
|
24
34
|
}
|
|
@@ -3,6 +3,7 @@ import remarkMath from "remark-math";
|
|
|
3
3
|
import remarkParse from "remark-parse";
|
|
4
4
|
import { unified } from "unified";
|
|
5
5
|
import type { MathConfig } from "../config/index.js";
|
|
6
|
+
import { speakLatex } from "../lib/latex-to-speech/speak.js";
|
|
6
7
|
import type { ContentItem, SpeechDocument } from "../types/index.js";
|
|
7
8
|
import { computeHash } from "./hash-manager.js";
|
|
8
9
|
|
|
@@ -13,81 +14,19 @@ interface LaTeXConverter {
|
|
|
13
14
|
convert(latex: string, display: boolean): Promise<string>;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
// Cache for latex-to-speech results
|
|
17
|
-
const latexSpeechCache = new Map<string, Promise<string>>();
|
|
18
|
-
let latexToSpeech:
|
|
19
|
-
| ((exprs: string[], options?: Record<string, unknown>) => Promise<string[]>)
|
|
20
|
-
| null = null;
|
|
21
|
-
let latexToSpeechLoadAttempted = false;
|
|
22
|
-
let warnedLatexSpeechFailure = false;
|
|
23
|
-
|
|
24
17
|
/**
|
|
25
|
-
*
|
|
18
|
+
* Create a LaTeX converter that delegates to the shared speakLatex module.
|
|
26
19
|
*/
|
|
27
|
-
function
|
|
28
|
-
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Create a lazy-loaded LaTeX converter using our internal latex-to-speech module.
|
|
33
|
-
* This uses speech-rule-engine and mathjax-full as optional peer dependencies.
|
|
34
|
-
*/
|
|
35
|
-
async function createLatexConverter(config: MathConfig): Promise<LaTeXConverter | null> {
|
|
20
|
+
function createLatexConverter(config: MathConfig): LaTeXConverter | null {
|
|
36
21
|
if (!config.enabled) {
|
|
37
22
|
return null;
|
|
38
23
|
}
|
|
39
24
|
|
|
40
|
-
|
|
41
|
-
if (!latexToSpeechLoadAttempted) {
|
|
42
|
-
latexToSpeechLoadAttempted = true;
|
|
43
|
-
try {
|
|
44
|
-
const mod = await import("../lib/latex-to-speech/index.js");
|
|
45
|
-
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
46
|
-
} catch {
|
|
47
|
-
console.warn(
|
|
48
|
-
"[vocasync] Math-to-speech initialization failed. Install optional dependencies:",
|
|
49
|
-
"\n npm install speech-rule-engine mathjax-full"
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!latexToSpeech) {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const options = {
|
|
59
|
-
domain: config.style || "clearspeak",
|
|
60
|
-
style: "default",
|
|
61
|
-
locale: "en",
|
|
62
|
-
modality: "speech",
|
|
63
|
-
};
|
|
25
|
+
const style = config.style || "clearspeak";
|
|
64
26
|
|
|
65
27
|
return {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!trimmed) return "";
|
|
69
|
-
|
|
70
|
-
const cacheKey = `${display}:${trimmed}`;
|
|
71
|
-
if (!latexSpeechCache.has(cacheKey)) {
|
|
72
|
-
const pending = (async () => {
|
|
73
|
-
try {
|
|
74
|
-
const result = await latexToSpeech!([trimmed], options);
|
|
75
|
-
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
76
|
-
return normalizeWhitespace(spoken);
|
|
77
|
-
} catch (error) {
|
|
78
|
-
if (!warnedLatexSpeechFailure) {
|
|
79
|
-
console.warn(
|
|
80
|
-
"[vocasync] Failed to convert LaTeX to speech:",
|
|
81
|
-
error instanceof Error ? error.message : error
|
|
82
|
-
);
|
|
83
|
-
warnedLatexSpeechFailure = true;
|
|
84
|
-
}
|
|
85
|
-
return normalizeWhitespace(trimmed);
|
|
86
|
-
}
|
|
87
|
-
})();
|
|
88
|
-
latexSpeechCache.set(cacheKey, pending);
|
|
89
|
-
}
|
|
90
|
-
return latexSpeechCache.get(cacheKey)!;
|
|
28
|
+
convert(latex: string, display: boolean): Promise<string> {
|
|
29
|
+
return speakLatex(latex, display, style);
|
|
91
30
|
},
|
|
92
31
|
};
|
|
93
32
|
}
|
|
@@ -112,7 +51,7 @@ export async function buildSpeechDocument(
|
|
|
112
51
|
options: BuildOptions = {}
|
|
113
52
|
): Promise<SpeechDocument> {
|
|
114
53
|
const mathConfig = options.math ?? { enabled: false, style: "clearspeak" };
|
|
115
|
-
const latexConverter =
|
|
54
|
+
const latexConverter = createLatexConverter(mathConfig);
|
|
116
55
|
|
|
117
56
|
// Parse markdown with math support
|
|
118
57
|
const processor = unified().use(remarkParse).use(remarkMath);
|
|
@@ -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
|
|
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
|
/**
|
|
@@ -55,9 +55,9 @@ async function init(): Promise<boolean> {
|
|
|
55
55
|
// Create a visitor to serialize to MathML
|
|
56
56
|
const visitor = new SerializedMmlVisitor();
|
|
57
57
|
|
|
58
|
-
// Create the tex2mml function
|
|
59
|
-
tex2mml = (latex: string): string => {
|
|
60
|
-
const node = doc.convert(latex, { display
|
|
58
|
+
// Create the tex2mml function — display flag controls MathML output
|
|
59
|
+
tex2mml = (latex: string, display: boolean): string => {
|
|
60
|
+
const node = doc.convert(latex, { display });
|
|
61
61
|
return visitor.visitTree(node);
|
|
62
62
|
};
|
|
63
63
|
|
|
@@ -83,14 +83,25 @@ export interface SREOptions {
|
|
|
83
83
|
speech?: string;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/**
|
|
87
|
+
* A single LaTeX expression with its display context.
|
|
88
|
+
*/
|
|
89
|
+
export interface LaTeXExpression {
|
|
90
|
+
latex: string;
|
|
91
|
+
display: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
86
94
|
/**
|
|
87
95
|
* Convert an array of LaTeX expressions to speech.
|
|
88
96
|
*
|
|
89
|
-
* @param exprs - Array of LaTeX expressions to convert
|
|
97
|
+
* @param exprs - Array of LaTeX expressions (with display flag) to convert
|
|
90
98
|
* @param options - SRE configuration options
|
|
91
99
|
* @returns Promise resolving to array of speech strings
|
|
92
100
|
*/
|
|
93
|
-
export async function latexToSpeech(
|
|
101
|
+
export async function latexToSpeech(
|
|
102
|
+
exprs: LaTeXExpression[],
|
|
103
|
+
options: SREOptions = {}
|
|
104
|
+
): Promise<string[]> {
|
|
94
105
|
const available = await init();
|
|
95
106
|
|
|
96
107
|
if (!available || !SRE || !tex2mml) {
|
|
@@ -98,23 +109,23 @@ export async function latexToSpeech(exprs: string[], options: SREOptions = {}):
|
|
|
98
109
|
return exprs.map(() => "");
|
|
99
110
|
}
|
|
100
111
|
|
|
101
|
-
//
|
|
102
|
-
|
|
112
|
+
// Reconfigure the engine when options change
|
|
113
|
+
const engineKey = `${options.locale || "en"}:${options.domain || "mathspeak"}:${options.style || "default"}`;
|
|
114
|
+
if (engineKey !== lastEngineKey) {
|
|
103
115
|
await SRE.setupEngine({
|
|
104
116
|
locale: options.locale || "en",
|
|
105
117
|
domain: options.domain || "mathspeak",
|
|
106
118
|
style: options.style || "default",
|
|
107
119
|
modality: options.modality || "speech",
|
|
108
120
|
speech: options.speech || "deep",
|
|
109
|
-
...options,
|
|
110
121
|
});
|
|
111
|
-
|
|
122
|
+
lastEngineKey = engineKey;
|
|
112
123
|
}
|
|
113
124
|
|
|
114
125
|
// Convert each expression
|
|
115
|
-
return exprs.map((latex) => {
|
|
126
|
+
return exprs.map(({ latex, display }) => {
|
|
116
127
|
try {
|
|
117
|
-
const mml = tex2mml!(latex);
|
|
128
|
+
const mml = tex2mml!(latex, display);
|
|
118
129
|
return SRE!.toSpeech(mml);
|
|
119
130
|
} catch {
|
|
120
131
|
console.warn(`[vocasync] Failed to convert LaTeX to speech: ${latex}`);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { LaTeXExpression, SREOptions } from "./index.js";
|
|
2
|
+
|
|
3
|
+
let latexToSpeech: ((exprs: LaTeXExpression[], options?: SREOptions) => Promise<string[]>) | null =
|
|
4
|
+
null;
|
|
5
|
+
let loadAttempted = false;
|
|
6
|
+
let warnedFailure = false;
|
|
7
|
+
|
|
8
|
+
const cache = new Map<string, Promise<string>>();
|
|
9
|
+
|
|
10
|
+
function normalizeWhitespace(value = ""): string {
|
|
11
|
+
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Lazily load the latex-to-speech module.
|
|
16
|
+
* Returns false if optional peer dependencies are not installed.
|
|
17
|
+
*/
|
|
18
|
+
async function ensureLoaded(): Promise<boolean> {
|
|
19
|
+
if (loadAttempted) return latexToSpeech !== null;
|
|
20
|
+
loadAttempted = true;
|
|
21
|
+
try {
|
|
22
|
+
const mod = await import("./index.js");
|
|
23
|
+
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
24
|
+
return latexToSpeech !== null;
|
|
25
|
+
} catch {
|
|
26
|
+
console.warn(
|
|
27
|
+
"[vocasync] Math-to-speech initialization failed. Install optional dependencies:",
|
|
28
|
+
"\n npm install speech-rule-engine mathjax-full"
|
|
29
|
+
);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Convert a LaTeX expression to spoken text.
|
|
36
|
+
*
|
|
37
|
+
* Handles lazy loading, caching, normalization, and error recovery.
|
|
38
|
+
* Cache key includes style and display mode for correctness.
|
|
39
|
+
*/
|
|
40
|
+
export async function speakLatex(
|
|
41
|
+
latex: string,
|
|
42
|
+
display: boolean,
|
|
43
|
+
style = "clearspeak"
|
|
44
|
+
): Promise<string> {
|
|
45
|
+
const trimmed = String(latex ?? "").trim();
|
|
46
|
+
if (!trimmed) return "";
|
|
47
|
+
|
|
48
|
+
if (!(await ensureLoaded())) {
|
|
49
|
+
return trimmed; // Fallback to raw LaTeX
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const cacheKey = `${style}:${display}:${trimmed}`;
|
|
53
|
+
if (!cache.has(cacheKey)) {
|
|
54
|
+
const pending = (async () => {
|
|
55
|
+
try {
|
|
56
|
+
const result = await latexToSpeech!([{ latex: trimmed, display }], {
|
|
57
|
+
domain: style,
|
|
58
|
+
style: "default",
|
|
59
|
+
locale: "en",
|
|
60
|
+
modality: "speech",
|
|
61
|
+
});
|
|
62
|
+
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
63
|
+
return normalizeWhitespace(spoken);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (!warnedFailure) {
|
|
66
|
+
console.warn(
|
|
67
|
+
"[vocasync] Failed to convert LaTeX to speech:",
|
|
68
|
+
error instanceof Error ? error.message : error
|
|
69
|
+
);
|
|
70
|
+
warnedFailure = true;
|
|
71
|
+
}
|
|
72
|
+
return normalizeWhitespace(trimmed);
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
cache.set(cacheKey, pending);
|
|
76
|
+
}
|
|
77
|
+
return cache.get(cacheKey)!;
|
|
78
|
+
}
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import type { Element, Parent, Root } from "hast";
|
|
2
2
|
import { visitParents } from "unist-util-visit-parents";
|
|
3
3
|
import type { VFile } from "vfile";
|
|
4
|
+
import { speakLatex } from "../lib/latex-to-speech/speak.js";
|
|
4
5
|
import type { MathSpeechEntry } from "./remark-math-speech.js";
|
|
5
6
|
|
|
6
7
|
const SR_SPEECH_CLASS = "vocasync-math-speech";
|
|
7
8
|
|
|
8
|
-
let latexToSpeech:
|
|
9
|
-
| ((exprs: string[], options?: Record<string, unknown>) => Promise<string[]>)
|
|
10
|
-
| null = null;
|
|
11
|
-
let latexToSpeechLoadAttempted = false;
|
|
12
|
-
|
|
13
|
-
const latexSpeechCache = new Map<string, Promise<string>>();
|
|
14
|
-
let warnedLatexSpeechFailure = false;
|
|
15
|
-
|
|
16
9
|
interface RehypeMathSpeechOptions {
|
|
17
10
|
/**
|
|
18
11
|
* SRE domain style for math-to-speech conversion
|
|
@@ -21,63 +14,6 @@ interface RehypeMathSpeechOptions {
|
|
|
21
14
|
style?: "clearspeak" | "mathspeak";
|
|
22
15
|
}
|
|
23
16
|
|
|
24
|
-
/**
|
|
25
|
-
* Convert LaTeX to spoken text using speech-rule-engine and mathjax-full.
|
|
26
|
-
*/
|
|
27
|
-
async function speakLatex(value: string, style = "clearspeak"): Promise<string> {
|
|
28
|
-
const trimmed = String(value ?? "").trim();
|
|
29
|
-
if (!trimmed) return "";
|
|
30
|
-
|
|
31
|
-
// Lazy load our internal latex-to-speech module
|
|
32
|
-
if (!latexToSpeechLoadAttempted) {
|
|
33
|
-
latexToSpeechLoadAttempted = true;
|
|
34
|
-
try {
|
|
35
|
-
const mod = await import("../lib/latex-to-speech/index.js");
|
|
36
|
-
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
37
|
-
} catch {
|
|
38
|
-
console.warn(
|
|
39
|
-
"[vocasync] Math-to-speech initialization failed. Install optional dependencies:",
|
|
40
|
-
"\n npm install speech-rule-engine mathjax-full"
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!latexToSpeech) {
|
|
46
|
-
return trimmed; // Fallback to raw latex if library not available
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const cacheKey = `${style}:${trimmed}`;
|
|
50
|
-
if (!latexSpeechCache.has(cacheKey)) {
|
|
51
|
-
const pending = (async () => {
|
|
52
|
-
try {
|
|
53
|
-
const result = await latexToSpeech!([trimmed], {
|
|
54
|
-
domain: style,
|
|
55
|
-
style: "default",
|
|
56
|
-
locale: "en",
|
|
57
|
-
modality: "speech",
|
|
58
|
-
});
|
|
59
|
-
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
60
|
-
return normalizeWhitespace(spoken);
|
|
61
|
-
} catch (error) {
|
|
62
|
-
if (!warnedLatexSpeechFailure) {
|
|
63
|
-
console.warn(
|
|
64
|
-
"[vocasync] Failed to convert LaTeX to speech:",
|
|
65
|
-
error instanceof Error ? error.message : error
|
|
66
|
-
);
|
|
67
|
-
warnedLatexSpeechFailure = true;
|
|
68
|
-
}
|
|
69
|
-
return normalizeWhitespace(trimmed);
|
|
70
|
-
}
|
|
71
|
-
})();
|
|
72
|
-
latexSpeechCache.set(cacheKey, pending);
|
|
73
|
-
}
|
|
74
|
-
return latexSpeechCache.get(cacheKey)!;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function normalizeWhitespace(value = ""): string {
|
|
78
|
-
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
17
|
/**
|
|
82
18
|
* Rehype plugin that injects hidden spoken text next to math elements.
|
|
83
19
|
* This allows rehypeAudioWords to wrap the spoken words with timing data.
|
|
@@ -110,6 +46,12 @@ export default function rehypeMathSpeech(options: RehypeMathSpeechOptions = {})
|
|
|
110
46
|
|
|
111
47
|
if (!occurrences.length) return;
|
|
112
48
|
|
|
49
|
+
if (mathSpeech.length !== occurrences.length) {
|
|
50
|
+
console.warn(
|
|
51
|
+
`[vocasync] Math speech mismatch: collected ${mathSpeech.length} expression(s) from markdown but found ${occurrences.length} rendered math element(s). Check that remark-math and rehype-katex/rehype-mathjax are both configured.`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
113
55
|
let mathIndex = 0;
|
|
114
56
|
for (const occurrence of occurrences) {
|
|
115
57
|
if (mathIndex >= mathSpeech.length) break;
|
|
@@ -118,7 +60,7 @@ export default function rehypeMathSpeech(options: RehypeMathSpeechOptions = {})
|
|
|
118
60
|
const latex = entry.latex.trim();
|
|
119
61
|
if (!latex) continue;
|
|
120
62
|
|
|
121
|
-
const spoken = await speakLatex(latex, style);
|
|
63
|
+
const spoken = await speakLatex(latex, entry.display, style);
|
|
122
64
|
if (!spoken) continue;
|
|
123
65
|
|
|
124
66
|
const parentChildren = occurrence.parent.children;
|
|
@@ -8,8 +8,9 @@ export type MathSpeechEntry = {
|
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Remark
|
|
12
|
-
*
|
|
11
|
+
* Remark transformer that collects all math expressions from markdown.
|
|
12
|
+
* Requires remark-math to be registered (order does not matter —
|
|
13
|
+
* remark-math operates at parse time via micromark syntax extensions).
|
|
13
14
|
* Stores entries in file.data.mathSpeech for rehypeMathSpeech to consume.
|
|
14
15
|
*/
|
|
15
16
|
export default function remarkMathSpeechCollector() {
|
package/src/types/modules.d.ts
CHANGED
|
@@ -4,8 +4,11 @@ declare module "speech-rule-engine" {
|
|
|
4
4
|
export function engineReady(): Promise<void>;
|
|
5
5
|
export function setupEngine(options: {
|
|
6
6
|
locale?: string;
|
|
7
|
+
domain?: string;
|
|
7
8
|
modality?: string;
|
|
8
9
|
style?: string;
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
speech?: string;
|
|
11
|
+
}): Promise<void>;
|
|
12
|
+
export function resetEngine(): void;
|
|
13
|
+
export function toSpeech(mml: string): string;
|
|
11
14
|
}
|