@vocasync/astro 1.0.0 → 2.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.
Files changed (139) hide show
  1. package/README.md +618 -80
  2. package/dist/api/client.d.ts.map +1 -1
  3. package/dist/api/client.js.map +1 -1
  4. package/dist/api/index.d.ts +1 -1
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/api/index.js +1 -1
  7. package/dist/api/index.js.map +1 -1
  8. package/dist/cli.js.map +1 -1
  9. package/dist/components/AudioPlayer.astro +298 -1136
  10. package/dist/components/index.d.ts +10 -32
  11. package/dist/components/index.d.ts.map +1 -1
  12. package/dist/components/index.js +1 -3
  13. package/dist/components/index.js.map +1 -1
  14. package/dist/components/types.d.ts +117 -0
  15. package/dist/components/types.d.ts.map +1 -0
  16. package/dist/components/types.js +2 -0
  17. package/dist/components/types.js.map +1 -0
  18. package/dist/config/index.d.ts +2 -2
  19. package/dist/config/index.d.ts.map +1 -1
  20. package/dist/config/index.js +1 -1
  21. package/dist/config/index.js.map +1 -1
  22. package/dist/config/schema.d.ts +124 -169
  23. package/dist/config/schema.d.ts.map +1 -1
  24. package/dist/config/schema.js +4 -4
  25. package/dist/config/schema.js.map +1 -1
  26. package/dist/core/content-loader.js +1 -1
  27. package/dist/core/content-loader.js.map +1 -1
  28. package/dist/core/index.d.ts +2 -2
  29. package/dist/core/index.d.ts.map +1 -1
  30. package/dist/core/index.js +2 -2
  31. package/dist/core/index.js.map +1 -1
  32. package/dist/core/speech-builder.js.map +1 -1
  33. package/dist/index.d.ts +6 -7
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +5 -6
  36. package/dist/index.js.map +1 -1
  37. package/dist/integration.d.ts +28 -2
  38. package/dist/integration.d.ts.map +1 -1
  39. package/dist/integration.js +40 -3
  40. package/dist/integration.js.map +1 -1
  41. package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
  42. package/dist/lib/latex-to-speech/index.js.map +1 -1
  43. package/dist/player-core/boot.d.ts +9 -0
  44. package/dist/player-core/boot.d.ts.map +1 -0
  45. package/dist/player-core/boot.js +59 -0
  46. package/dist/player-core/boot.js.map +1 -0
  47. package/dist/player-core/highlighter.d.ts +31 -0
  48. package/dist/player-core/highlighter.d.ts.map +1 -0
  49. package/dist/player-core/highlighter.js +103 -0
  50. package/dist/player-core/highlighter.js.map +1 -0
  51. package/dist/player-core/index.d.ts +17 -0
  52. package/dist/player-core/index.d.ts.map +1 -0
  53. package/dist/player-core/index.js +24 -0
  54. package/dist/player-core/index.js.map +1 -0
  55. package/dist/player-core/media-url.d.ts +18 -0
  56. package/dist/player-core/media-url.d.ts.map +1 -0
  57. package/dist/player-core/media-url.js +33 -0
  58. package/dist/player-core/media-url.js.map +1 -0
  59. package/dist/player-core/player.d.ts +9 -0
  60. package/dist/player-core/player.d.ts.map +1 -0
  61. package/dist/player-core/player.js +622 -0
  62. package/dist/player-core/player.js.map +1 -0
  63. package/dist/player-core/registry.d.ts +20 -0
  64. package/dist/player-core/registry.d.ts.map +1 -0
  65. package/dist/player-core/registry.js +31 -0
  66. package/dist/player-core/registry.js.map +1 -0
  67. package/dist/player-core/seek-delegate.d.ts +24 -0
  68. package/dist/player-core/seek-delegate.d.ts.map +1 -0
  69. package/dist/player-core/seek-delegate.js +64 -0
  70. package/dist/player-core/seek-delegate.js.map +1 -0
  71. package/dist/player-core/storage.d.ts +38 -0
  72. package/dist/player-core/storage.d.ts.map +1 -0
  73. package/dist/player-core/storage.js +107 -0
  74. package/dist/player-core/storage.js.map +1 -0
  75. package/dist/player-core/strings.d.ts +53 -0
  76. package/dist/player-core/strings.d.ts.map +1 -0
  77. package/dist/player-core/strings.js +45 -0
  78. package/dist/player-core/strings.js.map +1 -0
  79. package/dist/player-core/timings.d.ts +56 -0
  80. package/dist/player-core/timings.d.ts.map +1 -0
  81. package/dist/player-core/timings.js +90 -0
  82. package/dist/player-core/timings.js.map +1 -0
  83. package/dist/rehype/audio-words.d.ts +9 -3
  84. package/dist/rehype/audio-words.d.ts.map +1 -1
  85. package/dist/rehype/audio-words.js +8 -8
  86. package/dist/rehype/audio-words.js.map +1 -1
  87. package/dist/rehype/index.d.ts +2 -2
  88. package/dist/rehype/index.d.ts.map +1 -1
  89. package/dist/rehype/index.js.map +1 -1
  90. package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
  91. package/dist/rehype/rehype-math-speech.js +2 -0
  92. package/dist/rehype/rehype-math-speech.js.map +1 -1
  93. package/dist/styles/content.css +41 -0
  94. package/dist/styles/layers.css +13 -0
  95. package/dist/styles/player.css +517 -0
  96. package/dist/styles/preset-shadcn.css +31 -0
  97. package/dist/styles/tokens.css +266 -0
  98. package/dist/styles/vocasync.css +10 -0
  99. package/dist/sync/index.d.ts +1 -1
  100. package/dist/sync/index.d.ts.map +1 -1
  101. package/dist/sync/index.js +1 -1
  102. package/dist/sync/index.js.map +1 -1
  103. package/dist/sync/orchestrator.d.ts.map +1 -1
  104. package/dist/sync/orchestrator.js.map +1 -1
  105. package/package.json +40 -17
  106. package/src/api/index.ts +4 -4
  107. package/src/components/AudioPlayer.astro +298 -1136
  108. package/src/components/index.ts +19 -34
  109. package/src/components/types.ts +140 -0
  110. package/src/config/index.ts +23 -24
  111. package/src/config/schema.ts +4 -4
  112. package/src/core/content-loader.ts +1 -1
  113. package/src/core/index.ts +7 -7
  114. package/src/env.d.ts +2 -0
  115. package/src/index.ts +23 -28
  116. package/src/integration.ts +66 -4
  117. package/src/player-core/boot.ts +62 -0
  118. package/src/player-core/highlighter.ts +159 -0
  119. package/src/player-core/index.ts +45 -0
  120. package/src/player-core/media-url.ts +35 -0
  121. package/src/player-core/player.ts +725 -0
  122. package/src/player-core/registry.ts +40 -0
  123. package/src/player-core/seek-delegate.ts +92 -0
  124. package/src/player-core/storage.ts +112 -0
  125. package/src/player-core/strings.ts +89 -0
  126. package/src/player-core/timings.ts +115 -0
  127. package/src/rehype/audio-words.ts +17 -11
  128. package/src/rehype/index.ts +2 -3
  129. package/src/rehype/rehype-math-speech.ts +3 -1
  130. package/src/styles/content.css +41 -0
  131. package/src/styles/layers.css +13 -0
  132. package/src/styles/player.css +517 -0
  133. package/src/styles/preset-shadcn.css +31 -0
  134. package/src/styles/tokens.css +266 -0
  135. package/src/styles/vocasync.css +10 -0
  136. package/src/sync/index.ts +1 -1
  137. package/src/sync/orchestrator.ts +1 -1
  138. package/dist/styles/variables.css +0 -164
  139. package/src/styles/variables.css +0 -164
