@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,345 @@
|
|
|
1
|
+
import type { Root, Element, Text } from "hast";
|
|
2
|
+
import type { Plugin } from "unified";
|
|
3
|
+
import { visitParents } from "unist-util-visit-parents";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
import { execSync } from "node:child_process";
|
|
7
|
+
import type { AlignedWord, AudioMap } from "../types/index.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Options for the rehype audio words plugin.
|
|
11
|
+
*/
|
|
12
|
+
export interface RehypeAudioWordsOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Path to the audio map JSON file.
|
|
15
|
+
* Can be absolute or relative to the project root.
|
|
16
|
+
* @default ".vocasync/audio-map.json"
|
|
17
|
+
*/
|
|
18
|
+
audioMapPath?: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* CSS class prefix for generated elements.
|
|
22
|
+
* @default "vocasync"
|
|
23
|
+
*/
|
|
24
|
+
classPrefix?: string;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The name of the content collection folder to extract slugs from.
|
|
28
|
+
* @default "articles"
|
|
29
|
+
*/
|
|
30
|
+
collectionName?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Cache the loaded audio map to avoid re-reading on every file
|
|
34
|
+
let audioMapCache: AudioMap | null = null;
|
|
35
|
+
let audioMapCachePath: string | null = null;
|
|
36
|
+
|
|
37
|
+
// Cache for fetched alignment data per slug
|
|
38
|
+
const alignmentCache: Map<string, AlignedWord[]> = new Map();
|
|
39
|
+
|
|
40
|
+
function loadAudioMapSync(audioMapPath: string): AudioMap | null {
|
|
41
|
+
// Use cache if same path
|
|
42
|
+
if (audioMapCache && audioMapCachePath === audioMapPath) {
|
|
43
|
+
return audioMapCache;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const absolutePath = resolve(process.cwd(), audioMapPath);
|
|
48
|
+
const content = readFileSync(absolutePath, "utf-8");
|
|
49
|
+
audioMapCache = JSON.parse(content) as AudioMap;
|
|
50
|
+
audioMapCachePath = audioMapPath;
|
|
51
|
+
return audioMapCache;
|
|
52
|
+
} catch {
|
|
53
|
+
// File doesn't exist or is invalid - return null
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Fetch alignment data from URL (synchronously using a worker thread approach).
|
|
60
|
+
* Since rehype plugins must be synchronous, we use a sync HTTP request.
|
|
61
|
+
*/
|
|
62
|
+
function fetchAlignmentSync(alignmentUrl: string, slug: string): AlignedWord[] | null {
|
|
63
|
+
// Check cache first
|
|
64
|
+
if (alignmentCache.has(slug)) {
|
|
65
|
+
return alignmentCache.get(slug) ?? null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
// Use sync-fetch or execSync to make a synchronous HTTP request
|
|
70
|
+
// We'll use child_process.execSync with curl as a simple solution
|
|
71
|
+
// Note: -L flag follows redirects (required for vocasync.io signed URLs)
|
|
72
|
+
const result = execSync(`curl -sL "${alignmentUrl}"`, {
|
|
73
|
+
encoding: "utf-8",
|
|
74
|
+
timeout: 30000,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const data = JSON.parse(result);
|
|
78
|
+
|
|
79
|
+
// The alignment response contains a words array
|
|
80
|
+
const words = data.words as AlignedWord[] | undefined;
|
|
81
|
+
if (words && Array.isArray(words)) {
|
|
82
|
+
alignmentCache.set(slug, words);
|
|
83
|
+
return words;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
} catch {
|
|
88
|
+
// Failed to fetch - return null
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Rehype plugin that wraps words with timing spans for audio sync.
|
|
95
|
+
*
|
|
96
|
+
* For each text node in the document, this plugin:
|
|
97
|
+
* 1. Tokenizes the text into words
|
|
98
|
+
* 2. Matches words against alignment data
|
|
99
|
+
* 3. Wraps matched words in <span> elements with data-start and data-end attributes
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* // astro.config.mjs
|
|
104
|
+
* import { defineConfig } from "astro/config";
|
|
105
|
+
* import { rehypeAudioWords } from "@vocasync/astro/rehype";
|
|
106
|
+
*
|
|
107
|
+
* export default defineConfig({
|
|
108
|
+
* markdown: {
|
|
109
|
+
* rehypePlugins: [
|
|
110
|
+
* [rehypeAudioWords, { collectionName: "articles" }]
|
|
111
|
+
* ]
|
|
112
|
+
* }
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
const rehypeAudioWords: Plugin<[RehypeAudioWordsOptions?], Root> = (options = {}) => {
|
|
117
|
+
const {
|
|
118
|
+
audioMapPath = ".vocasync/audio-map.json",
|
|
119
|
+
classPrefix = "vocasync",
|
|
120
|
+
collectionName = "articles"
|
|
121
|
+
} = options;
|
|
122
|
+
|
|
123
|
+
return (tree, file) => {
|
|
124
|
+
// Load audio map (cached after first load)
|
|
125
|
+
const audioMap = loadAudioMapSync(audioMapPath);
|
|
126
|
+
if (!audioMap) {
|
|
127
|
+
return; // No audio map available, skip processing
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Resolve slug from file path
|
|
131
|
+
// Files are typically at paths like: /src/content/articles/hello-world.md
|
|
132
|
+
const filePath = file.path ?? (Array.isArray(file.history) ? file.history[file.history.length - 1] : undefined);
|
|
133
|
+
|
|
134
|
+
if (!filePath) {
|
|
135
|
+
return; // No file path, skip processing
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Normalize path separators
|
|
139
|
+
const normalizedPath = filePath.replace(/\\/g, "/");
|
|
140
|
+
|
|
141
|
+
// Try to extract slug from collection path
|
|
142
|
+
// Pattern: /src/content/{collectionName}/{slug}.md or .mdx
|
|
143
|
+
const collectionPattern = new RegExp(`/content/${collectionName}/([^/]+)\\.(md|mdx)$`);
|
|
144
|
+
const match = normalizedPath.match(collectionPattern);
|
|
145
|
+
|
|
146
|
+
let slug: string | undefined;
|
|
147
|
+
if (match) {
|
|
148
|
+
slug = match[1];
|
|
149
|
+
} else {
|
|
150
|
+
// Fallback: just get the filename without extension
|
|
151
|
+
const filenameMatch = normalizedPath.match(/\/([^/]+)\.(md|mdx)$/);
|
|
152
|
+
if (filenameMatch) {
|
|
153
|
+
slug = filenameMatch[1];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!slug) {
|
|
158
|
+
return; // No slug, skip processing
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Get alignment URL for this slug from audio map
|
|
162
|
+
const entry = audioMap.entries[slug];
|
|
163
|
+
if (!entry?.alignmentUrl) {
|
|
164
|
+
return; // No alignment URL, skip
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Fetch alignment words from URL
|
|
168
|
+
const words = fetchAlignmentSync(entry.alignmentUrl, slug);
|
|
169
|
+
|
|
170
|
+
if (!words || words.length === 0) {
|
|
171
|
+
return; // No alignment data, skip
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Track word index as we process text nodes
|
|
175
|
+
let wordIndex = 0;
|
|
176
|
+
|
|
177
|
+
// Process all text nodes in the document
|
|
178
|
+
visitParents(tree, "text", (node, ancestors) => {
|
|
179
|
+
const textNode = node as Text;
|
|
180
|
+
const parent = ancestors[ancestors.length - 1] as Element;
|
|
181
|
+
|
|
182
|
+
// Skip text in code blocks, scripts, styles
|
|
183
|
+
if (isInNonTextualElement(ancestors)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Tokenize and match words
|
|
188
|
+
const result = processTextNode(textNode.value, words, wordIndex, classPrefix);
|
|
189
|
+
|
|
190
|
+
if (result.nodes.length === 0 || result.nodes.length === 1) {
|
|
191
|
+
return; // No changes needed
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Update word index
|
|
195
|
+
wordIndex = result.nextWordIndex;
|
|
196
|
+
|
|
197
|
+
// Replace text node with annotated nodes
|
|
198
|
+
const parentChildren = parent.children;
|
|
199
|
+
const nodeIndex = parentChildren.indexOf(node);
|
|
200
|
+
|
|
201
|
+
if (nodeIndex !== -1) {
|
|
202
|
+
parentChildren.splice(nodeIndex, 1, ...result.nodes);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Check if ancestors include non-textual elements (code, script, style, KaTeX).
|
|
210
|
+
*/
|
|
211
|
+
function isInNonTextualElement(ancestors: (Root | Element)[]): boolean {
|
|
212
|
+
const nonTextualTags = new Set(["code", "pre", "script", "style", "svg", "math"]);
|
|
213
|
+
const nonTextualClasses = new Set(["katex", "katex-mathml", "katex-html", "mjx-container"]);
|
|
214
|
+
|
|
215
|
+
for (const ancestor of ancestors) {
|
|
216
|
+
if ("tagName" in ancestor && nonTextualTags.has(ancestor.tagName)) {
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
// Check for KaTeX/MathJax class names
|
|
220
|
+
if ("properties" in ancestor && ancestor.properties?.className) {
|
|
221
|
+
const classes = Array.isArray(ancestor.properties.className)
|
|
222
|
+
? ancestor.properties.className
|
|
223
|
+
: [ancestor.properties.className];
|
|
224
|
+
for (const cls of classes) {
|
|
225
|
+
if (typeof cls === "string" && nonTextualClasses.has(cls)) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Process a text node and create annotated nodes.
|
|
236
|
+
*/
|
|
237
|
+
function processTextNode(
|
|
238
|
+
text: string,
|
|
239
|
+
words: AlignedWord[],
|
|
240
|
+
startIndex: number,
|
|
241
|
+
classPrefix: string
|
|
242
|
+
): {
|
|
243
|
+
nodes: (Text | Element)[];
|
|
244
|
+
nextWordIndex: number;
|
|
245
|
+
} {
|
|
246
|
+
const nodes: (Text | Element)[] = [];
|
|
247
|
+
let wordIndex = startIndex;
|
|
248
|
+
let lastEnd = 0;
|
|
249
|
+
|
|
250
|
+
// Regex to match words (similar to word boundaries)
|
|
251
|
+
const wordPattern = /[\p{L}\p{N}'']+/gu;
|
|
252
|
+
let match: RegExpExecArray | null;
|
|
253
|
+
|
|
254
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
|
|
255
|
+
while ((match = wordPattern.exec(text)) !== null) {
|
|
256
|
+
const word = match[0];
|
|
257
|
+
const start = match.index;
|
|
258
|
+
|
|
259
|
+
// Add text before this word
|
|
260
|
+
if (start > lastEnd) {
|
|
261
|
+
nodes.push({
|
|
262
|
+
type: "text",
|
|
263
|
+
value: text.slice(lastEnd, start),
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Try to match with alignment data - look ahead if needed
|
|
268
|
+
const normalizedWord = normalizeWord(word);
|
|
269
|
+
let matchedIndex = -1;
|
|
270
|
+
let matchedAligned: AlignedWord | null = null;
|
|
271
|
+
|
|
272
|
+
// Search forward from current index to find matching word
|
|
273
|
+
// Look ahead up to 10 words to handle minor ordering differences
|
|
274
|
+
const maxLookahead = 10;
|
|
275
|
+
for (let i = wordIndex; i < Math.min(wordIndex + maxLookahead, words.length); i++) {
|
|
276
|
+
const candidate = words[i];
|
|
277
|
+
if (normalizeWord(candidate.word) === normalizedWord) {
|
|
278
|
+
matchedIndex = i;
|
|
279
|
+
matchedAligned = candidate;
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (matchedAligned && matchedIndex !== -1) {
|
|
285
|
+
// Create annotated span
|
|
286
|
+
nodes.push({
|
|
287
|
+
type: "element",
|
|
288
|
+
tagName: "span",
|
|
289
|
+
properties: {
|
|
290
|
+
className: [`${classPrefix}-word`],
|
|
291
|
+
"data-word-index": matchedIndex,
|
|
292
|
+
"data-start": matchedAligned.start.toFixed(3),
|
|
293
|
+
"data-end": matchedAligned.end.toFixed(3),
|
|
294
|
+
},
|
|
295
|
+
children: [{ type: "text", value: word }],
|
|
296
|
+
});
|
|
297
|
+
wordIndex = matchedIndex + 1;
|
|
298
|
+
} else {
|
|
299
|
+
// No match, just add as text
|
|
300
|
+
nodes.push({
|
|
301
|
+
type: "text",
|
|
302
|
+
value: word,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
lastEnd = start + word.length;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Add remaining text
|
|
310
|
+
if (lastEnd < text.length) {
|
|
311
|
+
nodes.push({
|
|
312
|
+
type: "text",
|
|
313
|
+
value: text.slice(lastEnd),
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Merge adjacent text nodes
|
|
318
|
+
const merged: (Text | Element)[] = [];
|
|
319
|
+
for (const node of nodes) {
|
|
320
|
+
const last = merged[merged.length - 1];
|
|
321
|
+
if (node.type === "text" && last?.type === "text") {
|
|
322
|
+
last.value += node.value;
|
|
323
|
+
} else {
|
|
324
|
+
merged.push(node);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
nodes: merged,
|
|
330
|
+
nextWordIndex: wordIndex,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Normalize a word for comparison (lowercase, remove punctuation).
|
|
336
|
+
*/
|
|
337
|
+
function normalizeWord(word: string): string {
|
|
338
|
+
return word
|
|
339
|
+
.toLowerCase()
|
|
340
|
+
.replace(/[^\p{L}\p{N}]/gu, "")
|
|
341
|
+
.trim();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export default rehypeAudioWords;
|
|
345
|
+
export { rehypeAudioWords };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default, rehypeAudioWords } from "./audio-words.js";
|
|
2
|
+
export type { RehypeAudioWordsOptions } from "./audio-words.js";
|
|
3
|
+
|
|
4
|
+
export { default as rehypeMathSpeech } from "./rehype-math-speech.js";
|
|
5
|
+
export { default as remarkMathSpeech } from "./remark-math-speech.js";
|
|
6
|
+
export type { MathSpeechEntry } from "./remark-math-speech.js";
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Element, Parent, Root } from "hast";
|
|
2
|
+
import { visitParents } from "unist-util-visit-parents";
|
|
3
|
+
import type { VFile } from "vfile";
|
|
4
|
+
import type { MathSpeechEntry } from "./remark-math-speech.js";
|
|
5
|
+
|
|
6
|
+
const SR_SPEECH_CLASS = "vocasync-math-speech";
|
|
7
|
+
|
|
8
|
+
let latexToSpeech: ((exprs: string[], options?: Record<string, unknown>) => Promise<string[]>) | null = null;
|
|
9
|
+
let latexToSpeechLoadAttempted = false;
|
|
10
|
+
|
|
11
|
+
const latexSpeechCache = new Map<string, Promise<string>>();
|
|
12
|
+
let warnedLatexSpeechFailure = false;
|
|
13
|
+
|
|
14
|
+
interface RehypeMathSpeechOptions {
|
|
15
|
+
/**
|
|
16
|
+
* SRE domain style for math-to-speech conversion
|
|
17
|
+
* @default "clearspeak"
|
|
18
|
+
*/
|
|
19
|
+
style?: "clearspeak" | "mathspeak";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Convert LaTeX to spoken text using speech-rule-engine and mathjax-full.
|
|
24
|
+
*/
|
|
25
|
+
async function speakLatex(
|
|
26
|
+
value: string,
|
|
27
|
+
style: string = "clearspeak"
|
|
28
|
+
): Promise<string> {
|
|
29
|
+
const trimmed = String(value ?? "").trim();
|
|
30
|
+
if (!trimmed) return "";
|
|
31
|
+
|
|
32
|
+
// Lazy load our internal latex-to-speech module
|
|
33
|
+
if (!latexToSpeechLoadAttempted) {
|
|
34
|
+
latexToSpeechLoadAttempted = true;
|
|
35
|
+
try {
|
|
36
|
+
const mod = await import("../lib/latex-to-speech/index.js");
|
|
37
|
+
latexToSpeech = mod.latexToSpeech || mod.default;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.warn(
|
|
40
|
+
"[vocasync] Math-to-speech initialization failed. Install optional dependencies:",
|
|
41
|
+
"\n npm install speech-rule-engine mathjax-full"
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!latexToSpeech) {
|
|
47
|
+
return trimmed; // Fallback to raw latex if library not available
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const cacheKey = `${style}:${trimmed}`;
|
|
51
|
+
if (!latexSpeechCache.has(cacheKey)) {
|
|
52
|
+
const pending = (async () => {
|
|
53
|
+
try {
|
|
54
|
+
const result = await latexToSpeech!([trimmed], {
|
|
55
|
+
domain: style,
|
|
56
|
+
style: "default",
|
|
57
|
+
locale: "en",
|
|
58
|
+
modality: "speech",
|
|
59
|
+
});
|
|
60
|
+
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
61
|
+
return normalizeWhitespace(spoken);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (!warnedLatexSpeechFailure) {
|
|
64
|
+
console.warn(
|
|
65
|
+
"[vocasync] Failed to convert LaTeX to speech:",
|
|
66
|
+
error instanceof Error ? error.message : error
|
|
67
|
+
);
|
|
68
|
+
warnedLatexSpeechFailure = true;
|
|
69
|
+
}
|
|
70
|
+
return normalizeWhitespace(trimmed);
|
|
71
|
+
}
|
|
72
|
+
})();
|
|
73
|
+
latexSpeechCache.set(cacheKey, pending);
|
|
74
|
+
}
|
|
75
|
+
return latexSpeechCache.get(cacheKey)!;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizeWhitespace(value = ""): string {
|
|
79
|
+
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Rehype plugin that injects hidden spoken text next to math elements.
|
|
84
|
+
* This allows rehypeAudioWords to wrap the spoken words with timing data.
|
|
85
|
+
*
|
|
86
|
+
* Must run AFTER rehype-katex or rehype-mathjax, and BEFORE rehypeAudioWords.
|
|
87
|
+
*/
|
|
88
|
+
export default function rehypeMathSpeech(options: RehypeMathSpeechOptions = {}) {
|
|
89
|
+
const { style = "clearspeak" } = options;
|
|
90
|
+
|
|
91
|
+
return async function transformer(tree: Root, file: VFile) {
|
|
92
|
+
const mathSpeech = file.data.mathSpeech as MathSpeechEntry[] | undefined;
|
|
93
|
+
if (!mathSpeech?.length) return;
|
|
94
|
+
|
|
95
|
+
// Find all MathJax/KaTeX container elements
|
|
96
|
+
const occurrences: Array<{ node: Element; parent: Parent }> = [];
|
|
97
|
+
visitParents(tree, "element", (node, ancestors) => {
|
|
98
|
+
// MathJax uses mjx-container, KaTeX uses .katex class
|
|
99
|
+
const isMathJax = node.tagName === "mjx-container";
|
|
100
|
+
const isKatex =
|
|
101
|
+
node.tagName === "span" &&
|
|
102
|
+
Array.isArray(node.properties?.className) &&
|
|
103
|
+
(node.properties.className as string[]).includes("katex");
|
|
104
|
+
|
|
105
|
+
if (!isMathJax && !isKatex) return;
|
|
106
|
+
|
|
107
|
+
const parent = ancestors[ancestors.length - 1] as Parent | undefined;
|
|
108
|
+
if (!parent) return;
|
|
109
|
+
occurrences.push({ node, parent });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if (!occurrences.length) return;
|
|
113
|
+
|
|
114
|
+
let mathIndex = 0;
|
|
115
|
+
for (const occurrence of occurrences) {
|
|
116
|
+
if (mathIndex >= mathSpeech.length) break;
|
|
117
|
+
const entry = mathSpeech[mathIndex];
|
|
118
|
+
mathIndex += 1;
|
|
119
|
+
const latex = entry.latex.trim();
|
|
120
|
+
if (!latex) continue;
|
|
121
|
+
|
|
122
|
+
const spoken = await speakLatex(latex, style);
|
|
123
|
+
if (!spoken) continue;
|
|
124
|
+
|
|
125
|
+
const parentChildren = occurrence.parent.children;
|
|
126
|
+
const index = parentChildren.indexOf(occurrence.node);
|
|
127
|
+
if (index === -1) continue;
|
|
128
|
+
|
|
129
|
+
// Create a hidden span with the spoken text
|
|
130
|
+
const speechNode: Element = {
|
|
131
|
+
type: "element",
|
|
132
|
+
tagName: "span",
|
|
133
|
+
properties: {
|
|
134
|
+
className: ["sr-only", SR_SPEECH_CLASS],
|
|
135
|
+
"data-math-speech": entry.display ? "display" : "inline",
|
|
136
|
+
"aria-hidden": "true",
|
|
137
|
+
},
|
|
138
|
+
children: [{ type: "text", value: spoken }],
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Insert the speech node right after the math element
|
|
142
|
+
parentChildren.splice(index + 1, 0, speechNode);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { visit } from "unist-util-visit";
|
|
2
|
+
import type { Root } from "mdast";
|
|
3
|
+
import type { VFile } from "vfile";
|
|
4
|
+
|
|
5
|
+
export type MathSpeechEntry = {
|
|
6
|
+
latex: string;
|
|
7
|
+
display: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Remark plugin that collects all math expressions from markdown.
|
|
12
|
+
* Must run BEFORE remark-math.
|
|
13
|
+
* Stores entries in file.data.mathSpeech for rehypeMathSpeech to consume.
|
|
14
|
+
*/
|
|
15
|
+
export default function remarkMathSpeechCollector() {
|
|
16
|
+
return function transformer(tree: Root, file: VFile) {
|
|
17
|
+
const entries: MathSpeechEntry[] = [];
|
|
18
|
+
visit(tree, (node: any) => {
|
|
19
|
+
if (node?.type !== "math" && node?.type !== "inlineMath") return;
|
|
20
|
+
const latex = String(node.value ?? "").trim();
|
|
21
|
+
if (!latex) return;
|
|
22
|
+
entries.push({
|
|
23
|
+
latex,
|
|
24
|
+
display: node.type === "math",
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
if (entries.length) {
|
|
28
|
+
file.data.mathSpeech = entries;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VocaSync AudioPlayer CSS Variables
|
|
3
|
+
*
|
|
4
|
+
* Import this file and override variables to customize the player appearance.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```css
|
|
8
|
+
* @import "@vocasync/astro/styles/variables.css";
|
|
9
|
+
*
|
|
10
|
+
* :root {
|
|
11
|
+
* --vocasync-primary: #8b5cf6;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
:root {
|
|
17
|
+
/* ========================================
|
|
18
|
+
* COLOR PALETTE
|
|
19
|
+
* ======================================== */
|
|
20
|
+
|
|
21
|
+
/* Primary brand color - used for buttons, progress bar, active states */
|
|
22
|
+
--vocasync-primary: #3b82f6;
|
|
23
|
+
--vocasync-primary-hover: #2563eb;
|
|
24
|
+
--vocasync-primary-content: #ffffff;
|
|
25
|
+
|
|
26
|
+
/* Surface colors - backgrounds and cards */
|
|
27
|
+
--vocasync-surface: #f8fafc;
|
|
28
|
+
--vocasync-surface-elevated: #ffffff;
|
|
29
|
+
|
|
30
|
+
/* Border colors */
|
|
31
|
+
--vocasync-border: #e2e8f0;
|
|
32
|
+
--vocasync-border-focus: #3b82f6;
|
|
33
|
+
|
|
34
|
+
/* Text colors */
|
|
35
|
+
--vocasync-text: #1e293b;
|
|
36
|
+
--vocasync-text-muted: #64748b;
|
|
37
|
+
--vocasync-text-faint: #94a3b8;
|
|
38
|
+
|
|
39
|
+
/* ========================================
|
|
40
|
+
* WORD HIGHLIGHTING
|
|
41
|
+
* ======================================== */
|
|
42
|
+
|
|
43
|
+
/* Color for the currently spoken word */
|
|
44
|
+
--vocasync-highlight: #10b981;
|
|
45
|
+
--vocasync-highlight-text: #ffffff;
|
|
46
|
+
|
|
47
|
+
/* Opacity for active word background */
|
|
48
|
+
--vocasync-highlight-active-opacity: 0.25;
|
|
49
|
+
|
|
50
|
+
/* Opacity for trailing words (creates a "wake" effect) */
|
|
51
|
+
--vocasync-highlight-trail-opacity: 0.12;
|
|
52
|
+
|
|
53
|
+
/* ========================================
|
|
54
|
+
* SIZING & SPACING
|
|
55
|
+
* ======================================== */
|
|
56
|
+
|
|
57
|
+
/* Player dimensions */
|
|
58
|
+
--vocasync-player-radius: 0.75rem;
|
|
59
|
+
--vocasync-player-padding: 1rem;
|
|
60
|
+
--vocasync-player-gap: 0.75rem;
|
|
61
|
+
|
|
62
|
+
/* Button sizes */
|
|
63
|
+
--vocasync-button-size: 2.5rem;
|
|
64
|
+
--vocasync-button-radius: 9999px;
|
|
65
|
+
|
|
66
|
+
/* Slider/range input */
|
|
67
|
+
--vocasync-slider-height: 0.375rem;
|
|
68
|
+
--vocasync-slider-thumb-size: 1rem;
|
|
69
|
+
--vocasync-slider-radius: 9999px;
|
|
70
|
+
|
|
71
|
+
/* Mini player */
|
|
72
|
+
--vocasync-mini-height: 3.5rem;
|
|
73
|
+
--vocasync-mini-offset: 1rem;
|
|
74
|
+
|
|
75
|
+
/* ========================================
|
|
76
|
+
* SHADOWS & EFFECTS
|
|
77
|
+
* ======================================== */
|
|
78
|
+
|
|
79
|
+
--vocasync-shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
|
|
80
|
+
--vocasync-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
|
|
81
|
+
--vocasync-shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
|
82
|
+
|
|
83
|
+
/* ========================================
|
|
84
|
+
* TRANSITIONS
|
|
85
|
+
* ======================================== */
|
|
86
|
+
|
|
87
|
+
--vocasync-transition-fast: 150ms ease;
|
|
88
|
+
--vocasync-transition: 200ms ease;
|
|
89
|
+
--vocasync-transition-slow: 300ms ease;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* ========================================
|
|
93
|
+
* DARK MODE
|
|
94
|
+
* ======================================== */
|
|
95
|
+
|
|
96
|
+
@media (prefers-color-scheme: dark) {
|
|
97
|
+
:root {
|
|
98
|
+
--vocasync-primary: #60a5fa;
|
|
99
|
+
--vocasync-primary-hover: #3b82f6;
|
|
100
|
+
--vocasync-primary-content: #1e293b;
|
|
101
|
+
|
|
102
|
+
--vocasync-surface: #1e293b;
|
|
103
|
+
--vocasync-surface-elevated: #334155;
|
|
104
|
+
|
|
105
|
+
--vocasync-border: #475569;
|
|
106
|
+
--vocasync-border-focus: #60a5fa;
|
|
107
|
+
|
|
108
|
+
--vocasync-text: #f1f5f9;
|
|
109
|
+
--vocasync-text-muted: #94a3b8;
|
|
110
|
+
--vocasync-text-faint: #64748b;
|
|
111
|
+
|
|
112
|
+
--vocasync-highlight: #34d399;
|
|
113
|
+
--vocasync-highlight-text: #1e293b;
|
|
114
|
+
|
|
115
|
+
--vocasync-shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.2);
|
|
116
|
+
--vocasync-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.3), 0 1px 2px -1px rgb(0 0 0 / 0.3);
|
|
117
|
+
--vocasync-shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.3), 0 4px 6px -4px rgb(0 0 0 / 0.3);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* ========================================
|
|
122
|
+
* WORD HIGHLIGHTING STYLES
|
|
123
|
+
* ======================================== */
|
|
124
|
+
|
|
125
|
+
.vocasync-word {
|
|
126
|
+
transition: background-color var(--vocasync-transition-fast),
|
|
127
|
+
color var(--vocasync-transition-fast);
|
|
128
|
+
border-radius: 0.125rem;
|
|
129
|
+
padding: 0 0.0625rem;
|
|
130
|
+
margin: 0 -0.0625rem;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.vocasync-word[data-active="true"] {
|
|
134
|
+
background-color: color-mix(
|
|
135
|
+
in oklch,
|
|
136
|
+
var(--vocasync-highlight) calc(var(--vocasync-highlight-active-opacity) * 100%),
|
|
137
|
+
transparent
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* Trail effect for previous words */
|
|
142
|
+
.vocasync-word[data-trail="1"] {
|
|
143
|
+
background-color: color-mix(
|
|
144
|
+
in oklch,
|
|
145
|
+
var(--vocasync-highlight) calc(var(--vocasync-highlight-trail-opacity) * 100%),
|
|
146
|
+
transparent
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.vocasync-word[data-trail="2"] {
|
|
151
|
+
background-color: color-mix(
|
|
152
|
+
in oklch,
|
|
153
|
+
var(--vocasync-highlight) calc(var(--vocasync-highlight-trail-opacity) * 50%),
|
|
154
|
+
transparent
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.vocasync-word[data-trail="3"],
|
|
159
|
+
.vocasync-word[data-trail="4"] {
|
|
160
|
+
background-color: color-mix(
|
|
161
|
+
in oklch,
|
|
162
|
+
var(--vocasync-highlight) calc(var(--vocasync-highlight-trail-opacity) * 25%),
|
|
163
|
+
transparent
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/* ========================================
|
|
168
|
+
* MATH SPEECH (hidden visually, accessible to highlighter)
|
|
169
|
+
* ======================================== */
|
|
170
|
+
|
|
171
|
+
.vocasync-math-speech {
|
|
172
|
+
position: absolute;
|
|
173
|
+
width: 1px;
|
|
174
|
+
height: 1px;
|
|
175
|
+
padding: 0;
|
|
176
|
+
margin: -1px;
|
|
177
|
+
overflow: hidden;
|
|
178
|
+
clip: rect(0, 0, 0, 0);
|
|
179
|
+
white-space: nowrap;
|
|
180
|
+
border: 0;
|
|
181
|
+
}
|