@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,24 @@
|
|
|
1
|
+
// Components are re-exported from the main components directory
|
|
2
|
+
// Import using: import { AudioPlayer } from "@vocasync/astro/components"
|
|
3
|
+
|
|
4
|
+
// Note: AudioPlayer.astro is copied to dist during build
|
|
5
|
+
// This file provides TypeScript types for the component
|
|
6
|
+
|
|
7
|
+
export interface AudioPlayerProps {
|
|
8
|
+
/** Post slug to lookup audio in audio-map */
|
|
9
|
+
slug: string;
|
|
10
|
+
/** Accessible label for the player */
|
|
11
|
+
label?: string;
|
|
12
|
+
/** Show placeholder when no audio available */
|
|
13
|
+
showPlaceholder?: boolean;
|
|
14
|
+
/** Additional CSS classes */
|
|
15
|
+
class?: string;
|
|
16
|
+
/** Selector for article body (for word highlighting) */
|
|
17
|
+
articleSelector?: string;
|
|
18
|
+
/** Enable floating mini player when main player scrolls out of view */
|
|
19
|
+
enableMiniPlayer?: boolean;
|
|
20
|
+
/** Enable word highlighting during playback */
|
|
21
|
+
enableHighlighting?: boolean;
|
|
22
|
+
/** Number of trailing highlighted words */
|
|
23
|
+
trailLength?: number;
|
|
24
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export {
|
|
2
|
+
VocaSyncConfigSchema,
|
|
3
|
+
CollectionConfigSchema,
|
|
4
|
+
SynthesisConfigSchema,
|
|
5
|
+
MathConfigSchema,
|
|
6
|
+
OutputConfigSchema,
|
|
7
|
+
ProcessingConfigSchema,
|
|
8
|
+
VoiceSchema,
|
|
9
|
+
QualitySchema,
|
|
10
|
+
FormatSchema,
|
|
11
|
+
LanguageSchema,
|
|
12
|
+
MathStyleSchema,
|
|
13
|
+
validateConfig,
|
|
14
|
+
defaultConfig,
|
|
15
|
+
} from "./schema.js";
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
VocaSyncConfig,
|
|
19
|
+
VocaSyncUserConfig,
|
|
20
|
+
CollectionConfig,
|
|
21
|
+
SynthesisConfig,
|
|
22
|
+
MathConfig,
|
|
23
|
+
OutputConfig,
|
|
24
|
+
ProcessingConfig,
|
|
25
|
+
Voice,
|
|
26
|
+
Quality,
|
|
27
|
+
Format,
|
|
28
|
+
Language,
|
|
29
|
+
MathStyle,
|
|
30
|
+
} from "./schema.js";
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Supported TTS voices
|
|
5
|
+
*/
|
|
6
|
+
export const VoiceSchema = z.enum(["alloy", "echo", "fable", "onyx", "nova", "shimmer"]);
|
|
7
|
+
export type Voice = z.infer<typeof VoiceSchema>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Supported audio qualities
|
|
11
|
+
*/
|
|
12
|
+
export const QualitySchema = z.enum(["sd", "hd"]);
|
|
13
|
+
export type Quality = z.infer<typeof QualitySchema>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Supported audio formats
|
|
17
|
+
*/
|
|
18
|
+
export const FormatSchema = z.enum(["mp3", "opus", "aac", "flac"]);
|
|
19
|
+
export type Format = z.infer<typeof FormatSchema>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Supported languages for synthesis
|
|
23
|
+
* Uses short ISO 639-1 codes as expected by the VocaSync API
|
|
24
|
+
*/
|
|
25
|
+
export const LanguageSchema = z.enum([
|
|
26
|
+
"af", "ar", "hy", "az", "be", "bs", "bg", "ca", "zh", "hr",
|
|
27
|
+
"cs", "da", "nl", "en", "et", "fi", "fr", "gl", "de", "el",
|
|
28
|
+
"he", "hi", "hu", "is", "id", "it", "ja", "kn", "kk", "ko",
|
|
29
|
+
"lv", "lt", "mk", "ms", "mr", "mi", "ne", "no", "fa", "pl",
|
|
30
|
+
"pt", "ro", "ru", "sr", "sk", "sl", "es", "sw", "sv", "tl",
|
|
31
|
+
"ta", "th", "tr", "uk", "ur", "vi", "cy"
|
|
32
|
+
]);
|
|
33
|
+
export type Language = z.infer<typeof LanguageSchema>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Math speech styles
|
|
37
|
+
*/
|
|
38
|
+
export const MathStyleSchema = z.enum(["clearspeak", "mathspeak"]);
|
|
39
|
+
export type MathStyle = z.infer<typeof MathStyleSchema>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Collection configuration
|
|
43
|
+
*/
|
|
44
|
+
export const CollectionConfigSchema = z.object({
|
|
45
|
+
/** Name of the Astro content collection */
|
|
46
|
+
name: z.string().min(1),
|
|
47
|
+
/** Path to the content directory */
|
|
48
|
+
path: z.string().min(1),
|
|
49
|
+
/** Field used for unique slug identification */
|
|
50
|
+
slugField: z.string().default("slug"),
|
|
51
|
+
});
|
|
52
|
+
export type CollectionConfig = z.infer<typeof CollectionConfigSchema>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Synthesis options
|
|
56
|
+
*/
|
|
57
|
+
export const SynthesisConfigSchema = z.object({
|
|
58
|
+
/** TTS voice to use */
|
|
59
|
+
voice: VoiceSchema.default("onyx"),
|
|
60
|
+
/** Audio quality */
|
|
61
|
+
quality: QualitySchema.default("sd"),
|
|
62
|
+
/** Output format */
|
|
63
|
+
format: FormatSchema.default("mp3"),
|
|
64
|
+
});
|
|
65
|
+
export type SynthesisConfig = z.infer<typeof SynthesisConfigSchema>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Math/LaTeX configuration
|
|
69
|
+
*/
|
|
70
|
+
export const MathConfigSchema = z.object({
|
|
71
|
+
/** Enable LaTeX to speech conversion */
|
|
72
|
+
enabled: z.boolean().default(false),
|
|
73
|
+
/** Speech style for math expressions */
|
|
74
|
+
style: MathStyleSchema.default("clearspeak"),
|
|
75
|
+
});
|
|
76
|
+
export type MathConfig = z.infer<typeof MathConfigSchema>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Output configuration
|
|
80
|
+
*/
|
|
81
|
+
export const OutputConfigSchema = z.object({
|
|
82
|
+
/** Path to store the audio-map.json artifact */
|
|
83
|
+
audioMapPath: z.string().default("./src/data/audio-map.json"),
|
|
84
|
+
});
|
|
85
|
+
export type OutputConfig = z.infer<typeof OutputConfigSchema>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Processing options
|
|
89
|
+
*/
|
|
90
|
+
export const ProcessingConfigSchema = z.object({
|
|
91
|
+
/** Number of concurrent synthesis jobs */
|
|
92
|
+
concurrency: z.number().int().min(1).max(10).default(3),
|
|
93
|
+
/** Force reprocessing even if content hasn't changed */
|
|
94
|
+
force: z.boolean().default(false),
|
|
95
|
+
});
|
|
96
|
+
export type ProcessingConfig = z.infer<typeof ProcessingConfigSchema>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Main VocaSync configuration schema
|
|
100
|
+
*/
|
|
101
|
+
export const VocaSyncConfigSchema = z.object({
|
|
102
|
+
/** Content collection settings */
|
|
103
|
+
collection: CollectionConfigSchema,
|
|
104
|
+
/** Language for synthesis (e.g., "en", "fr", "de") */
|
|
105
|
+
language: LanguageSchema.default("en"),
|
|
106
|
+
/** Synthesis options */
|
|
107
|
+
synthesis: SynthesisConfigSchema.default({}),
|
|
108
|
+
/** Math/LaTeX support */
|
|
109
|
+
math: MathConfigSchema.default({}),
|
|
110
|
+
/** Output paths */
|
|
111
|
+
output: OutputConfigSchema.default({}),
|
|
112
|
+
/** Frontmatter field to check for audio opt-in/out (true/false) */
|
|
113
|
+
frontmatterField: z.string().optional(),
|
|
114
|
+
/** Processing options */
|
|
115
|
+
processing: ProcessingConfigSchema.default({}),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
export type VocaSyncConfig = z.infer<typeof VocaSyncConfigSchema>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* User-facing config type (with optional defaults)
|
|
122
|
+
*/
|
|
123
|
+
export type VocaSyncUserConfig = {
|
|
124
|
+
collection: {
|
|
125
|
+
name: string;
|
|
126
|
+
path: string;
|
|
127
|
+
slugField?: string;
|
|
128
|
+
};
|
|
129
|
+
language?: Language;
|
|
130
|
+
synthesis?: {
|
|
131
|
+
voice?: Voice;
|
|
132
|
+
quality?: Quality;
|
|
133
|
+
format?: Format;
|
|
134
|
+
};
|
|
135
|
+
math?: {
|
|
136
|
+
enabled?: boolean;
|
|
137
|
+
style?: MathStyle;
|
|
138
|
+
};
|
|
139
|
+
output?: {
|
|
140
|
+
audioMapPath?: string;
|
|
141
|
+
};
|
|
142
|
+
frontmatterField?: string;
|
|
143
|
+
processing?: {
|
|
144
|
+
concurrency?: number;
|
|
145
|
+
force?: boolean;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Validate and normalize user configuration
|
|
151
|
+
*/
|
|
152
|
+
export function validateConfig(userConfig: VocaSyncUserConfig): VocaSyncConfig {
|
|
153
|
+
return VocaSyncConfigSchema.parse(userConfig);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Default configuration values
|
|
158
|
+
*/
|
|
159
|
+
export const defaultConfig: Omit<VocaSyncConfig, "collection"> = {
|
|
160
|
+
language: "en",
|
|
161
|
+
synthesis: {
|
|
162
|
+
voice: "onyx",
|
|
163
|
+
quality: "sd",
|
|
164
|
+
format: "mp3",
|
|
165
|
+
},
|
|
166
|
+
math: {
|
|
167
|
+
enabled: false,
|
|
168
|
+
style: "clearspeak",
|
|
169
|
+
},
|
|
170
|
+
output: {
|
|
171
|
+
audioMapPath: "./src/data/audio-map.json",
|
|
172
|
+
},
|
|
173
|
+
processing: {
|
|
174
|
+
concurrency: 3,
|
|
175
|
+
force: false,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import type { AudioMap, AudioArtifact } from "../types/index.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create an empty audio map structure
|
|
7
|
+
*/
|
|
8
|
+
export function createEmptyAudioMap(): AudioMap {
|
|
9
|
+
return {
|
|
10
|
+
version: 1,
|
|
11
|
+
updatedAt: new Date().toISOString(),
|
|
12
|
+
entries: {},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Load the audio map from disk.
|
|
18
|
+
* Returns an empty map if the file doesn't exist.
|
|
19
|
+
*
|
|
20
|
+
* @param filePath - Path to audio-map.json
|
|
21
|
+
*/
|
|
22
|
+
export async function loadAudioMap(filePath: string): Promise<AudioMap> {
|
|
23
|
+
try {
|
|
24
|
+
const content = await readFile(filePath, "utf-8");
|
|
25
|
+
const data = JSON.parse(content) as AudioMap;
|
|
26
|
+
|
|
27
|
+
// Validate basic structure
|
|
28
|
+
if (typeof data !== "object" || !data.entries) {
|
|
29
|
+
console.warn(`Invalid audio-map.json structure at ${filePath}, creating new`);
|
|
30
|
+
return createEmptyAudioMap();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return data;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
36
|
+
return createEmptyAudioMap();
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Save the audio map to disk.
|
|
44
|
+
* Creates parent directories if they don't exist.
|
|
45
|
+
*
|
|
46
|
+
* @param filePath - Path to audio-map.json
|
|
47
|
+
* @param audioMap - The audio map to save
|
|
48
|
+
*/
|
|
49
|
+
export async function saveAudioMap(filePath: string, audioMap: AudioMap): Promise<void> {
|
|
50
|
+
// Update timestamp
|
|
51
|
+
audioMap.updatedAt = new Date().toISOString();
|
|
52
|
+
|
|
53
|
+
// Ensure directory exists
|
|
54
|
+
const dir = dirname(filePath);
|
|
55
|
+
await mkdir(dir, { recursive: true });
|
|
56
|
+
|
|
57
|
+
// Write with pretty formatting
|
|
58
|
+
const content = JSON.stringify(audioMap, null, 2);
|
|
59
|
+
await writeFile(filePath, content, "utf-8");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get an audio entry by slug.
|
|
64
|
+
*
|
|
65
|
+
* @param audioMap - The audio map
|
|
66
|
+
* @param slug - The content slug
|
|
67
|
+
* @returns The audio artifact or undefined
|
|
68
|
+
*/
|
|
69
|
+
export function getAudioEntry(audioMap: AudioMap, slug: string): AudioArtifact | undefined {
|
|
70
|
+
return audioMap.entries[slug];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Set an audio entry for a slug.
|
|
75
|
+
*
|
|
76
|
+
* @param audioMap - The audio map to modify
|
|
77
|
+
* @param slug - The content slug
|
|
78
|
+
* @param artifact - The audio artifact
|
|
79
|
+
*/
|
|
80
|
+
export function setAudioEntry(audioMap: AudioMap, slug: string, artifact: AudioArtifact): void {
|
|
81
|
+
audioMap.entries[slug] = artifact;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Remove an audio entry by slug.
|
|
86
|
+
*
|
|
87
|
+
* @param audioMap - The audio map to modify
|
|
88
|
+
* @param slug - The content slug
|
|
89
|
+
* @returns True if entry was removed
|
|
90
|
+
*/
|
|
91
|
+
export function removeAudioEntry(audioMap: AudioMap, slug: string): boolean {
|
|
92
|
+
if (slug in audioMap.entries) {
|
|
93
|
+
delete audioMap.entries[slug];
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get all slugs that have audio entries.
|
|
101
|
+
*
|
|
102
|
+
* @param audioMap - The audio map
|
|
103
|
+
* @returns Array of slugs
|
|
104
|
+
*/
|
|
105
|
+
export function getAudioSlugs(audioMap: AudioMap): string[] {
|
|
106
|
+
return Object.keys(audioMap.entries);
|
|
107
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { join, extname, basename, relative } from "node:path";
|
|
3
|
+
import type { ContentItem } from "../types/index.js";
|
|
4
|
+
import type { CollectionConfig } from "../config/index.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse frontmatter from markdown content.
|
|
8
|
+
* Expects YAML frontmatter between --- delimiters.
|
|
9
|
+
*/
|
|
10
|
+
function parseFrontmatter(content: string): {
|
|
11
|
+
frontmatter: Record<string, unknown>;
|
|
12
|
+
body: string;
|
|
13
|
+
} {
|
|
14
|
+
const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n/;
|
|
15
|
+
const match = content.match(frontmatterRegex);
|
|
16
|
+
|
|
17
|
+
if (!match) {
|
|
18
|
+
return { frontmatter: {}, body: content };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const frontmatterStr = match[1];
|
|
22
|
+
const body = content.slice(match[0].length);
|
|
23
|
+
|
|
24
|
+
// Simple YAML parser for common cases
|
|
25
|
+
const frontmatter: Record<string, unknown> = {};
|
|
26
|
+
|
|
27
|
+
for (const line of frontmatterStr.split("\n")) {
|
|
28
|
+
const colonIndex = line.indexOf(":");
|
|
29
|
+
if (colonIndex === -1) continue;
|
|
30
|
+
|
|
31
|
+
const key = line.slice(0, colonIndex).trim();
|
|
32
|
+
let value: unknown = line.slice(colonIndex + 1).trim();
|
|
33
|
+
|
|
34
|
+
// Parse common value types
|
|
35
|
+
if (value === "true") value = true;
|
|
36
|
+
else if (value === "false") value = false;
|
|
37
|
+
else if (value === "null" || value === "") value = null;
|
|
38
|
+
else if (/^-?\d+$/.test(value as string)) value = Number.parseInt(value as string, 10);
|
|
39
|
+
else if (/^-?\d+\.\d+$/.test(value as string)) value = Number.parseFloat(value as string);
|
|
40
|
+
else if ((value as string).startsWith('"') && (value as string).endsWith('"')) {
|
|
41
|
+
value = (value as string).slice(1, -1);
|
|
42
|
+
} else if ((value as string).startsWith("'") && (value as string).endsWith("'")) {
|
|
43
|
+
value = (value as string).slice(1, -1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
frontmatter[key] = value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { frontmatter, body };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Extract slug from file path or frontmatter.
|
|
54
|
+
*/
|
|
55
|
+
function extractSlug(
|
|
56
|
+
filePath: string,
|
|
57
|
+
frontmatter: Record<string, unknown>,
|
|
58
|
+
slugField: string
|
|
59
|
+
): string {
|
|
60
|
+
// First check frontmatter
|
|
61
|
+
if (slugField in frontmatter && typeof frontmatter[slugField] === "string") {
|
|
62
|
+
return frontmatter[slugField] as string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Fall back to filename without extension
|
|
66
|
+
const filename = basename(filePath);
|
|
67
|
+
const ext = extname(filename);
|
|
68
|
+
return filename.slice(0, -ext.length);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if a file is a markdown/MDX file.
|
|
73
|
+
*/
|
|
74
|
+
function isContentFile(filename: string): boolean {
|
|
75
|
+
const ext = extname(filename).toLowerCase();
|
|
76
|
+
return [".md", ".mdx", ".markdown"].includes(ext);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Recursively collect all content files from a directory.
|
|
81
|
+
*/
|
|
82
|
+
async function collectFiles(dir: string): Promise<string[]> {
|
|
83
|
+
const files: string[] = [];
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
87
|
+
|
|
88
|
+
for (const entry of entries) {
|
|
89
|
+
const fullPath = join(dir, entry.name);
|
|
90
|
+
|
|
91
|
+
if (entry.isDirectory()) {
|
|
92
|
+
// Skip hidden directories
|
|
93
|
+
if (entry.name.startsWith(".")) continue;
|
|
94
|
+
const subFiles = await collectFiles(fullPath);
|
|
95
|
+
files.push(...subFiles);
|
|
96
|
+
} else if (entry.isFile() && isContentFile(entry.name)) {
|
|
97
|
+
files.push(fullPath);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return files;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Load all content items from a collection.
|
|
111
|
+
*
|
|
112
|
+
* @param config - Collection configuration
|
|
113
|
+
* @param frontmatterField - Optional field to filter by (items with false are skipped)
|
|
114
|
+
*/
|
|
115
|
+
export async function loadContent(
|
|
116
|
+
config: CollectionConfig,
|
|
117
|
+
frontmatterField?: string
|
|
118
|
+
): Promise<ContentItem[]> {
|
|
119
|
+
const contentDir = config.path;
|
|
120
|
+
|
|
121
|
+
// Check if directory exists
|
|
122
|
+
try {
|
|
123
|
+
await stat(contentDir);
|
|
124
|
+
} catch {
|
|
125
|
+
throw new Error(`Content directory not found: ${contentDir}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Collect all markdown files
|
|
129
|
+
const files = await collectFiles(contentDir);
|
|
130
|
+
const items: ContentItem[] = [];
|
|
131
|
+
|
|
132
|
+
for (const filePath of files) {
|
|
133
|
+
const rawContent = await readFile(filePath, "utf-8");
|
|
134
|
+
const { frontmatter, body } = parseFrontmatter(rawContent);
|
|
135
|
+
|
|
136
|
+
// Skip if frontmatter field is set and value is false
|
|
137
|
+
if (frontmatterField && frontmatter[frontmatterField] === false) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Skip drafts by default
|
|
142
|
+
if (frontmatter.draft === true) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const slug = extractSlug(filePath, frontmatter, config.slugField);
|
|
147
|
+
|
|
148
|
+
items.push({
|
|
149
|
+
slug,
|
|
150
|
+
content: body,
|
|
151
|
+
frontmatter,
|
|
152
|
+
filePath: relative(process.cwd(), filePath),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return items;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Load a single content item by slug.
|
|
161
|
+
*
|
|
162
|
+
* @param config - Collection configuration
|
|
163
|
+
* @param targetSlug - The slug to find
|
|
164
|
+
*/
|
|
165
|
+
export async function loadContentBySlug(
|
|
166
|
+
config: CollectionConfig,
|
|
167
|
+
targetSlug: string
|
|
168
|
+
): Promise<ContentItem | undefined> {
|
|
169
|
+
const items = await loadContent(config);
|
|
170
|
+
return items.find((item) => item.slug === targetSlug);
|
|
171
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compute a SHA-256 hash of content for change detection.
|
|
5
|
+
*
|
|
6
|
+
* @param content - The content string to hash
|
|
7
|
+
* @returns A hexadecimal hash string
|
|
8
|
+
*/
|
|
9
|
+
export function computeHash(content: string): string {
|
|
10
|
+
return createHash("sha256").update(content, "utf8").digest("hex");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Compare two hashes to check if content has changed.
|
|
15
|
+
*
|
|
16
|
+
* @param oldHash - The previous hash
|
|
17
|
+
* @param newHash - The new hash
|
|
18
|
+
* @returns True if the hashes are different
|
|
19
|
+
*/
|
|
20
|
+
export function hasChanged(oldHash: string | undefined, newHash: string): boolean {
|
|
21
|
+
return oldHash !== newHash;
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { loadContent, loadContentBySlug } from "./content-loader.js";
|
|
2
|
+
export { buildSpeechDocument } from "./speech-builder.js";
|
|
3
|
+
export { computeHash, hasChanged } from "./hash-manager.js";
|
|
4
|
+
export {
|
|
5
|
+
loadAudioMap,
|
|
6
|
+
saveAudioMap,
|
|
7
|
+
getAudioEntry,
|
|
8
|
+
setAudioEntry,
|
|
9
|
+
removeAudioEntry,
|
|
10
|
+
getAudioSlugs,
|
|
11
|
+
createEmptyAudioMap,
|
|
12
|
+
} from "./audio-map.js";
|