@vocasync/astro 0.1.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 +580 -0
- package/dist/api/client.d.ts +77 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +223 -0
- package/dist/api/client.js.map +1 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +257 -0
- package/dist/cli.js.map +1 -0
- package/dist/components/AudioPlayer.astro +1097 -0
- package/dist/components/index.d.ts +19 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +4 -0
- package/dist/components/index.js.map +1 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/schema.d.ts +275 -0
- package/dist/config/schema.d.ts.map +1 -0
- package/dist/config/schema.js +124 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/core/audio-map.d.ts +52 -0
- package/dist/core/audio-map.d.ts.map +1 -0
- package/dist/core/audio-map.js +97 -0
- package/dist/core/audio-map.js.map +1 -0
- package/dist/core/content-loader.d.ts +17 -0
- package/dist/core/content-loader.d.ts.map +1 -0
- package/dist/core/content-loader.js +141 -0
- package/dist/core/content-loader.js.map +1 -0
- package/dist/core/hash-manager.d.ts +16 -0
- package/dist/core/hash-manager.d.ts.map +1 -0
- package/dist/core/hash-manager.js +21 -0
- package/dist/core/hash-manager.js.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +5 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/speech-builder.d.ts +19 -0
- package/dist/core/speech-builder.d.ts.map +1 -0
- package/dist/core/speech-builder.js +252 -0
- package/dist/core/speech-builder.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/integration.d.ts +27 -0
- package/dist/integration.d.ts.map +1 -0
- package/dist/integration.js +110 -0
- package/dist/integration.js.map +1 -0
- package/dist/lib/latex-to-speech/index.d.ts +26 -0
- package/dist/lib/latex-to-speech/index.d.ts.map +1 -0
- package/dist/lib/latex-to-speech/index.js +101 -0
- package/dist/lib/latex-to-speech/index.js.map +1 -0
- package/dist/rehype/audio-words.d.ts +50 -0
- package/dist/rehype/audio-words.d.ts.map +1 -0
- package/dist/rehype/audio-words.js +269 -0
- package/dist/rehype/audio-words.js.map +1 -0
- package/dist/rehype/index.d.ts +6 -0
- package/dist/rehype/index.d.ts.map +1 -0
- package/dist/rehype/index.js +4 -0
- package/dist/rehype/index.js.map +1 -0
- package/dist/rehype/rehype-math-speech.d.ts +18 -0
- package/dist/rehype/rehype-math-speech.d.ts.map +1 -0
- package/dist/rehype/rehype-math-speech.js +117 -0
- package/dist/rehype/rehype-math-speech.js.map +1 -0
- package/dist/rehype/remark-math-speech.d.ts +13 -0
- package/dist/rehype/remark-math-speech.d.ts.map +1 -0
- package/dist/rehype/remark-math-speech.js +26 -0
- package/dist/rehype/remark-math-speech.js.map +1 -0
- package/dist/styles/variables.css +181 -0
- package/dist/sync/index.d.ts +3 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +2 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/orchestrator.d.ts +36 -0
- package/dist/sync/orchestrator.d.ts.map +1 -0
- package/dist/sync/orchestrator.js +166 -0
- package/dist/sync/orchestrator.js.map +1 -0
- package/dist/types/index.d.ts +196 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +89 -0
- package/src/api/client.ts +316 -0
- package/src/api/index.ts +1 -0
- package/src/cli.ts +301 -0
- package/src/components/AudioPlayer.astro +1097 -0
- package/src/components/index.ts +24 -0
- package/src/config/index.ts +30 -0
- package/src/config/schema.ts +177 -0
- package/src/core/audio-map.ts +107 -0
- package/src/core/content-loader.ts +171 -0
- package/src/core/hash-manager.ts +22 -0
- package/src/core/index.ts +12 -0
- package/src/core/speech-builder.ts +307 -0
- package/src/data/audio-map.example.json +19 -0
- package/src/env.d.ts +13 -0
- package/src/index.ts +67 -0
- package/src/integration.ts +123 -0
- package/src/lib/latex-to-speech/index.ts +126 -0
- package/src/rehype/audio-words.ts +345 -0
- package/src/rehype/index.ts +6 -0
- package/src/rehype/rehype-math-speech.ts +145 -0
- package/src/rehype/remark-math-speech.ts +31 -0
- package/src/styles/variables.css +181 -0
- package/src/sync/index.ts +2 -0
- package/src/sync/orchestrator.ts +239 -0
- package/src/types/index.ts +211 -0
- package/src/types/modules.d.ts +11 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { unified } from "unified";
|
|
2
|
+
import remarkParse from "remark-parse";
|
|
3
|
+
import remarkMath from "remark-math";
|
|
4
|
+
import type { Root, Content, Text, InlineCode, Code, Heading, Emphasis, Strong } from "mdast";
|
|
5
|
+
import type { ContentItem, SpeechDocument } from "../types/index.js";
|
|
6
|
+
import type { MathConfig } from "../config/index.js";
|
|
7
|
+
import { computeHash } from "./hash-manager.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* LaTeX to speech converter interface
|
|
11
|
+
*/
|
|
12
|
+
interface LaTeXConverter {
|
|
13
|
+
convert(latex: string, display: boolean): Promise<string>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Cache for latex-to-speech results
|
|
17
|
+
const latexSpeechCache = new Map<string, Promise<string>>();
|
|
18
|
+
let latexToSpeech: ((exprs: string[], options?: Record<string, unknown>) => Promise<string[]>) | null = null;
|
|
19
|
+
let latexToSpeechLoadAttempted = false;
|
|
20
|
+
let warnedLatexSpeechFailure = false;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Normalize whitespace in text.
|
|
24
|
+
*/
|
|
25
|
+
function normalizeWhitespace(value = ""): string {
|
|
26
|
+
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Create a lazy-loaded LaTeX converter using our internal latex-to-speech module.
|
|
31
|
+
* This uses speech-rule-engine and mathjax-full as optional peer dependencies.
|
|
32
|
+
*/
|
|
33
|
+
async function createLatexConverter(config: MathConfig): Promise<LaTeXConverter | null> {
|
|
34
|
+
if (!config.enabled) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Lazy load our internal latex-to-speech module
|
|
39
|
+
if (!latexToSpeechLoadAttempted) {
|
|
40
|
+
latexToSpeechLoadAttempted = true;
|
|
41
|
+
try {
|
|
42
|
+
const mod = await import("../lib/latex-to-speech/index.js");
|
|
43
|
+
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.warn(
|
|
46
|
+
"[vocasync] Math-to-speech initialization failed. Install optional dependencies:",
|
|
47
|
+
"\n npm install speech-rule-engine mathjax-full"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!latexToSpeech) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const options = {
|
|
57
|
+
domain: config.style || "clearspeak",
|
|
58
|
+
style: "default",
|
|
59
|
+
locale: "en",
|
|
60
|
+
modality: "speech",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
async convert(latex: string, display: boolean): Promise<string> {
|
|
65
|
+
const trimmed = latex.trim();
|
|
66
|
+
if (!trimmed) return "";
|
|
67
|
+
|
|
68
|
+
const cacheKey = `${display}:${trimmed}`;
|
|
69
|
+
if (!latexSpeechCache.has(cacheKey)) {
|
|
70
|
+
const pending = (async () => {
|
|
71
|
+
try {
|
|
72
|
+
const result = await latexToSpeech!([trimmed], options);
|
|
73
|
+
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
74
|
+
return normalizeWhitespace(spoken);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (!warnedLatexSpeechFailure) {
|
|
77
|
+
console.warn(
|
|
78
|
+
"[vocasync] Failed to convert LaTeX to speech:",
|
|
79
|
+
error instanceof Error ? error.message : error
|
|
80
|
+
);
|
|
81
|
+
warnedLatexSpeechFailure = true;
|
|
82
|
+
}
|
|
83
|
+
return normalizeWhitespace(trimmed);
|
|
84
|
+
}
|
|
85
|
+
})();
|
|
86
|
+
latexSpeechCache.set(cacheKey, pending);
|
|
87
|
+
}
|
|
88
|
+
return latexSpeechCache.get(cacheKey)!;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Options for building speech document
|
|
95
|
+
*/
|
|
96
|
+
interface BuildOptions {
|
|
97
|
+
math?: MathConfig;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Build a speech document from markdown content.
|
|
102
|
+
* Converts markdown to plain text suitable for TTS synthesis.
|
|
103
|
+
*
|
|
104
|
+
* @param item - The content item to process
|
|
105
|
+
* @param options - Build options including math configuration
|
|
106
|
+
* @returns A speech document ready for synthesis
|
|
107
|
+
*/
|
|
108
|
+
export async function buildSpeechDocument(
|
|
109
|
+
item: ContentItem,
|
|
110
|
+
options: BuildOptions = {}
|
|
111
|
+
): Promise<SpeechDocument> {
|
|
112
|
+
const mathConfig = options.math ?? { enabled: false, style: "clearspeak" };
|
|
113
|
+
const latexConverter = await createLatexConverter(mathConfig);
|
|
114
|
+
|
|
115
|
+
// Parse markdown with math support
|
|
116
|
+
const processor = unified().use(remarkParse).use(remarkMath);
|
|
117
|
+
|
|
118
|
+
const tree = processor.parse(item.content) as Root;
|
|
119
|
+
|
|
120
|
+
// Extract text from AST - can contain strings or promises
|
|
121
|
+
const textParts: (string | Promise<string>)[] = [];
|
|
122
|
+
|
|
123
|
+
function processNode(node: Content | Root): void {
|
|
124
|
+
switch (node.type) {
|
|
125
|
+
case "text": {
|
|
126
|
+
const textNode = node as Text;
|
|
127
|
+
textParts.push(textNode.value);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
case "inlineCode": {
|
|
132
|
+
const codeNode = node as InlineCode;
|
|
133
|
+
// Skip short code, read longer code
|
|
134
|
+
if (codeNode.value.length > 10) {
|
|
135
|
+
textParts.push(codeNode.value);
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
case "code": {
|
|
141
|
+
const blockCode = node as Code;
|
|
142
|
+
// Skip code blocks entirely for TTS
|
|
143
|
+
// Could optionally add language hint: "Code block in {lang}"
|
|
144
|
+
if (blockCode.lang) {
|
|
145
|
+
textParts.push(`[Code in ${blockCode.lang}]`);
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
case "heading": {
|
|
151
|
+
const heading = node as Heading;
|
|
152
|
+
// Add single # marker for synthesis chunking (backend expects single # for all heading levels)
|
|
153
|
+
textParts.push("\n# ");
|
|
154
|
+
// Process children
|
|
155
|
+
for (const child of heading.children) {
|
|
156
|
+
processNode(child as Content);
|
|
157
|
+
}
|
|
158
|
+
// Add newline after headings for separation
|
|
159
|
+
textParts.push("\n");
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
case "paragraph": {
|
|
164
|
+
const para = node as { children: Content[] };
|
|
165
|
+
for (const child of para.children) {
|
|
166
|
+
processNode(child);
|
|
167
|
+
}
|
|
168
|
+
// Paragraph break
|
|
169
|
+
textParts.push("\n");
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
case "emphasis": {
|
|
174
|
+
const em = node as Emphasis;
|
|
175
|
+
for (const child of em.children) {
|
|
176
|
+
processNode(child as Content);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
case "strong": {
|
|
182
|
+
const strong = node as Strong;
|
|
183
|
+
for (const child of strong.children) {
|
|
184
|
+
processNode(child as Content);
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
case "link": {
|
|
190
|
+
const link = node as { children: Content[] };
|
|
191
|
+
// Only read link text, not URL
|
|
192
|
+
for (const child of link.children) {
|
|
193
|
+
processNode(child);
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
case "image": {
|
|
199
|
+
const img = node as { alt?: string };
|
|
200
|
+
// Read alt text
|
|
201
|
+
if (img.alt) {
|
|
202
|
+
textParts.push(`Image: ${img.alt}`);
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
case "list": {
|
|
208
|
+
const list = node as { children: Content[] };
|
|
209
|
+
for (const child of list.children) {
|
|
210
|
+
processNode(child);
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
case "listItem": {
|
|
216
|
+
const listItem = node as { children: Content[] };
|
|
217
|
+
for (const child of listItem.children) {
|
|
218
|
+
processNode(child);
|
|
219
|
+
}
|
|
220
|
+
textParts.push(". ");
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
case "blockquote": {
|
|
225
|
+
const quote = node as { children: Content[] };
|
|
226
|
+
textParts.push("Quote: ");
|
|
227
|
+
for (const child of quote.children) {
|
|
228
|
+
processNode(child);
|
|
229
|
+
}
|
|
230
|
+
textParts.push(" End quote.");
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Math nodes from remark-math
|
|
235
|
+
case "math": {
|
|
236
|
+
const mathNode = node as { value: string };
|
|
237
|
+
if (latexConverter) {
|
|
238
|
+
textParts.push(latexConverter.convert(mathNode.value, true));
|
|
239
|
+
} else {
|
|
240
|
+
textParts.push(`[Math: ${mathNode.value.slice(0, 30)}]`);
|
|
241
|
+
}
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
case "inlineMath": {
|
|
246
|
+
const inlineMathNode = node as { value: string };
|
|
247
|
+
if (latexConverter) {
|
|
248
|
+
textParts.push(latexConverter.convert(inlineMathNode.value, false));
|
|
249
|
+
} else {
|
|
250
|
+
textParts.push(inlineMathNode.value);
|
|
251
|
+
}
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
case "thematicBreak": {
|
|
256
|
+
textParts.push("\n\n");
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Handle container nodes with children
|
|
261
|
+
case "root": {
|
|
262
|
+
const root = node as Root;
|
|
263
|
+
for (const child of root.children) {
|
|
264
|
+
processNode(child);
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
default: {
|
|
270
|
+
// Try to process children if they exist
|
|
271
|
+
const maybeParent = node as { children?: Content[] };
|
|
272
|
+
if (maybeParent.children) {
|
|
273
|
+
for (const child of maybeParent.children) {
|
|
274
|
+
processNode(child);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
processNode(tree);
|
|
282
|
+
|
|
283
|
+
// Resolve all promises in textParts (for async math conversion)
|
|
284
|
+
const resolvedParts = await Promise.all(textParts);
|
|
285
|
+
|
|
286
|
+
// Clean up and normalize text
|
|
287
|
+
let text = resolvedParts
|
|
288
|
+
.join(" ")
|
|
289
|
+
.replace(/\s+/g, " ") // Normalize whitespace
|
|
290
|
+
.replace(/\n\s*\n/g, "\n\n") // Preserve paragraph breaks
|
|
291
|
+
.replace(/\s*\.\s*\./g, ".") // Clean up double periods
|
|
292
|
+
.trim();
|
|
293
|
+
|
|
294
|
+
// Note: We intentionally do NOT include the title in the speech text.
|
|
295
|
+
// The title is displayed visually and doesn't need to be in the audio.
|
|
296
|
+
// This also ensures alignment data matches the article body content.
|
|
297
|
+
|
|
298
|
+
// Compute hash for change detection
|
|
299
|
+
const hash = computeHash(text);
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
slug: item.slug,
|
|
303
|
+
text,
|
|
304
|
+
hash,
|
|
305
|
+
source: item.filePath,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"updatedAt": "2024-01-01T00:00:00.000Z",
|
|
4
|
+
"entries": {
|
|
5
|
+
"example-post": {
|
|
6
|
+
"projectUuid": "550e8400-e29b-41d4-a716-446655440000",
|
|
7
|
+
"contentHash": "abc123def456...",
|
|
8
|
+
"duration": 125.5,
|
|
9
|
+
"audioUrl": "https://vocasync.io/api/v1/stream/550e8400-e29b-41d4-a716-446655440000/audio.mp3",
|
|
10
|
+
"alignmentUrl": "https://vocasync.io/api/v1/stream/550e8400-e29b-41d4-a716-446655440000/alignment.json",
|
|
11
|
+
"words": [
|
|
12
|
+
{ "word": "Hello", "start": 0.0, "end": 0.35 },
|
|
13
|
+
{ "word": "world", "start": 0.35, "end": 0.75 }
|
|
14
|
+
],
|
|
15
|
+
"createdAt": "2024-01-01T00:00:00.000Z",
|
|
16
|
+
"updatedAt": "2024-01-01T00:00:00.000Z"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="astro/client" />
|
|
2
|
+
|
|
3
|
+
declare module "virtual:vocasync/audio-map" {
|
|
4
|
+
import type { AudioMap } from "@vocasync/astro";
|
|
5
|
+
const audioMap: AudioMap;
|
|
6
|
+
export default audioMap;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare module "virtual:vocasync/config" {
|
|
10
|
+
import type { VocaSyncConfig } from "@vocasync/astro";
|
|
11
|
+
const config: VocaSyncConfig;
|
|
12
|
+
export default config;
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vocasync/astro - VocaSync integration for Astro
|
|
3
|
+
*
|
|
4
|
+
* Text-to-speech synthesis and word-level alignment for Astro static sites.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // astro.config.ts
|
|
9
|
+
* import { defineConfig } from "astro/config";
|
|
10
|
+
* import vocasync from "@vocasync/astro";
|
|
11
|
+
*
|
|
12
|
+
* export default defineConfig({
|
|
13
|
+
* integrations: [
|
|
14
|
+
* vocasync({
|
|
15
|
+
* collection: {
|
|
16
|
+
* name: "blog",
|
|
17
|
+
* path: "./src/content/blog",
|
|
18
|
+
* },
|
|
19
|
+
* language: "en-US",
|
|
20
|
+
* }),
|
|
21
|
+
* ],
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
// Astro Integration
|
|
27
|
+
export { default } from "./integration.js";
|
|
28
|
+
export { default as vocasync } from "./integration.js";
|
|
29
|
+
|
|
30
|
+
// Configuration
|
|
31
|
+
export {
|
|
32
|
+
validateConfig,
|
|
33
|
+
defaultConfig,
|
|
34
|
+
VocaSyncConfigSchema,
|
|
35
|
+
VoiceSchema,
|
|
36
|
+
QualitySchema,
|
|
37
|
+
FormatSchema,
|
|
38
|
+
LanguageSchema,
|
|
39
|
+
} from "./config/index.js";
|
|
40
|
+
|
|
41
|
+
export type {
|
|
42
|
+
VocaSyncConfig,
|
|
43
|
+
VocaSyncUserConfig,
|
|
44
|
+
Voice,
|
|
45
|
+
Quality,
|
|
46
|
+
Format,
|
|
47
|
+
Language,
|
|
48
|
+
MathStyle,
|
|
49
|
+
} from "./config/index.js";
|
|
50
|
+
|
|
51
|
+
// Types
|
|
52
|
+
export type {
|
|
53
|
+
ContentItem,
|
|
54
|
+
SpeechDocument,
|
|
55
|
+
AlignedWord,
|
|
56
|
+
AudioArtifact,
|
|
57
|
+
AudioMap,
|
|
58
|
+
SyncResult,
|
|
59
|
+
SyncSummary,
|
|
60
|
+
SyncStatus,
|
|
61
|
+
} from "./types/index.js";
|
|
62
|
+
|
|
63
|
+
// Core utilities (for advanced usage)
|
|
64
|
+
export { loadContent } from "./core/content-loader.js";
|
|
65
|
+
export { buildSpeechDocument } from "./core/speech-builder.js";
|
|
66
|
+
export { computeHash } from "./core/hash-manager.js";
|
|
67
|
+
export { loadAudioMap, saveAudioMap, getAudioEntry } from "./core/audio-map.js";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { validateConfig, type VocaSyncUserConfig } from "./config/index.js";
|
|
5
|
+
import { loadAudioMap } from "./core/audio-map.js";
|
|
6
|
+
import type { AudioMap } from "./types/index.js";
|
|
7
|
+
|
|
8
|
+
// Cache for the loaded audio map
|
|
9
|
+
let audioMapCache: AudioMap | null = null;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Load configuration from vocasync.config.ts/js/mjs.
|
|
13
|
+
*/
|
|
14
|
+
async function loadVocaSyncConfig(): Promise<VocaSyncUserConfig> {
|
|
15
|
+
const cwd = process.cwd();
|
|
16
|
+
|
|
17
|
+
for (const ext of [".ts", ".mjs", ".js"]) {
|
|
18
|
+
const configFile = resolve(cwd, `vocasync.config${ext}`);
|
|
19
|
+
if (existsSync(configFile)) {
|
|
20
|
+
const fileUrl = `file://${configFile}`;
|
|
21
|
+
const module = await import(fileUrl);
|
|
22
|
+
return (module.default || module) as VocaSyncUserConfig;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
throw new Error(
|
|
27
|
+
"No VocaSync configuration found.\n" +
|
|
28
|
+
"Create a vocasync.config.mjs file with your configuration."
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* VocaSync Astro Integration.
|
|
34
|
+
*
|
|
35
|
+
* Provides:
|
|
36
|
+
* - Virtual module `virtual:vocasync/audio-map` for accessing audio data
|
|
37
|
+
* - Automatic rehype plugin for word-level highlighting
|
|
38
|
+
* - Optional build-time sync command
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* // astro.config.mjs
|
|
43
|
+
* import vocasync from "@vocasync/astro";
|
|
44
|
+
*
|
|
45
|
+
* export default defineConfig({
|
|
46
|
+
* integrations: [vocasync()],
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // vocasync.config.mjs
|
|
50
|
+
* export default {
|
|
51
|
+
* collection: { name: "blog", path: "./src/content/blog" },
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export default function vocasyncIntegration(): AstroIntegration {
|
|
56
|
+
return {
|
|
57
|
+
name: "@vocasync/astro",
|
|
58
|
+
hooks: {
|
|
59
|
+
"astro:config:setup": async ({ updateConfig, logger }) => {
|
|
60
|
+
// Load config from vocasync.config.mjs
|
|
61
|
+
let config;
|
|
62
|
+
try {
|
|
63
|
+
const userConfig = await loadVocaSyncConfig();
|
|
64
|
+
config = validateConfig(userConfig);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
logger.error(`Failed to load VocaSync config: ${error}`);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
logger.info("VocaSync integration loaded");
|
|
71
|
+
|
|
72
|
+
// Pre-load audio map for rehype plugin
|
|
73
|
+
try {
|
|
74
|
+
audioMapCache = await loadAudioMap(config.output.audioMapPath);
|
|
75
|
+
logger.info(`Loaded audio map with ${Object.keys(audioMapCache.entries).length} entries`);
|
|
76
|
+
} catch {
|
|
77
|
+
logger.warn(
|
|
78
|
+
`Audio map not found at ${config.output.audioMapPath}. Run 'npx vocasync sync' first.`
|
|
79
|
+
);
|
|
80
|
+
audioMapCache = { version: 1, updatedAt: "", entries: {} };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Add virtual module for audio-map access
|
|
84
|
+
updateConfig({
|
|
85
|
+
vite: {
|
|
86
|
+
plugins: [
|
|
87
|
+
{
|
|
88
|
+
name: "vite-plugin-vocasync",
|
|
89
|
+
resolveId(id: string) {
|
|
90
|
+
if (id === "virtual:vocasync/audio-map") {
|
|
91
|
+
return "\0virtual:vocasync/audio-map";
|
|
92
|
+
}
|
|
93
|
+
if (id === "virtual:vocasync/config") {
|
|
94
|
+
return "\0virtual:vocasync/config";
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
},
|
|
98
|
+
async load(id: string) {
|
|
99
|
+
if (id === "\0virtual:vocasync/audio-map") {
|
|
100
|
+
// Return cached audio map
|
|
101
|
+
return `export default ${JSON.stringify(audioMapCache)};`;
|
|
102
|
+
}
|
|
103
|
+
if (id === "\0virtual:vocasync/config") {
|
|
104
|
+
// Export the validated config
|
|
105
|
+
return `export default ${JSON.stringify(config)};`;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
"astro:build:done": ({ logger }) => {
|
|
116
|
+
logger.info("VocaSync build complete");
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Also export as named export
|
|
123
|
+
export { vocasyncIntegration as vocasync };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LaTeX to Speech conversion using speech-rule-engine and mathjax-full.
|
|
3
|
+
*
|
|
4
|
+
* This module provides the same interface as the vendored latex-to-speech package
|
|
5
|
+
* but works with the optional peer dependencies instead of bundling MathJax.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
let SRE: any = null;
|
|
9
|
+
let tex2mml: ((latex: string) => string) | null = null;
|
|
10
|
+
let engineSetup = false;
|
|
11
|
+
let initPromise: Promise<boolean> | null = null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the speech-rule-engine and MathJax tex2mml converter.
|
|
15
|
+
* Returns true if successful, false if dependencies are not available.
|
|
16
|
+
*/
|
|
17
|
+
async function init(): Promise<boolean> {
|
|
18
|
+
if (initPromise) return initPromise;
|
|
19
|
+
|
|
20
|
+
initPromise = (async () => {
|
|
21
|
+
try {
|
|
22
|
+
// Dynamically import speech-rule-engine
|
|
23
|
+
SRE = await import("speech-rule-engine");
|
|
24
|
+
} catch {
|
|
25
|
+
console.warn("[vocasync] speech-rule-engine not installed. Math-to-speech disabled.");
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
// Dynamically import mathjax-full components for tex2mml
|
|
31
|
+
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
32
|
+
const { TeX } = await import("mathjax-full/js/input/tex.js");
|
|
33
|
+
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
34
|
+
const { HTMLDocument } = await import("mathjax-full/js/handlers/html/HTMLDocument.js");
|
|
35
|
+
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
36
|
+
const { liteAdaptor } = await import("mathjax-full/js/adaptors/liteAdaptor.js");
|
|
37
|
+
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
38
|
+
const { SerializedMmlVisitor } = await import("mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
|
|
39
|
+
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
40
|
+
const { AllPackages } = await import("mathjax-full/js/input/tex/AllPackages.js");
|
|
41
|
+
|
|
42
|
+
// Filter out problematic packages
|
|
43
|
+
const packages = AllPackages.filter((p: string) => p !== "bussproofs");
|
|
44
|
+
|
|
45
|
+
// Create TeX input jax with all packages
|
|
46
|
+
const texInput = new TeX({ packages });
|
|
47
|
+
|
|
48
|
+
// Create a minimal HTML document for conversion
|
|
49
|
+
const adaptor = liteAdaptor();
|
|
50
|
+
const doc = new HTMLDocument("", adaptor, { InputJax: texInput });
|
|
51
|
+
|
|
52
|
+
// Create a visitor to serialize to MathML
|
|
53
|
+
const visitor = new SerializedMmlVisitor();
|
|
54
|
+
|
|
55
|
+
// Create the tex2mml function
|
|
56
|
+
tex2mml = (latex: string): string => {
|
|
57
|
+
const node = doc.convert(latex, { display: true });
|
|
58
|
+
return visitor.visitTree(node);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return true;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.warn("[vocasync] mathjax-full not installed. Math-to-speech disabled.");
|
|
64
|
+
console.warn("[vocasync] Install with: npm install mathjax-full speech-rule-engine");
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
|
|
69
|
+
return initPromise;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Options for speech-rule-engine
|
|
74
|
+
*/
|
|
75
|
+
export interface SREOptions {
|
|
76
|
+
locale?: string;
|
|
77
|
+
domain?: string;
|
|
78
|
+
style?: string;
|
|
79
|
+
modality?: string;
|
|
80
|
+
speech?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert an array of LaTeX expressions to speech.
|
|
85
|
+
*
|
|
86
|
+
* @param exprs - Array of LaTeX expressions to convert
|
|
87
|
+
* @param options - SRE configuration options
|
|
88
|
+
* @returns Promise resolving to array of speech strings
|
|
89
|
+
*/
|
|
90
|
+
export async function latexToSpeech(
|
|
91
|
+
exprs: string[],
|
|
92
|
+
options: SREOptions = {}
|
|
93
|
+
): Promise<string[]> {
|
|
94
|
+
const available = await init();
|
|
95
|
+
|
|
96
|
+
if (!available || !SRE || !tex2mml) {
|
|
97
|
+
// Return empty descriptions when dependencies not available
|
|
98
|
+
return exprs.map(() => "");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Set up the engine if not already done
|
|
102
|
+
if (!engineSetup) {
|
|
103
|
+
await SRE.setupEngine({
|
|
104
|
+
locale: options.locale || "en",
|
|
105
|
+
domain: options.domain || "mathspeak",
|
|
106
|
+
style: options.style || "default",
|
|
107
|
+
modality: options.modality || "speech",
|
|
108
|
+
speech: options.speech || "deep",
|
|
109
|
+
...options,
|
|
110
|
+
});
|
|
111
|
+
engineSetup = true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Convert each expression
|
|
115
|
+
return exprs.map((latex) => {
|
|
116
|
+
try {
|
|
117
|
+
const mml = tex2mml!(latex);
|
|
118
|
+
return SRE!.toSpeech(mml);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.warn(`[vocasync] Failed to convert LaTeX to speech: ${latex}`);
|
|
121
|
+
return "";
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export default latexToSpeech;
|