@@ -0,0 +1,159 @@
1
+ import {
2
+ computeSpanTimings,
3
+ EPS,
4
+ idxForTime,
5
+ type SpanTiming,
6
+ trailRange,
7
+ type WordTiming,
8
+ } from "./timings.js";
9
+
10
+ /**
11
+ * Binds alignment timings to the `.vocasync-word[data-i][data-n]` spans the rehype
12
+ * plugin emitted, and keeps the active word and its trail in sync with a time.
13
+ *
14
+ * Owns no audio element and no animation loop -- the caller drives it by calling
15
+ * `update(time)`. Class names are options rather than literals so the same engine can
16
+ * serve markup that does not use the default prefix.
17
+ */
18
+
19
+ const PARAGRAPH_SELECTOR = "p, li, blockquote, h1, h2, h3, h4, h5, h6, td, th";
20
+
21
+ export interface HighlighterOptions {
22
+ /** Element containing the word spans. */
23
+ articleRoot: Element;
24
+ /** Alignment timings, indexed by `data-i`. */
25
+ words: readonly WordTiming[];
26
+ /** How many spans behind the active one stay marked. */
27
+ trailLength?: number;
28
+ wordSelector?: string;
29
+ activeClass?: string;
30
+ trailClass?: string;
31
+ /** Scroll the active word's paragraph into view when it changes. */
32
+ autoScroll?: boolean;
33
+ /** Injected so the engine stays testable and window-free. */
34
+ prefersReducedMotion?: () => boolean;
35
+ /** Called when the active word changes, including to none during silence. */
36
+ onWordChange?: (el: HTMLElement | null) => void;
37
+ }
38
+
39
+ export interface Highlighter {
40
+ /** Number of resolved spans; 0 means there is nothing to highlight. */
41
+ readonly size: number;
42
+ update(time: number): void;
43
+ /** Remove every mark this highlighter applied. */
44
+ clear(): void;
45
+ /** Start time of the alignment token at `dataI`, or null. */
46
+ startOfToken(dataI: number): number | null;
47
+ setAutoScroll(enabled: boolean): void;
48
+ destroy(): void;
49
+ }
50
+
51
+ export function createHighlighter(options: HighlighterOptions): Highlighter {
52
+ const {
53
+ articleRoot,
54
+ words,
55
+ trailLength = 4,
56
+ wordSelector = ".vocasync-word[data-i]",
57
+ activeClass = "is-active",
58
+ trailClass = "is-trail",
59
+ prefersReducedMotion = () => false,
60
+ } = options;
61
+
62
+ let autoScroll = options.autoScroll ?? false;
63
+
64
+ const spans = Array.from(articleRoot.querySelectorAll(wordSelector)) as HTMLElement[];
65
+ const spanTimings: SpanTiming<HTMLElement>[] = computeSpanTimings(
66
+ spans.map((el) => ({
67
+ dataI: Number.parseInt(el.dataset.i ?? "", 10),
68
+ dataN: Number.parseInt(el.dataset.n ?? "", 10),
69
+ ref: el,
70
+ })),
71
+ words
72
+ );
73
+
74
+ let activeEl: HTMLElement | null = null;
75
+ let trailStart = 0;
76
+ let trailEnd = 0;
77
+ let lastScrolledParagraph: Element | null = null;
78
+
79
+ function setActive(el: HTMLElement | null) {
80
+ if (activeEl === el) return;
81
+ if (activeEl) activeEl.classList.remove(activeClass);
82
+ if (el) el.classList.add(activeClass);
83
+ activeEl = el;
84
+ options.onWordChange?.(el);
85
+ }
86
+
87
+ function clearTrail() {
88
+ for (let i = trailStart; i < trailEnd; i++) {
89
+ spanTimings[i]?.ref.classList.remove(trailClass);
90
+ }
91
+ trailStart = 0;
92
+ trailEnd = 0;
93
+ }
94
+
95
+ function scrollActiveIntoView(el: HTMLElement) {
96
+ const paragraph = el.closest(PARAGRAPH_SELECTOR);
97
+ if (!paragraph || paragraph === lastScrolledParagraph) return;
98
+ paragraph.scrollIntoView({
99
+ behavior: prefersReducedMotion() ? "auto" : "smooth",
100
+ block: "center",
101
+ });
102
+ lastScrolledParagraph = paragraph;
103
+ }
104
+
105
+ return {
106
+ get size() {
107
+ return spanTimings.length;
108
+ },
109
+
110
+ update(time) {
111
+ if (!spanTimings.length) return;
112
+ if (!Number.isFinite(time)) return;
113
+
114
+ // One index drives both the active word and the trail, exactly as the
115
+ // original did: computing them separately lets them disagree by one at a
116
+ // span boundary, which shows up as a word that is both active and trailing.
117
+ const lo = idxForTime(spanTimings, time + EPS);
118
+ const candidate = spanTimings[lo];
119
+ const active =
120
+ candidate && time >= candidate.start - EPS && time < candidate.end + EPS
121
+ ? candidate.ref
122
+ : null;
123
+
124
+ setActive(active);
125
+ if (autoScroll && activeEl) scrollActiveIntoView(activeEl);
126
+
127
+ // Differential trail: only touch spans that entered or left the range.
128
+ const next = trailRange(lo, trailLength);
129
+ for (let i = trailStart; i < trailEnd; i++) {
130
+ if (i < next.start || i >= next.end) spanTimings[i]?.ref.classList.remove(trailClass);
131
+ }
132
+ for (let i = next.start; i < next.end; i++) {
133
+ if (i < trailStart || i >= trailEnd) spanTimings[i]?.ref.classList.add(trailClass);
134
+ }
135
+ trailStart = next.start;
136
+ trailEnd = next.end;
137
+ },
138
+
139
+ clear() {
140
+ setActive(null);
141
+ clearTrail();
142
+ lastScrolledParagraph = null;
143
+ },
144
+
145
+ startOfToken(dataI) {
146
+ const timing = words[dataI];
147
+ return timing && typeof timing.start === "number" ? timing.start : null;
148
+ },
149
+
150
+ setAutoScroll(enabled) {
151
+ autoScroll = enabled;
152
+ if (!enabled) lastScrolledParagraph = null;
153
+ },
154
+
155
+ destroy() {
156
+ this.clear();
157
+ },
158
+ };
159
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The player engine: playback, word highlighting and the view-transition lifecycle,
3
+ * with no markup opinions and no Astro dependency.
4
+ *
5
+ * Public and covered by semver from 2.0.0. `install()` is what the shipped component
6
+ * calls; everything below it is for building something else.
7
+ *
8
+ * Storage and the click delegate are deliberately not exported — they are wiring
9
+ * rather than contract, and freezing them would make the engine harder to improve.
10
+ */
11
+
12
+ // Discovery and lifecycle. `install()` boots every player on the page and keeps them
13
+ // working across Astro view transitions.
14
+ export { bootAll, install, teardownAll } from "./boot.js";
15
+ // Word highlighting against rehype-generated spans.
16
+ export {
17
+ createHighlighter,
18
+ type Highlighter,
19
+ type HighlighterOptions,
20
+ } from "./highlighter.js";
21
+ // The stream URL, with its publishable key applied.
22
+ export { resolveAudioSrc } from "./media-url.js";
23
+ // One player, driven from your own markup.
24
+ export { createPlayer, formatTime, type PlayerInstance } from "./player.js";
25
+
26
+ // Labels, for a custom UI that should read the same as the shipped one.
27
+ export {
28
+ defaultStrings,
29
+ describeTime,
30
+ type PlayerStrings,
31
+ resolveStrings,
32
+ type SerializableStrings,
33
+ } from "./strings.js";
34
+
35
+ // The pure timing maths, for building something quite different.
36
+ export {
37
+ computeSpanTimings,
38
+ EPS,
39
+ idxForTime,
40
+ type SpanItem,
41
+ type SpanTiming,
42
+ spanAtTime,
43
+ trailRange,
44
+ type WordTiming,
45
+ } from "./timings.js";
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Resolve the audio stream URL, appending the publishable key when one is present.
3
+ *
4
+ * This is deliberately NOT shared with `api/client.ts`'s `withPublishableKey`, which
5
+ * looks almost identical. That one runs under Node during `vocasync sync`, always
6
+ * against absolute API URLs, and has no `document` to resolve a base from. This one
7
+ * runs in the browser, where an audio map may legitimately carry a site-relative URL.
8
+ * Two runtimes, two correct behaviours -- unifying them would break one of them.
9
+ */
10
+
11
+ /**
12
+ * Returns the URL to assign to `audio.src`, or null when there is nothing playable.
13
+ *
14
+ * Never throws. The previous implementation called `new URL(url)` with no base, which
15
+ * throws on a relative URL; the throw was swallowed by the caller's outer try/catch
16
+ * and surfaced as "Audio not available for this article" with no error anywhere.
17
+ */
18
+ export function resolveAudioSrc(
19
+ audioUrl: unknown,
20
+ publishableKey?: unknown,
21
+ base?: string
22
+ ): string | null {
23
+ if (typeof audioUrl !== "string" || audioUrl.trim() === "") return null;
24
+ if (typeof publishableKey !== "string" || publishableKey === "") return audioUrl;
25
+
26
+ try {
27
+ const url = new URL(audioUrl, base);
28
+ url.searchParams.set("pk", publishableKey);
29
+ return url.toString();
30
+ } catch {
31
+ // An unparseable URL is still worth attempting: the browser may resolve it even
32
+ // where URL() will not, and a 404 is a better failure than no audio at all.
33
+ return audioUrl;
34
+ }
35
+ }