The code below demonstrates how the catalog initializes real-time text-to-speech sentence tracking alongside kid-friendly reading filters:
class ReadMeEngine {
constructor(catalogConfig) {
this.synth = window.speechSynthesis;
this.catalog = catalogConfig.books || [];
this.kidSafeFilter = true;
}
searchCatalog(query, maxAgeRating = 12) {
return this.catalog.filter(book => {
const matchesQuery = book.title.toLowerCase().includes(query.toLowerCase()) ||
book.author.toLowerCase().includes(query.toLowerCase());
const safeRating = book.ageRating <= maxAgeRating;
return matchesQuery && safeRating;
});
}
speakPassage(textElement, rate = 1.0) {
if (this.synth.speaking) this.synth.cancel();
const utterance = new SpeechSynthesisUtterance(textElement.textContent);
utterance.rate = rate;
utterance.onboundary = (evt) => this.highlightWord(textElement, evt.charIndex);
this.synth.speak(utterance);
}
}