@vocasync/astro 0.1.10 → 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.
- package/README.md +77 -41
- package/dist/api/client.d.ts +63 -50
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +132 -145
- package/dist/api/client.js.map +1 -1
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +1 -1
- package/dist/api/index.js.map +1 -1
- package/dist/cli.js +7 -7
- package/dist/cli.js.map +1 -1
- package/dist/components/AudioPlayer.astro +136 -137
- package/dist/config/schema.d.ts +21 -20
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +15 -4
- package/dist/config/schema.js.map +1 -1
- package/dist/core/audio-map.js +1 -1
- package/dist/core/speech-builder.d.ts +10 -7
- package/dist/core/speech-builder.d.ts.map +1 -1
- package/dist/core/speech-builder.js +72 -116
- package/dist/core/speech-builder.js.map +1 -1
- package/dist/core/spoken-form.d.ts +52 -0
- package/dist/core/spoken-form.d.ts.map +1 -0
- package/dist/core/spoken-form.js +279 -0
- package/dist/core/spoken-form.js.map +1 -0
- package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
- package/dist/lib/latex-to-speech/index.js +9 -11
- package/dist/lib/latex-to-speech/index.js.map +1 -1
- package/dist/lib/latex-to-speech/speak.d.ts +7 -2
- package/dist/lib/latex-to-speech/speak.d.ts.map +1 -1
- package/dist/lib/latex-to-speech/speak.js +10 -27
- package/dist/lib/latex-to-speech/speak.js.map +1 -1
- package/dist/rehype/audio-words.d.ts +14 -24
- package/dist/rehype/audio-words.d.ts.map +1 -1
- package/dist/rehype/audio-words.js +131 -252
- package/dist/rehype/audio-words.js.map +1 -1
- package/dist/rehype/index.d.ts +1 -2
- package/dist/rehype/index.d.ts.map +1 -1
- package/dist/rehype/index.js +0 -1
- package/dist/rehype/index.js.map +1 -1
- package/dist/rehype/rehype-math-speech.d.ts +26 -10
- package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
- package/dist/rehype/rehype-math-speech.js +59 -43
- package/dist/rehype/rehype-math-speech.js.map +1 -1
- package/dist/rehype/shared.d.ts +9 -0
- package/dist/rehype/shared.d.ts.map +1 -0
- package/dist/rehype/shared.js +31 -0
- package/dist/rehype/shared.js.map +1 -0
- package/dist/sync/orchestrator.d.ts +4 -22
- package/dist/sync/orchestrator.d.ts.map +1 -1
- package/dist/sync/orchestrator.js +123 -123
- package/dist/sync/orchestrator.js.map +1 -1
- package/dist/types/index.d.ts +82 -46
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +14 -11
- package/src/api/client.ts +175 -190
- package/src/api/index.ts +8 -1
- package/src/cli.ts +7 -14
- package/src/components/AudioPlayer.astro +136 -137
- package/src/config/schema.ts +15 -4
- package/src/core/audio-map.ts +1 -1
- package/src/core/speech-builder.ts +74 -130
- package/src/core/spoken-form.ts +298 -0
- package/src/lib/latex-to-speech/index.ts +8 -9
- package/src/lib/latex-to-speech/speak.ts +11 -34
- package/src/rehype/audio-words.ts +145 -307
- package/src/rehype/index.ts +1 -2
- package/src/rehype/rehype-math-speech.ts +64 -59
- package/src/rehype/shared.ts +36 -0
- package/src/sync/orchestrator.ts +150 -157
- package/src/types/index.ts +85 -47
- package/dist/rehype/remark-math-speech.d.ts +0 -14
- package/dist/rehype/remark-math-speech.d.ts.map +0 -1
- package/dist/rehype/remark-math-speech.js +0 -27
- package/dist/rehype/remark-math-speech.js.map +0 -1
- package/src/rehype/remark-math-speech.ts +0 -32
|
@@ -1,194 +1,143 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Content, Emphasis, Heading, Root, Strong, Text } from "mdast";
|
|
2
2
|
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
6
|
import { speakLatex } from "../lib/latex-to-speech/speak.js";
|
|
7
7
|
import type { ContentItem, SpeechDocument } from "../types/index.js";
|
|
8
|
-
import {
|
|
8
|
+
import { buildSpeechtextPair } from "./spoken-form.js";
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* LaTeX to speech converter interface
|
|
12
|
-
*/
|
|
13
|
-
interface LaTeXConverter {
|
|
14
|
-
convert(latex: string, display: boolean): Promise<string>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Create a LaTeX converter that delegates to the shared speakLatex module.
|
|
19
|
-
*/
|
|
20
|
-
function createLatexConverter(config: MathConfig): LaTeXConverter | null {
|
|
21
|
-
if (!config.enabled) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const style = config.style || "clearspeak";
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
convert(latex: string, display: boolean): Promise<string> {
|
|
29
|
-
return speakLatex(latex, display, style);
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Options for building speech document
|
|
36
|
-
*/
|
|
37
10
|
interface BuildOptions {
|
|
38
11
|
math?: MathConfig;
|
|
39
12
|
}
|
|
40
13
|
|
|
41
14
|
/**
|
|
42
15
|
* Build a speech document from markdown content.
|
|
43
|
-
* Converts markdown to plain text suitable for TTS synthesis.
|
|
44
16
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
17
|
+
* Produces plain text from the SAME visible tokens the rehype word-wrapper
|
|
18
|
+
* sees — no phantom tokens (no "Quote:", "Image:", "[Code in X]", inline
|
|
19
|
+
* code), and code blocks / images are skipped exactly as rehype skips
|
|
20
|
+
* `pre`/`code`/`img`. This keeps the alignment transcript
|
|
21
|
+
* (`normalisedSpeechtext`) token-for-token with the rehype `data-i` stream.
|
|
22
|
+
*
|
|
23
|
+
* Headings keep a leading "# " marker so the synthesis backend chunks at
|
|
24
|
+
* heading boundaries; `stripForAlignment` removes it, so it never becomes an
|
|
25
|
+
* alignment token. Math expressions contribute their `speakLatex` output at
|
|
26
|
+
* their reading position (the same string the rehype math handler counts).
|
|
48
27
|
*/
|
|
49
28
|
export async function buildSpeechDocument(
|
|
50
29
|
item: ContentItem,
|
|
51
30
|
options: BuildOptions = {}
|
|
52
31
|
): Promise<SpeechDocument> {
|
|
53
32
|
const mathConfig = options.math ?? { enabled: false, style: "clearspeak" };
|
|
54
|
-
const
|
|
33
|
+
const style = mathConfig.style || "clearspeak";
|
|
55
34
|
|
|
56
|
-
// Parse markdown with math support
|
|
57
35
|
const processor = unified().use(remarkParse).use(remarkMath);
|
|
58
|
-
|
|
59
36
|
const tree = processor.parse(item.content) as Root;
|
|
60
37
|
|
|
61
|
-
// Extract text from AST - can contain strings or promises
|
|
62
38
|
const textParts: (string | Promise<string>)[] = [];
|
|
39
|
+
// Collect math spoken forms keyed by `${'d'|'i'}:${latex}` so the rehype
|
|
40
|
+
// plugin can reuse them without running math-to-speech in the Vite build.
|
|
41
|
+
const mathKeys: string[] = [];
|
|
42
|
+
const mathPromises: Promise<string>[] = [];
|
|
43
|
+
|
|
44
|
+
function recordMath(latex: string, display: boolean): Promise<string> {
|
|
45
|
+
const p = speakLatex(latex, display, style);
|
|
46
|
+
mathKeys.push(`${display ? "d" : "i"}:${latex.trim()}`);
|
|
47
|
+
mathPromises.push(p);
|
|
48
|
+
return p;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function processChildren(node: { children: Content[] }): void {
|
|
52
|
+
for (const child of node.children) {
|
|
53
|
+
processNode(child);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
63
56
|
|
|
64
57
|
function processNode(node: Content | Root): void {
|
|
65
58
|
switch (node.type) {
|
|
66
59
|
case "text": {
|
|
67
|
-
|
|
68
|
-
textParts.push(textNode.value);
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
case "inlineCode": {
|
|
73
|
-
const codeNode = node as InlineCode;
|
|
74
|
-
// Skip short code, read longer code
|
|
75
|
-
if (codeNode.value.length > 10) {
|
|
76
|
-
textParts.push(codeNode.value);
|
|
77
|
-
}
|
|
60
|
+
textParts.push((node as Text).value);
|
|
78
61
|
break;
|
|
79
62
|
}
|
|
80
63
|
|
|
64
|
+
// Inline code and code blocks are skipped by the rehype word-wrapper
|
|
65
|
+
// (pre/code in its skip set), so they must not contribute speech tokens.
|
|
66
|
+
case "inlineCode":
|
|
81
67
|
case "code": {
|
|
82
|
-
const blockCode = node as Code;
|
|
83
|
-
// Skip code blocks entirely for TTS
|
|
84
|
-
// Could optionally add language hint: "Code block in {lang}"
|
|
85
|
-
if (blockCode.lang) {
|
|
86
|
-
textParts.push(`[Code in ${blockCode.lang}]`);
|
|
87
|
-
}
|
|
88
68
|
break;
|
|
89
69
|
}
|
|
90
70
|
|
|
91
71
|
case "heading": {
|
|
92
72
|
const heading = node as Heading;
|
|
93
|
-
//
|
|
73
|
+
// Single "#" chunk marker for all heading levels (synthesis backend
|
|
74
|
+
// expects it). Removed by stripForAlignment, so not an alignment token.
|
|
94
75
|
textParts.push("\n# ");
|
|
95
|
-
|
|
96
|
-
for (const child of heading.children) {
|
|
97
|
-
processNode(child as Content);
|
|
98
|
-
}
|
|
99
|
-
// Add newline after headings for separation
|
|
76
|
+
processChildren(heading);
|
|
100
77
|
textParts.push("\n");
|
|
101
78
|
break;
|
|
102
79
|
}
|
|
103
80
|
|
|
104
81
|
case "paragraph": {
|
|
105
|
-
|
|
106
|
-
for (const child of para.children) {
|
|
107
|
-
processNode(child);
|
|
108
|
-
}
|
|
109
|
-
// Paragraph break
|
|
82
|
+
processChildren(node as { children: Content[] });
|
|
110
83
|
textParts.push("\n");
|
|
111
84
|
break;
|
|
112
85
|
}
|
|
113
86
|
|
|
114
87
|
case "emphasis": {
|
|
115
|
-
|
|
116
|
-
for (const child of em.children) {
|
|
117
|
-
processNode(child as Content);
|
|
118
|
-
}
|
|
88
|
+
processChildren(node as Emphasis);
|
|
119
89
|
break;
|
|
120
90
|
}
|
|
121
91
|
|
|
122
92
|
case "strong": {
|
|
123
|
-
|
|
124
|
-
for (const child of strong.children) {
|
|
125
|
-
processNode(child as Content);
|
|
126
|
-
}
|
|
93
|
+
processChildren(node as Strong);
|
|
127
94
|
break;
|
|
128
95
|
}
|
|
129
96
|
|
|
130
97
|
case "link": {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
for (const child of link.children) {
|
|
134
|
-
processNode(child);
|
|
135
|
-
}
|
|
98
|
+
// Read link text only, not the URL (matches rehype: <a> text is walked).
|
|
99
|
+
processChildren(node as { children: Content[] });
|
|
136
100
|
break;
|
|
137
101
|
}
|
|
138
102
|
|
|
103
|
+
// Images are skipped by the rehype word-wrapper (img in its skip set);
|
|
104
|
+
// alt text is an attribute, not a rendered text node — emit nothing.
|
|
139
105
|
case "image": {
|
|
140
|
-
const img = node as { alt?: string };
|
|
141
|
-
// Read alt text
|
|
142
|
-
if (img.alt) {
|
|
143
|
-
textParts.push(`Image: ${img.alt}`);
|
|
144
|
-
}
|
|
145
106
|
break;
|
|
146
107
|
}
|
|
147
108
|
|
|
148
109
|
case "list": {
|
|
149
|
-
|
|
150
|
-
for (const child of list.children) {
|
|
151
|
-
processNode(child);
|
|
152
|
-
}
|
|
110
|
+
processChildren(node as { children: Content[] });
|
|
153
111
|
break;
|
|
154
112
|
}
|
|
155
113
|
|
|
156
114
|
case "listItem": {
|
|
157
|
-
|
|
158
|
-
for
|
|
159
|
-
processNode(child);
|
|
160
|
-
}
|
|
115
|
+
processChildren(node as { children: Content[] });
|
|
116
|
+
// Sentence break for TTS prosody; punctuation only, not an alignment token.
|
|
161
117
|
textParts.push(". ");
|
|
162
118
|
break;
|
|
163
119
|
}
|
|
164
120
|
|
|
165
121
|
case "blockquote": {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
textParts.push(" End quote.");
|
|
122
|
+
// Just the quote's text — no "Quote:"/"End quote." wrappers (those
|
|
123
|
+
// words have no rendered-DOM counterpart and would desync data-i).
|
|
124
|
+
processChildren(node as { children: Content[] });
|
|
125
|
+
textParts.push("\n");
|
|
172
126
|
break;
|
|
173
127
|
}
|
|
174
128
|
|
|
175
|
-
// Math nodes from remark-math
|
|
176
129
|
case "math": {
|
|
177
130
|
const mathNode = node as { value: string };
|
|
178
|
-
if (
|
|
179
|
-
textParts.push(
|
|
180
|
-
} else {
|
|
181
|
-
textParts.push(`[Math: ${mathNode.value.slice(0, 30)}]`);
|
|
131
|
+
if (mathConfig.enabled) {
|
|
132
|
+
textParts.push(recordMath(mathNode.value, true));
|
|
182
133
|
}
|
|
183
134
|
break;
|
|
184
135
|
}
|
|
185
136
|
|
|
186
137
|
case "inlineMath": {
|
|
187
138
|
const inlineMathNode = node as { value: string };
|
|
188
|
-
if (
|
|
189
|
-
textParts.push(
|
|
190
|
-
} else {
|
|
191
|
-
textParts.push(inlineMathNode.value);
|
|
139
|
+
if (mathConfig.enabled) {
|
|
140
|
+
textParts.push(recordMath(inlineMathNode.value, false));
|
|
192
141
|
}
|
|
193
142
|
break;
|
|
194
143
|
}
|
|
@@ -198,51 +147,46 @@ export async function buildSpeechDocument(
|
|
|
198
147
|
break;
|
|
199
148
|
}
|
|
200
149
|
|
|
201
|
-
// Handle container nodes with children
|
|
202
150
|
case "root": {
|
|
203
|
-
|
|
204
|
-
for (const child of root.children) {
|
|
205
|
-
processNode(child);
|
|
206
|
-
}
|
|
151
|
+
processChildren(node as Root);
|
|
207
152
|
break;
|
|
208
153
|
}
|
|
209
154
|
|
|
210
155
|
default: {
|
|
211
|
-
// Try to process children if they exist
|
|
212
156
|
const maybeParent = node as { children?: Content[] };
|
|
213
|
-
if (maybeParent.children) {
|
|
214
|
-
for (const child of maybeParent.children) {
|
|
215
|
-
processNode(child);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
157
|
+
if (maybeParent.children) processChildren(maybeParent as { children: Content[] });
|
|
218
158
|
}
|
|
219
159
|
}
|
|
220
160
|
}
|
|
221
161
|
|
|
222
162
|
processNode(tree);
|
|
223
163
|
|
|
224
|
-
// Resolve
|
|
164
|
+
// Resolve async math conversions.
|
|
225
165
|
const resolvedParts = await Promise.all(textParts);
|
|
226
166
|
|
|
227
|
-
//
|
|
228
|
-
|
|
167
|
+
// Normalize whitespace. The title is intentionally excluded so the
|
|
168
|
+
// alignment data matches the article body only.
|
|
169
|
+
const plainText = resolvedParts
|
|
229
170
|
.join(" ")
|
|
230
|
-
.replace(
|
|
231
|
-
.replace(/\n\s*\n/g, "\n\n")
|
|
232
|
-
.replace(/\s*\.\s*\./g, ".")
|
|
171
|
+
.replace(/[ \t]+/g, " ")
|
|
172
|
+
.replace(/\n\s*\n/g, "\n\n")
|
|
173
|
+
.replace(/\s*\.\s*\./g, ".")
|
|
233
174
|
.trim();
|
|
234
175
|
|
|
235
|
-
|
|
236
|
-
// The title is displayed visually and doesn't need to be in the audio.
|
|
237
|
-
// This also ensures alignment data matches the article body content.
|
|
176
|
+
const { speechtext, normalisedSpeechtext } = buildSpeechtextPair(plainText);
|
|
238
177
|
|
|
239
|
-
//
|
|
240
|
-
const
|
|
178
|
+
// Map each math expression's latex key to its resolved spoken form.
|
|
179
|
+
const resolvedMath = await Promise.all(mathPromises);
|
|
180
|
+
const mathSpeech: Record<string, string> = {};
|
|
181
|
+
for (let i = 0; i < mathKeys.length; i++) {
|
|
182
|
+
mathSpeech[mathKeys[i]] = resolvedMath[i];
|
|
183
|
+
}
|
|
241
184
|
|
|
242
185
|
return {
|
|
243
186
|
slug: item.slug,
|
|
244
|
-
|
|
245
|
-
|
|
187
|
+
speechtext,
|
|
188
|
+
normalisedSpeechtext,
|
|
189
|
+
mathSpeech,
|
|
246
190
|
source: item.filePath,
|
|
247
191
|
};
|
|
248
192
|
}
|
|
@@ -0,0 +1,298 @@
|
|
|
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
|
+
|
|
26
|
+
const CURRENCY_SYMBOL_WORDS: Record<string, string> = {
|
|
27
|
+
"£": "pound",
|
|
28
|
+
$: "dollar",
|
|
29
|
+
"€": "euro",
|
|
30
|
+
"¥": "yen",
|
|
31
|
+
"₹": "rupee",
|
|
32
|
+
"₽": "ruble",
|
|
33
|
+
"₩": "won",
|
|
34
|
+
"₪": "shekel",
|
|
35
|
+
"₺": "lira",
|
|
36
|
+
"฿": "baht",
|
|
37
|
+
"₫": "dong",
|
|
38
|
+
"₴": "hryvnia",
|
|
39
|
+
"₦": "naira",
|
|
40
|
+
"₱": "peso",
|
|
41
|
+
"₲": "guarani",
|
|
42
|
+
"₡": "colon",
|
|
43
|
+
"₭": "kip",
|
|
44
|
+
"₮": "tugrik",
|
|
45
|
+
"₿": "bitcoin",
|
|
46
|
+
"₣": "franc",
|
|
47
|
+
"¢": "cent",
|
|
48
|
+
"₤": "lira",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const CURRENCY_SYMBOL_LIST = Object.keys(CURRENCY_SYMBOL_WORDS);
|
|
52
|
+
const CURRENCY_SYMBOL_CLASS = CURRENCY_SYMBOL_LIST.join("");
|
|
53
|
+
const CURRENCY_SYMBOL_RANGE = `[${CURRENCY_SYMBOL_CLASS}]`;
|
|
54
|
+
const CURRENCY_SYMBOL_BEFORE_NUMBER_REGEX = new RegExp(
|
|
55
|
+
`(${CURRENCY_SYMBOL_RANGE})\\s*([0-9]+(?:[.,][0-9]+)*)`,
|
|
56
|
+
"gu"
|
|
57
|
+
);
|
|
58
|
+
const CURRENCY_SYMBOL_AFTER_NUMBER_REGEX = new RegExp(
|
|
59
|
+
`([0-9]+(?:[.,][0-9]+)*)\\s*(${CURRENCY_SYMBOL_RANGE})`,
|
|
60
|
+
"gu"
|
|
61
|
+
);
|
|
62
|
+
const CURRENCY_SYMBOL_REGEX = new RegExp(CURRENCY_SYMBOL_RANGE, "gu");
|
|
63
|
+
|
|
64
|
+
const DIGIT_WORDS = [
|
|
65
|
+
"zero",
|
|
66
|
+
"one",
|
|
67
|
+
"two",
|
|
68
|
+
"three",
|
|
69
|
+
"four",
|
|
70
|
+
"five",
|
|
71
|
+
"six",
|
|
72
|
+
"seven",
|
|
73
|
+
"eight",
|
|
74
|
+
"nine",
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const SMALL_WORDS = [
|
|
78
|
+
"zero",
|
|
79
|
+
"one",
|
|
80
|
+
"two",
|
|
81
|
+
"three",
|
|
82
|
+
"four",
|
|
83
|
+
"five",
|
|
84
|
+
"six",
|
|
85
|
+
"seven",
|
|
86
|
+
"eight",
|
|
87
|
+
"nine",
|
|
88
|
+
"ten",
|
|
89
|
+
"eleven",
|
|
90
|
+
"twelve",
|
|
91
|
+
"thirteen",
|
|
92
|
+
"fourteen",
|
|
93
|
+
"fifteen",
|
|
94
|
+
"sixteen",
|
|
95
|
+
"seventeen",
|
|
96
|
+
"eighteen",
|
|
97
|
+
"nineteen",
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
const TENS_WORDS = [
|
|
101
|
+
"",
|
|
102
|
+
"",
|
|
103
|
+
"twenty",
|
|
104
|
+
"thirty",
|
|
105
|
+
"forty",
|
|
106
|
+
"fifty",
|
|
107
|
+
"sixty",
|
|
108
|
+
"seventy",
|
|
109
|
+
"eighty",
|
|
110
|
+
"ninety",
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
const SCALE_WORDS = [
|
|
114
|
+
"",
|
|
115
|
+
"thousand",
|
|
116
|
+
"million",
|
|
117
|
+
"billion",
|
|
118
|
+
"trillion",
|
|
119
|
+
"quadrillion",
|
|
120
|
+
"quintillion",
|
|
121
|
+
"sextillion",
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
function threeDigitWords(value: number): string {
|
|
125
|
+
const parts: string[] = [];
|
|
126
|
+
let v = value;
|
|
127
|
+
if (v >= 100) {
|
|
128
|
+
parts.push(SMALL_WORDS[Math.floor(v / 100)]);
|
|
129
|
+
parts.push("hundred");
|
|
130
|
+
v %= 100;
|
|
131
|
+
}
|
|
132
|
+
if (v >= 20) {
|
|
133
|
+
parts.push(TENS_WORDS[Math.floor(v / 10)]);
|
|
134
|
+
v %= 10;
|
|
135
|
+
if (v > 0) parts.push(SMALL_WORDS[v]);
|
|
136
|
+
} else if (v > 0 || parts.length === 0) {
|
|
137
|
+
parts.push(SMALL_WORDS[v]);
|
|
138
|
+
}
|
|
139
|
+
return parts.join(" ").trim();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const BIG_ZERO = BigInt(0);
|
|
143
|
+
const BIG_THOUSAND = BigInt(1000);
|
|
144
|
+
|
|
145
|
+
function integerToWords(value: bigint): string {
|
|
146
|
+
if (value === BIG_ZERO) return "zero";
|
|
147
|
+
const parts: string[] = [];
|
|
148
|
+
let remaining = value;
|
|
149
|
+
let scale = 0;
|
|
150
|
+
while (remaining > BIG_ZERO) {
|
|
151
|
+
const chunk = Number(remaining % BIG_THOUSAND);
|
|
152
|
+
if (chunk > 0) {
|
|
153
|
+
const words = threeDigitWords(chunk);
|
|
154
|
+
const scaleWord = SCALE_WORDS[scale];
|
|
155
|
+
parts.unshift(scaleWord ? `${words} ${scaleWord}`.trim() : words);
|
|
156
|
+
}
|
|
157
|
+
remaining /= BIG_THOUSAND;
|
|
158
|
+
scale += 1;
|
|
159
|
+
}
|
|
160
|
+
return parts.join(" ").trim();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function expandNumberToken(token: string): string {
|
|
164
|
+
let working = token;
|
|
165
|
+
const result: string[] = [];
|
|
166
|
+
let negative = false;
|
|
167
|
+
if (working.startsWith("-")) {
|
|
168
|
+
negative = true;
|
|
169
|
+
working = working.slice(1);
|
|
170
|
+
}
|
|
171
|
+
if (!working) working = "0";
|
|
172
|
+
const [intPart, decPart] = working.split(".");
|
|
173
|
+
const integerValue = intPart ? BigInt(intPart) : BIG_ZERO;
|
|
174
|
+
const integerWords = integerToWords(integerValue);
|
|
175
|
+
if (negative) result.push("minus");
|
|
176
|
+
result.push(integerWords);
|
|
177
|
+
if (decPart) {
|
|
178
|
+
result.push("point");
|
|
179
|
+
for (const digit of decPart) {
|
|
180
|
+
const value = Number(digit);
|
|
181
|
+
if (!Number.isNaN(value) && value >= 0 && value <= 9) {
|
|
182
|
+
result.push(DIGIT_WORDS[value]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return result.join(" ");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function expandNumbers(input: string): string {
|
|
190
|
+
return input.replace(/-?\d+(\.\d+)?/g, expandNumberToken);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function expandCurrencySymbols(input: string): string {
|
|
194
|
+
let text = input.replace(CURRENCY_SYMBOL_BEFORE_NUMBER_REGEX, (_, symbol, digits) => {
|
|
195
|
+
const word = CURRENCY_SYMBOL_WORDS[symbol];
|
|
196
|
+
return word ? `${digits} ${word}` : `${symbol}${digits}`;
|
|
197
|
+
});
|
|
198
|
+
text = text.replace(CURRENCY_SYMBOL_AFTER_NUMBER_REGEX, (_, digits, symbol) => {
|
|
199
|
+
const word = CURRENCY_SYMBOL_WORDS[symbol];
|
|
200
|
+
return word ? `${digits} ${word}` : `${digits}${symbol}`;
|
|
201
|
+
});
|
|
202
|
+
return text.replace(CURRENCY_SYMBOL_REGEX, (symbol) => {
|
|
203
|
+
const word = CURRENCY_SYMBOL_WORDS[symbol];
|
|
204
|
+
return word ? ` ${word} ` : " ";
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function expandPercents(input: string): string {
|
|
209
|
+
// Match number-followed-by-% (with optional space). "50%" -> "50 percent".
|
|
210
|
+
// Runs before number expansion so the digits still parse cleanly.
|
|
211
|
+
return input.replace(/([0-9]+(?:\.[0-9]+)?)\s*%/g, "$1 percent");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Expand currency symbols, numbers, and percentages into their spoken
|
|
216
|
+
* word forms. Preserves punctuation and surrounding text — this is the
|
|
217
|
+
* surface that becomes `speechtext`.
|
|
218
|
+
*/
|
|
219
|
+
export function expandSpokenForms(input: string): string {
|
|
220
|
+
let text = input.normalize("NFKC");
|
|
221
|
+
text = expandPercents(text);
|
|
222
|
+
text = expandCurrencySymbols(text);
|
|
223
|
+
text = text.replace(/(?<=\d),(?=\d)/g, "");
|
|
224
|
+
text = expandNumbers(text);
|
|
225
|
+
// Collapse any double-spaces introduced by symbol expansion.
|
|
226
|
+
return text.replace(/[ \t]+/g, " ");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Strip a speechtext-form string into the alignment form: lowercase,
|
|
231
|
+
* punctuation removed, whitespace collapsed. Contractions are preserved
|
|
232
|
+
* (e.g. "don't" stays one token).
|
|
233
|
+
*/
|
|
234
|
+
export function stripForAlignment(input: string): string {
|
|
235
|
+
let text = input.normalize("NFKC");
|
|
236
|
+
// Strip control (C0/C1) + DEL, then zero-width / BiDi formatting chars.
|
|
237
|
+
// Code-point filtering avoids embedding raw control bytes in a regex literal.
|
|
238
|
+
text = Array.from(text)
|
|
239
|
+
.map((ch) => {
|
|
240
|
+
const c = ch.codePointAt(0) ?? 0;
|
|
241
|
+
if (c <= 0x1f || (c >= 0x7f && c <= 0x9f)) return " ";
|
|
242
|
+
if (c === 0xa0) return " ";
|
|
243
|
+
if (
|
|
244
|
+
(c >= 0x200b && c <= 0x200f) ||
|
|
245
|
+
(c >= 0x202a && c <= 0x202e) ||
|
|
246
|
+
(c >= 0x2060 && c <= 0x2064)
|
|
247
|
+
) {
|
|
248
|
+
return "";
|
|
249
|
+
}
|
|
250
|
+
return ch;
|
|
251
|
+
})
|
|
252
|
+
.join("");
|
|
253
|
+
// Quote / dash normalisation.
|
|
254
|
+
text = text.replace(/[“”„‟]/g, '"');
|
|
255
|
+
text = text.replace(/[‘’‚‛]/g, "'");
|
|
256
|
+
text = text.replace(/…/g, " ");
|
|
257
|
+
text = text.replace(/[-‐‑⁃–—−]/g, " ");
|
|
258
|
+
// Strip emoji + URLs + emails + handles before the catch-all punctuation pass.
|
|
259
|
+
text = text.replace(/\p{Extended_Pictographic}/gu, " ");
|
|
260
|
+
text = text.replace(/https?:\/\/\S+/gi, " ");
|
|
261
|
+
text = text.replace(/\bwww\.[^\s/]+\b/gi, " ");
|
|
262
|
+
text = text.replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/gi, " ");
|
|
263
|
+
text = text.replace(/@\w+/g, " ");
|
|
264
|
+
// NFKD-then-strip-marks to drop combining diacritics.
|
|
265
|
+
text = text.normalize("NFKD").replace(/\p{M}/gu, "").normalize("NFKC");
|
|
266
|
+
// Strip stray apostrophes that aren't part of a contraction.
|
|
267
|
+
text = text.replace(/(^|[^A-Za-z0-9])'(?=[^A-Za-z0-9]|$)/g, "$1");
|
|
268
|
+
text = text.replace(/(^|[^A-Za-z0-9])'(?=[A-Za-z0-9])/g, "$1");
|
|
269
|
+
// Everything that's not a contraction-letter, digit, or whitespace -> space.
|
|
270
|
+
text = text.replace(/[^A-Za-z0-9'\s]/g, " ");
|
|
271
|
+
text = text.replace(/\s+/g, " ").trim();
|
|
272
|
+
return text.toLowerCase();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Produce both the speechtext (POSTed to synthesis) and
|
|
277
|
+
* normalisedSpeechtext (POSTed as the alignment transcript) forms from a
|
|
278
|
+
* single plain-text input.
|
|
279
|
+
*/
|
|
280
|
+
export function buildSpeechtextPair(plainText: string): {
|
|
281
|
+
speechtext: string;
|
|
282
|
+
normalisedSpeechtext: string;
|
|
283
|
+
} {
|
|
284
|
+
const speechtext = expandSpokenForms(plainText);
|
|
285
|
+
const normalisedSpeechtext = stripForAlignment(speechtext);
|
|
286
|
+
return { speechtext, normalisedSpeechtext };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* How many alignment tokens a visible string expands to. Used by the
|
|
291
|
+
* rehype plugin to compute `data-n` for each wrapped word / math unit.
|
|
292
|
+
* "$50" -> 2 ("fifty dollar"); "1995" -> 6; unambiguous words -> 1.
|
|
293
|
+
*/
|
|
294
|
+
export function spokenTokenCount(visible: string): number {
|
|
295
|
+
const normalised = stripForAlignment(expandSpokenForms(visible));
|
|
296
|
+
if (!normalised) return 0;
|
|
297
|
+
return normalised.split(/\s+/).filter(Boolean).length;
|
|
298
|
+
}
|
|
@@ -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
|
-
|
|
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
|
|
@@ -64,7 +63,7 @@ async function init(): Promise<boolean> {
|
|
|
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:
|
|
66
|
+
console.warn("[vocasync] Install with: bun add mathjax-full speech-rule-engine");
|
|
68
67
|
return false;
|
|
69
68
|
}
|
|
70
69
|
})();
|