this repo has no description
at fix-ts-uint8array 62 lines 1.9 kB view raw
1import type { Lesson, TopicsConfig, Topic, Section } from '../types/lesson'; 2 3const modules = import.meta.glob('../../../content/**/*.json', { eager: true }); 4 5function getModule<T>(path: string): T | null { 6 const entry = modules[path]; 7 if (!entry) return null; 8 // Vite eager imports have a `default` property for JSON files 9 const mod = entry as { default?: T } & T; 10 return mod.default ?? (entry as T); 11} 12 13export function getSections(): Section[] { 14 const config = getModule<TopicsConfig>('../../../content/topics.json'); 15 return config?.sections ?? []; 16} 17 18export function getTopics(): Topic[] { 19 const config = getModule<TopicsConfig>('../../../content/topics.json'); 20 return config?.topics ?? []; 21} 22 23export function getLesson(topicId: string, lessonId: string): Lesson | null { 24 // Try common path patterns 25 const paths = [ 26 `../../../content/${topicId}/${lessonId}.json`, 27 `../../../content/lessons/${topicId}/${lessonId}.json`, 28 `../../../content/${topicId}/lessons/${lessonId}.json`, 29 ]; 30 31 for (const path of paths) { 32 const lesson = getModule<Lesson>(path); 33 if (lesson) return lesson; 34 } 35 36 // Fallback: search through all modules for matching lesson 37 for (const [path, mod] of Object.entries(modules)) { 38 if (path.endsWith('.json') && !path.endsWith('topics.json')) { 39 const data = (mod as { default?: Lesson }).default ?? (mod as Lesson); 40 if (data.id === lessonId && data.topicId === topicId) { 41 return data; 42 } 43 } 44 } 45 46 return null; 47} 48 49export function getLessonsForTopic(topicId: string): Lesson[] { 50 const lessons: Lesson[] = []; 51 52 for (const [path, mod] of Object.entries(modules)) { 53 if (path.endsWith('.json') && !path.endsWith('topics.json')) { 54 const data = (mod as { default?: Lesson }).default ?? (mod as Lesson); 55 if (data.topicId === topicId) { 56 lessons.push(data); 57 } 58 } 59 } 60 61 return lessons; 62}