@vocasync/astro 0.1.10 → 1.0.1
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 +141 -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 +141 -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
|
@@ -18,25 +18,23 @@ async function init() {
|
|
|
18
18
|
return initPromise;
|
|
19
19
|
initPromise = (async () => {
|
|
20
20
|
try {
|
|
21
|
-
// Dynamically import speech-rule-engine
|
|
22
|
-
|
|
21
|
+
// Dynamically import speech-rule-engine. Normalize CJS/ESM interop:
|
|
22
|
+
// bun hoists named exports onto the namespace, but Vite's SSR loader
|
|
23
|
+
// (used during `astro build`) puts them under `.default`.
|
|
24
|
+
// biome-ignore lint/suspicious/noExplicitAny: optional peer dep, no bundled types
|
|
25
|
+
const sreMod = await import("speech-rule-engine");
|
|
26
|
+
SRE = sreMod.default ?? sreMod;
|
|
23
27
|
}
|
|
24
28
|
catch {
|
|
25
29
|
console.warn("[vocasync] speech-rule-engine not installed. Math-to-speech disabled.");
|
|
26
30
|
return false;
|
|
27
31
|
}
|
|
28
32
|
try {
|
|
29
|
-
// Dynamically import mathjax-full components for tex2mml
|
|
30
|
-
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
33
|
+
// Dynamically import mathjax-full components for tex2mml.
|
|
31
34
|
const { TeX } = await import("mathjax-full/js/input/tex.js");
|
|
32
|
-
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
33
35
|
const { HTMLDocument } = await import("mathjax-full/js/handlers/html/HTMLDocument.js");
|
|
34
|
-
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
35
36
|
const { liteAdaptor } = await import("mathjax-full/js/adaptors/liteAdaptor.js");
|
|
36
|
-
const { SerializedMmlVisitor } = await import(
|
|
37
|
-
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
38
|
-
"mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
|
|
39
|
-
// @ts-expect-error - mathjax-full is an optional peer dependency
|
|
37
|
+
const { SerializedMmlVisitor } = await import("mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js");
|
|
40
38
|
const { AllPackages } = await import("mathjax-full/js/input/tex/AllPackages.js");
|
|
41
39
|
// Filter out problematic packages
|
|
42
40
|
const packages = AllPackages.filter((p) => p !== "bussproofs");
|
|
@@ -56,7 +54,7 @@ async function init() {
|
|
|
56
54
|
}
|
|
57
55
|
catch {
|
|
58
56
|
console.warn("[vocasync] mathjax-full not installed. Math-to-speech disabled.");
|
|
59
|
-
console.warn("[vocasync] Install with:
|
|
57
|
+
console.warn("[vocasync] Install with: bun add mathjax-full speech-rule-engine");
|
|
60
58
|
return false;
|
|
61
59
|
}
|
|
62
60
|
})();
|
|
@@ -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,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,
|
|
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,oEAAoE;YACpE,qEAAqE;YACrE,0DAA0D;YAC1D,kFAAkF;YAClF,MAAM,MAAM,GAAQ,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;YACvD,GAAG,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,IAAI,CAAC,uEAAuE,CAAC,CAAC;YACtF,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,CAAC;YACH,0DAA0D;YAC1D,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,MAAM,CAAC,8BAA8B,CAA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,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Convert a LaTeX expression to spoken text.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* `latexToSpeech` is imported statically (it lazy-loads the optional
|
|
5
|
+
* speech-rule-engine / mathjax-full deps internally), which is robust under
|
|
6
|
+
* Astro's Vite SSR loader where a lazy `import()` of this module returned a
|
|
7
|
+
* namespace without the expected named export.
|
|
8
|
+
*
|
|
9
|
+
* Falls back to the raw LaTeX if the math dependencies are unavailable or
|
|
10
|
+
* conversion fails, so a build never crashes on math content.
|
|
6
11
|
*/
|
|
7
12
|
export declare function speakLatex(latex: string, display: boolean, style?: string): Promise<string>;
|
|
8
13
|
//# sourceMappingURL=speak.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speak.d.ts","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"speak.d.ts","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"AASA;;;;;;;;;;GAUG;AACH,wBAAsB,UAAU,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,KAAK,SAAe,GACnB,OAAO,CAAC,MAAM,CAAC,CA8BjB"}
|
|
@@ -1,41 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
let loadAttempted = false;
|
|
3
|
-
let warnedFailure = false;
|
|
1
|
+
import { latexToSpeech } from "./index.js";
|
|
4
2
|
const cache = new Map();
|
|
3
|
+
let warnedFailure = false;
|
|
5
4
|
function normalizeWhitespace(value = "") {
|
|
6
5
|
return value.replace(/\r/g, "").replace(/\s+/g, " ").trim();
|
|
7
6
|
}
|
|
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
7
|
/**
|
|
27
8
|
* Convert a LaTeX expression to spoken text.
|
|
28
9
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
10
|
+
* `latexToSpeech` is imported statically (it lazy-loads the optional
|
|
11
|
+
* speech-rule-engine / mathjax-full deps internally), which is robust under
|
|
12
|
+
* Astro's Vite SSR loader where a lazy `import()` of this module returned a
|
|
13
|
+
* namespace without the expected named export.
|
|
14
|
+
*
|
|
15
|
+
* Falls back to the raw LaTeX if the math dependencies are unavailable or
|
|
16
|
+
* conversion fails, so a build never crashes on math content.
|
|
31
17
|
*/
|
|
32
18
|
export async function speakLatex(latex, display, style = "clearspeak") {
|
|
33
19
|
const trimmed = String(latex ?? "").trim();
|
|
34
20
|
if (!trimmed)
|
|
35
21
|
return "";
|
|
36
|
-
if (!(await ensureLoaded())) {
|
|
37
|
-
return trimmed; // Fallback to raw LaTeX
|
|
38
|
-
}
|
|
39
22
|
const cacheKey = `${style}:${display}:${trimmed}`;
|
|
40
23
|
if (!cache.has(cacheKey)) {
|
|
41
24
|
const pending = (async () => {
|
|
@@ -47,7 +30,7 @@ export async function speakLatex(latex, display, style = "clearspeak") {
|
|
|
47
30
|
modality: "speech",
|
|
48
31
|
});
|
|
49
32
|
const spoken = Array.isArray(result) ? result[0] : String(result ?? "");
|
|
50
|
-
return normalizeWhitespace(spoken);
|
|
33
|
+
return normalizeWhitespace(spoken) || trimmed;
|
|
51
34
|
}
|
|
52
35
|
catch (error) {
|
|
53
36
|
if (!warnedFailure) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speak.js","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"speak.js","sourceRoot":"","sources":["../../../src/lib/latex-to-speech/speak.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;AACjD,IAAI,aAAa,GAAG,KAAK,CAAC;AAE1B,SAAS,mBAAmB,CAAC,KAAK,GAAG,EAAE;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,KAAa,EACb,OAAgB,EAChB,KAAK,GAAG,YAAY;IAEpB,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,IAAI,CAAC,OAAO;QAAE,OAAO,EAAE,CAAC;IAExB,MAAM,QAAQ,GAAG,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE,CAAC;IAClD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;YAC1B,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;oBAChE,MAAM,EAAE,KAAK;oBACb,KAAK,EAAE,SAAS;oBAChB,MAAM,EAAE,IAAI;oBACZ,QAAQ,EAAE,QAAQ;iBACnB,CAAC,CAAC;gBACH,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC;gBACxE,OAAO,mBAAmB,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC;YAChD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,aAAa,EAAE,CAAC;oBACnB,OAAO,CAAC,IAAI,CACV,+CAA+C,EAC/C,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAC/C,CAAC;oBACF,aAAa,GAAG,IAAI,CAAC;gBACvB,CAAC;gBACD,OAAO,mBAAmB,CAAC,OAAO,CAAC,CAAC;YACtC,CAAC;QACH,CAAC,CAAC,EAAE,CAAC;QACL,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAE,CAAC;AAC9B,CAAC"}
|
|
@@ -5,44 +5,34 @@ import type { Plugin } from "unified";
|
|
|
5
5
|
*/
|
|
6
6
|
export interface RehypeAudioWordsOptions {
|
|
7
7
|
/**
|
|
8
|
-
* Path to the audio map JSON file.
|
|
9
|
-
* Can be absolute or relative to the project root.
|
|
8
|
+
* Path to the audio map JSON file (absolute or relative to project root).
|
|
10
9
|
* @default ".vocasync/audio-map.json"
|
|
11
10
|
*/
|
|
12
11
|
audioMapPath?: string;
|
|
13
12
|
/**
|
|
14
|
-
* CSS class
|
|
15
|
-
* @default "vocasync"
|
|
13
|
+
* CSS class for generated word spans.
|
|
14
|
+
* @default "vocasync" (spans get the class `${classPrefix}-word`)
|
|
16
15
|
*/
|
|
17
16
|
classPrefix?: string;
|
|
18
17
|
/**
|
|
19
|
-
*
|
|
18
|
+
* Content collection folder name used to extract slugs.
|
|
20
19
|
* @default "articles"
|
|
21
20
|
*/
|
|
22
21
|
collectionName?: string;
|
|
23
22
|
}
|
|
24
23
|
/**
|
|
25
|
-
* Rehype plugin that wraps
|
|
24
|
+
* Rehype plugin that wraps each visible word in
|
|
25
|
+
* `<span class="vocasync-word" data-i="N" data-n="K">…</span>`:
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
27
|
+
* data-i = the word's starting index in the alignment word stream
|
|
28
|
+
* (the normalisedSpeechtext POSTed to VocaSync).
|
|
29
|
+
* data-n = how many alignment tokens this visible word expands to
|
|
30
|
+
* (via spokenTokenCount): "$50" -> 2, "1995" -> 6, words -> 1.
|
|
31
31
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* import { rehypeAudioWords } from "@vocasync/astro/rehype";
|
|
37
|
-
*
|
|
38
|
-
* export default defineConfig({
|
|
39
|
-
* markdown: {
|
|
40
|
-
* rehypePlugins: [
|
|
41
|
-
* [rehypeAudioWords, { collectionName: "articles" }]
|
|
42
|
-
* ]
|
|
43
|
-
* }
|
|
44
|
-
* });
|
|
45
|
-
* ```
|
|
32
|
+
* No alignment data is fetched here — tokenization is deterministic and runs
|
|
33
|
+
* the same `spokenTokenCount` used to build the transcript, so indices line
|
|
34
|
+
* up token-for-token. Math containers carrying `data-speech` (attached by
|
|
35
|
+
* rehypeMathSpeech) are wrapped as a single unit with their token count.
|
|
46
36
|
*/
|
|
47
37
|
declare const rehypeAudioWords: Plugin<[RehypeAudioWordsOptions?], Root>;
|
|
48
38
|
export default rehypeAudioWords;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio-words.d.ts","sourceRoot":"","sources":["../../src/rehype/audio-words.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"audio-words.d.ts","sourceRoot":"","sources":["../../src/rehype/audio-words.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,IAAI,EAAQ,MAAM,MAAM,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAuBD;;;;;;;;;;;;;GAaG;AACH,QAAA,MAAM,gBAAgB,EAAE,MAAM,CAAC,CAAC,uBAAuB,CAAC,CAAC,EAAE,IAAI,CAmF9D,CAAC;AAqDF,eAAe,gBAAgB,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|
|
@@ -1,280 +1,159 @@
|
|
|
1
|
-
import { execFileSync } from "node:child_process";
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
1
|
import { visitParents } from "unist-util-visit-parents";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Fetch alignment data from URL (synchronously using a worker thread approach).
|
|
29
|
-
* Since rehype plugins must be synchronous, we use a sync HTTP request.
|
|
30
|
-
* @param alignmentUrl - The base alignment URL
|
|
31
|
-
* @param slug - The content slug for caching
|
|
32
|
-
* @param publishableKey - Optional publishable key for authentication
|
|
33
|
-
*/
|
|
34
|
-
function fetchAlignmentSync(alignmentUrl, slug, publishableKey) {
|
|
35
|
-
// Check cache first
|
|
36
|
-
if (alignmentCache.has(slug)) {
|
|
37
|
-
return alignmentCache.get(slug) ?? null;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
// Build the URL with publishable key if provided
|
|
41
|
-
let fetchUrl = alignmentUrl;
|
|
42
|
-
if (publishableKey) {
|
|
43
|
-
const url = new URL(alignmentUrl);
|
|
44
|
-
url.searchParams.set("pk", publishableKey);
|
|
45
|
-
fetchUrl = url.toString();
|
|
46
|
-
}
|
|
47
|
-
// Use execFileSync with curl to make a synchronous HTTP request
|
|
48
|
-
// execFileSync avoids shell invocation, preventing command injection
|
|
49
|
-
// Note: -L flag follows redirects (required for vocasync.io signed URLs)
|
|
50
|
-
const result = execFileSync("curl", ["-sL", fetchUrl], {
|
|
51
|
-
encoding: "utf-8",
|
|
52
|
-
timeout: 30000,
|
|
53
|
-
});
|
|
54
|
-
const data = JSON.parse(result);
|
|
55
|
-
// The alignment response contains a words array
|
|
56
|
-
const words = data.words;
|
|
57
|
-
if (words && Array.isArray(words)) {
|
|
58
|
-
alignmentCache.set(slug, words);
|
|
59
|
-
return words;
|
|
60
|
-
}
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
// Failed to fetch - return null
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
2
|
+
import { spokenTokenCount } from "../core/spoken-form.js";
|
|
3
|
+
import { loadAudioMapSync, resolveSlug } from "./shared.js";
|
|
4
|
+
// Matches a visible "word": letters/digits/currency/percent, with intra-word
|
|
5
|
+
// apostrophes. Currency (\p{Sc}) and % are kept attached to their number so
|
|
6
|
+
// "$50" and "50%" stay single tokens whose spokenTokenCount matches the
|
|
7
|
+
// transcript ("fifty dollar", "fifty percent"). Comma-grouped numbers
|
|
8
|
+
// (e.g. "1,000,000") are a known limitation — author numbers without commas.
|
|
9
|
+
const WORD_PATTERN = /[\p{L}\p{N}\p{Sc}%]+(?:['’][\p{L}\p{N}\p{Sc}%]+)*/gu;
|
|
10
|
+
const SKIP_TAGS = new Set([
|
|
11
|
+
"pre",
|
|
12
|
+
"code",
|
|
13
|
+
"script",
|
|
14
|
+
"style",
|
|
15
|
+
"textarea",
|
|
16
|
+
"svg",
|
|
17
|
+
"math",
|
|
18
|
+
"mjx-container",
|
|
19
|
+
"img",
|
|
20
|
+
"figcaption",
|
|
21
|
+
]);
|
|
22
|
+
const SKIP_CLASSES = new Set(["katex", "katex-mathml", "katex-html"]);
|
|
68
23
|
/**
|
|
69
|
-
* Rehype plugin that wraps
|
|
70
|
-
*
|
|
71
|
-
* For each text node in the document, this plugin:
|
|
72
|
-
* 1. Tokenizes the text into words
|
|
73
|
-
* 2. Matches words against alignment data
|
|
74
|
-
* 3. Wraps matched words in <span> elements with data-start and data-end attributes
|
|
24
|
+
* Rehype plugin that wraps each visible word in
|
|
25
|
+
* `<span class="vocasync-word" data-i="N" data-n="K">…</span>`:
|
|
75
26
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* import { rehypeAudioWords } from "@vocasync/astro/rehype";
|
|
27
|
+
* data-i = the word's starting index in the alignment word stream
|
|
28
|
+
* (the normalisedSpeechtext POSTed to VocaSync).
|
|
29
|
+
* data-n = how many alignment tokens this visible word expands to
|
|
30
|
+
* (via spokenTokenCount): "$50" -> 2, "1995" -> 6, words -> 1.
|
|
81
31
|
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* ]
|
|
87
|
-
* }
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
32
|
+
* No alignment data is fetched here — tokenization is deterministic and runs
|
|
33
|
+
* the same `spokenTokenCount` used to build the transcript, so indices line
|
|
34
|
+
* up token-for-token. Math containers carrying `data-speech` (attached by
|
|
35
|
+
* rehypeMathSpeech) are wrapped as a single unit with their token count.
|
|
90
36
|
*/
|
|
91
37
|
const rehypeAudioWords = (options = {}) => {
|
|
92
38
|
const { audioMapPath = ".vocasync/audio-map.json", classPrefix = "vocasync", collectionName = "articles", } = options;
|
|
39
|
+
const wordClass = `${classPrefix}-word`;
|
|
93
40
|
return (tree, file) => {
|
|
94
|
-
// Load audio map (cached after first load)
|
|
95
41
|
const audioMap = loadAudioMapSync(audioMapPath);
|
|
96
|
-
if (!audioMap)
|
|
97
|
-
return;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return; // No file path, skip processing
|
|
105
|
-
}
|
|
106
|
-
// Normalize path separators
|
|
107
|
-
const normalizedPath = filePath.replace(/\\/g, "/");
|
|
108
|
-
// Try to extract slug from collection path
|
|
109
|
-
// Pattern: /src/content/{collectionName}/{slug}.md or .mdx
|
|
110
|
-
const collectionPattern = new RegExp(`/content/${collectionName}/([^/]+)\\.(md|mdx)$`);
|
|
111
|
-
const match = normalizedPath.match(collectionPattern);
|
|
112
|
-
let slug;
|
|
113
|
-
if (match) {
|
|
114
|
-
slug = match[1];
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
// Fallback: just get the filename without extension
|
|
118
|
-
const filenameMatch = normalizedPath.match(/\/([^/]+)\.(md|mdx)$/);
|
|
119
|
-
if (filenameMatch) {
|
|
120
|
-
slug = filenameMatch[1];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (!slug) {
|
|
124
|
-
return; // No slug, skip processing
|
|
125
|
-
}
|
|
126
|
-
// Get alignment URL for this slug from audio map
|
|
127
|
-
const entry = audioMap.entries[slug];
|
|
128
|
-
if (!entry?.alignmentUrl) {
|
|
129
|
-
return; // No alignment URL, skip
|
|
130
|
-
}
|
|
131
|
-
// Fetch alignment words from URL with publishable key
|
|
132
|
-
const words = fetchAlignmentSync(entry.alignmentUrl, slug, entry.publishableKey);
|
|
133
|
-
if (!words || words.length === 0) {
|
|
134
|
-
return; // No alignment data, skip
|
|
135
|
-
}
|
|
136
|
-
// Track word index as we process text nodes
|
|
137
|
-
let wordIndex = 0;
|
|
138
|
-
// Process all text nodes in the document
|
|
139
|
-
visitParents(tree, "text", (node, ancestors) => {
|
|
140
|
-
const textNode = node;
|
|
42
|
+
if (!audioMap)
|
|
43
|
+
return;
|
|
44
|
+
const slug = resolveSlug(file, collectionName);
|
|
45
|
+
if (!slug || !audioMap.entries[slug])
|
|
46
|
+
return; // only wrap posts that have audio
|
|
47
|
+
let nextIndex = 0;
|
|
48
|
+
const replacements = [];
|
|
49
|
+
visitParents(tree, (node) => node.type === "text" || (node.type === "element" && hasDataSpeech(node)), (node, ancestors) => {
|
|
141
50
|
const parent = ancestors[ancestors.length - 1];
|
|
142
|
-
|
|
143
|
-
|
|
51
|
+
if (!parent)
|
|
52
|
+
return;
|
|
53
|
+
// Math container with attached spoken form -> wrap as one unit.
|
|
54
|
+
if (node.type === "element") {
|
|
55
|
+
const speech = node.properties?.dataSpeech;
|
|
56
|
+
if (typeof speech !== "string" || !speech)
|
|
57
|
+
return;
|
|
58
|
+
const count = spokenTokenCount(speech);
|
|
59
|
+
if (count === 0)
|
|
60
|
+
return;
|
|
61
|
+
const index = parent.children.indexOf(node);
|
|
62
|
+
if (index === -1)
|
|
63
|
+
return;
|
|
64
|
+
replacements.push({
|
|
65
|
+
parent,
|
|
66
|
+
index,
|
|
67
|
+
nodes: [wrapSpan([node], nextIndex, count, wordClass)],
|
|
68
|
+
});
|
|
69
|
+
nextIndex += count;
|
|
144
70
|
return;
|
|
145
71
|
}
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
if (
|
|
157
|
-
|
|
72
|
+
// Text node -> wrap each word, skipping non-textual ancestors.
|
|
73
|
+
if (isSkipped(ancestors))
|
|
74
|
+
return;
|
|
75
|
+
const text = node.value;
|
|
76
|
+
if (!text)
|
|
77
|
+
return;
|
|
78
|
+
const matches = Array.from(text.matchAll(WORD_PATTERN));
|
|
79
|
+
if (matches.length === 0)
|
|
80
|
+
return;
|
|
81
|
+
const index = parent.children.indexOf(node);
|
|
82
|
+
if (index === -1)
|
|
83
|
+
return;
|
|
84
|
+
const out = [];
|
|
85
|
+
let cursor = 0;
|
|
86
|
+
for (const m of matches) {
|
|
87
|
+
const start = m.index ?? 0;
|
|
88
|
+
const end = start + m[0].length;
|
|
89
|
+
if (start > cursor)
|
|
90
|
+
out.push({ type: "text", value: text.slice(cursor, start) });
|
|
91
|
+
const count = spokenTokenCount(m[0]);
|
|
92
|
+
if (count > 0) {
|
|
93
|
+
out.push(wrapText(m[0], nextIndex, count, wordClass));
|
|
94
|
+
nextIndex += count;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
out.push({ type: "text", value: m[0] });
|
|
98
|
+
}
|
|
99
|
+
cursor = end;
|
|
158
100
|
}
|
|
101
|
+
if (cursor < text.length)
|
|
102
|
+
out.push({ type: "text", value: text.slice(cursor) });
|
|
103
|
+
replacements.push({ parent, index, nodes: out });
|
|
159
104
|
});
|
|
105
|
+
// Apply in reverse so earlier indices stay valid.
|
|
106
|
+
for (let i = replacements.length - 1; i >= 0; i--) {
|
|
107
|
+
const r = replacements[i];
|
|
108
|
+
r.parent.children.splice(r.index, 1, ...r.nodes);
|
|
109
|
+
}
|
|
160
110
|
};
|
|
161
111
|
};
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
112
|
+
function wrapText(word, dataI, dataN, wordClass) {
|
|
113
|
+
return {
|
|
114
|
+
type: "element",
|
|
115
|
+
tagName: "span",
|
|
116
|
+
properties: {
|
|
117
|
+
className: [wordClass],
|
|
118
|
+
"data-i": String(dataI),
|
|
119
|
+
"data-n": String(dataN),
|
|
120
|
+
},
|
|
121
|
+
children: [{ type: "text", value: word }],
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function wrapSpan(children, dataI, dataN, wordClass) {
|
|
125
|
+
return {
|
|
126
|
+
type: "element",
|
|
127
|
+
tagName: "span",
|
|
128
|
+
properties: {
|
|
129
|
+
className: [wordClass],
|
|
130
|
+
"data-i": String(dataI),
|
|
131
|
+
"data-n": String(dataN),
|
|
132
|
+
},
|
|
133
|
+
children,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function hasDataSpeech(node) {
|
|
137
|
+
const props = node.properties ?? {};
|
|
138
|
+
return typeof props.dataSpeech === "string" && props.dataSpeech.length > 0;
|
|
139
|
+
}
|
|
140
|
+
function isSkipped(ancestors) {
|
|
168
141
|
for (const ancestor of ancestors) {
|
|
169
|
-
if ("tagName" in ancestor
|
|
142
|
+
if (!("tagName" in ancestor))
|
|
143
|
+
continue;
|
|
144
|
+
if (SKIP_TAGS.has(ancestor.tagName))
|
|
170
145
|
return true;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const classes = Array.isArray(ancestor.properties.className)
|
|
175
|
-
? ancestor.properties.className
|
|
176
|
-
: [ancestor.properties.className];
|
|
146
|
+
const className = ancestor.properties?.className;
|
|
147
|
+
if (className) {
|
|
148
|
+
const classes = Array.isArray(className) ? className : [className];
|
|
177
149
|
for (const cls of classes) {
|
|
178
|
-
if (typeof cls === "string" &&
|
|
150
|
+
if (typeof cls === "string" && SKIP_CLASSES.has(cls))
|
|
179
151
|
return true;
|
|
180
|
-
}
|
|
181
152
|
}
|
|
182
153
|
}
|
|
183
154
|
}
|
|
184
155
|
return false;
|
|
185
156
|
}
|
|
186
|
-
/**
|
|
187
|
-
* Process a text node and create annotated nodes.
|
|
188
|
-
*/
|
|
189
|
-
function processTextNode(text, words, startIndex, classPrefix) {
|
|
190
|
-
const nodes = [];
|
|
191
|
-
let wordIndex = startIndex;
|
|
192
|
-
let lastEnd = 0;
|
|
193
|
-
// Regex to match words (similar to word boundaries)
|
|
194
|
-
const wordPattern = /[\p{L}\p{N}'']+/gu;
|
|
195
|
-
let match;
|
|
196
|
-
// biome-ignore lint/suspicious/noAssignInExpressions: regex exec loop requires assignment in condition
|
|
197
|
-
while ((match = wordPattern.exec(text)) !== null) {
|
|
198
|
-
const word = match[0];
|
|
199
|
-
const start = match.index;
|
|
200
|
-
// Add text before this word
|
|
201
|
-
if (start > lastEnd) {
|
|
202
|
-
nodes.push({
|
|
203
|
-
type: "text",
|
|
204
|
-
value: text.slice(lastEnd, start),
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
// Try to match with alignment data - look ahead if needed
|
|
208
|
-
const normalizedWord = normalizeWord(word);
|
|
209
|
-
let matchedIndex = -1;
|
|
210
|
-
let matchedAligned = null;
|
|
211
|
-
// Search forward from current index to find matching word
|
|
212
|
-
// Look ahead up to 10 words to handle minor ordering differences
|
|
213
|
-
const maxLookahead = 10;
|
|
214
|
-
for (let i = wordIndex; i < Math.min(wordIndex + maxLookahead, words.length); i++) {
|
|
215
|
-
const candidate = words[i];
|
|
216
|
-
if (normalizeWord(candidate.word) === normalizedWord) {
|
|
217
|
-
matchedIndex = i;
|
|
218
|
-
matchedAligned = candidate;
|
|
219
|
-
break;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (matchedAligned && matchedIndex !== -1) {
|
|
223
|
-
// Create annotated span
|
|
224
|
-
nodes.push({
|
|
225
|
-
type: "element",
|
|
226
|
-
tagName: "span",
|
|
227
|
-
properties: {
|
|
228
|
-
className: [`${classPrefix}-word`],
|
|
229
|
-
"data-word-index": matchedIndex,
|
|
230
|
-
"data-start": matchedAligned.start.toFixed(3),
|
|
231
|
-
"data-end": matchedAligned.end.toFixed(3),
|
|
232
|
-
},
|
|
233
|
-
children: [{ type: "text", value: word }],
|
|
234
|
-
});
|
|
235
|
-
wordIndex = matchedIndex + 1;
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
// No match, just add as text
|
|
239
|
-
nodes.push({
|
|
240
|
-
type: "text",
|
|
241
|
-
value: word,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
lastEnd = start + word.length;
|
|
245
|
-
}
|
|
246
|
-
// Add remaining text
|
|
247
|
-
if (lastEnd < text.length) {
|
|
248
|
-
nodes.push({
|
|
249
|
-
type: "text",
|
|
250
|
-
value: text.slice(lastEnd),
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
// Merge adjacent text nodes
|
|
254
|
-
const merged = [];
|
|
255
|
-
for (const node of nodes) {
|
|
256
|
-
const last = merged[merged.length - 1];
|
|
257
|
-
if (node.type === "text" && last?.type === "text") {
|
|
258
|
-
last.value += node.value;
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
merged.push(node);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return {
|
|
265
|
-
nodes: merged,
|
|
266
|
-
nextWordIndex: wordIndex,
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Normalize a word for comparison (lowercase, remove punctuation).
|
|
271
|
-
*/
|
|
272
|
-
function normalizeWord(word) {
|
|
273
|
-
return word
|
|
274
|
-
.toLowerCase()
|
|
275
|
-
.replace(/[^\p{L}\p{N}]/gu, "")
|
|
276
|
-
.trim();
|
|
277
|
-
}
|
|
278
157
|
export default rehypeAudioWords;
|
|
279
158
|
export { rehypeAudioWords };
|
|
280
159
|
//# sourceMappingURL=audio-words.js.map
|