Repository Public
1import "server-only";2import { getCapsuleData, getCourse, getDeck, getLab, studioIsSource, termsOf } from "./content";3import { env } from "./env";4import { accessLabel } from "./labels";5import { diagramPath, glossaryPath, sessionPath, watchPath } from "@/modules/content-schema";6import { DIAGRAMS } from "@/modules/diagram-library/registry";7import type { SearchDoc } from "@/modules/search";8import type { CourseView } from "@/modules/content-schema";9import { contentRev } from "@/server/content";1011const PAGES: [string, string, string][] = [12 ["/programme", "Programme", "Les 24 séances du cours, en six parties, avec niveaux et préalables."],13 ["/parcours", "Parcours suggérés", "Quatre ordres de lecture selon le profil : découverte, travail du savoir, développement, compréhension approfondie."],14 ["/laboratoires", "Laboratoires", "Travaux pratiques guidés du cours."],15 ["/ressources", "Ressources", "Documents consultables et fichiers à télécharger."],16 ["/glossaire", "Glossaire", "Définitions des termes du cours, en français et en anglais."],17 ["/a-propos", "À propos", "Le cours, l’enseignant, la méthode, l’affiliation et la licence."],18 ["/accessibilite", "Accessibilité", "Engagements, raccourcis clavier, limites connues."],19 ["/confidentialite", "Confidentialité", "Aucun compte, aucun témoin de suivi ; données locales, exportation et effacement."],20 ["/conditions", "Conditions d’utilisation", "Accès gratuit, droits réservés, quiz formatifs, absence d’attestation."],21];2223const stripMarkdown = (md: string) => md.replace(/```[\s\S]*?```/g, " ").replace(/[#>*_`|-]+/g, " ").replace(/\s+/g, " ").trim();2425/**26 * The public search corpus. Built from the filtered view only: a session that is not open contributes27 * its public plan (title, outcomes, themes) and nothing else; quiz answers and solutions are never indexed.28 */29export async function buildSearchDocs(view?: CourseView): Promise<SearchDoc[]> {30 const course = view ?? await getCourse();31 const docs: SearchDoc[] = [];32 const partLabel = (id: string) => { const p = course.parts.find((x) => x.id === id)!; return `Partie ${p.id} — ${p.short}`; };33 for (const s of course.sessions) {34 if (s.access === "archived") continue;35 const base = { part: s.part, session: s.id, level: s.level };36 const context = `Séance ${s.number} · ${partLabel(s.part)}`;37 docs.push({ id: `seance:${s.id}`, type: "seance", title: s.title, text: [...s.outcomes, s.tags.join(", ")].join(" "), url: sessionPath(s.slug), context: `${context} · ${accessLabel(s)}`, ...base });38 if (s.access !== "open") continue;39 for (const c of s.capsules) {40 const { chapters, transcript } = await getCapsuleData(c);41 const label = c.synthetic ? `${context} · vidéo de test technique` : context;42 for (const ch of chapters ?? []) docs.push({ id: `chapitre:${s.id}:${c.id}:${ch.start}`, type: "chapitre", title: ch.title, text: c.title, url: watchPath(s.slug, { capsule: c.id, t: ch.start }), t: ch.start, context: label, ...base });43 for (const seg of transcript?.segments ?? []) docs.push({ id: `transcription:${s.id}:${c.id}:${seg.start}`, type: "transcription", title: c.title, text: seg.text, url: watchPath(s.slug, { capsule: c.id, t: seg.start }), t: seg.start, context: label, ...base });44 }45 // Slides: one document each, with the text read from the slide; a hit opens the viewer on that slide.46 const deck = await getDeck(s);47 for (const sl of deck?.slides ?? []) if (sl.kind === "content" || sl.kind === "section") docs.push({ id: `diapositive:${s.id}:${sl.n}`, type: "diapositive", title: sl.title, text: [sl.text, ...sl.figures.map((f) => f.alt)].join(" ").slice(0, 4000), url: `${sessionPath(s.slug)}#diapositive-${sl.n}`, context: `${context} · diapositive ${sl.n} sur ${deck!.count}`, ...base });48 const lab = await getLab(s);49 if (lab) docs.push({ id: `labo:${s.id}`, type: "ressource", title: `Laboratoire — ${s.short}`, text: stripMarkdown(lab).slice(0, 6000), url: `${sessionPath(s.slug)}#laboratoire`, context, ...base });50 for (const r of s.resources) docs.push({ id: `ressource:${s.id}:${r.id}`, type: "ressource", title: r.title, text: r.description ?? "", url: `${sessionPath(s.slug)}#ressources`, context, ...base });51 }52 for (const t of await termsOf(course)) {53 const s = course.sessions.find((x) => x.id === t.session)!;54 docs.push({ id: `glossaire:${t.id}`, type: "glossaire", title: t.term, text: `${t.english ?? ""} ${t.definition}`, url: glossaryPath(t.id), context: `Glossaire · séance ${s.number}`, part: s.part, session: s.id, level: s.level });55 }56 for (const d of DIAGRAMS) if (course.diagrams.includes(d.id)) docs.push({ id: `schema:${d.id}`, type: "schema", title: d.title, text: d.summary, url: diagramPath(d.id), context: `Schéma interactif nº ${d.number}` });57 for (const [url, title, text] of PAGES) docs.push({ id: `page:${url}`, type: "page", title, text, url, context: "Page du site" });58 return docs;59}6061type G = typeof globalThis & { __atlasSearch?: { key: string; until: number; body: string } };6263/**64 * The public index as JSON. Building it reads every published transcript, so the result is kept in memory65 * for one content revision (a publication, a rollback or an import bumps it) and never longer than a minute,66 * which bounds how late a scheduled publication can enter the index.67 */68export async function searchIndexJson(force = false): Promise<string> {69 const g = globalThis as G, key = `${env.dataDir}:${env.mode}:${studioIsSource() ? contentRev() : "seed"}`;70 if (!force && g.__atlasSearch?.key === key && Date.now() < g.__atlasSearch.until) return g.__atlasSearch.body;71 const body = JSON.stringify({ v: 1, docs: await buildSearchDocs() });72 g.__atlasSearch = { key, until: Date.now() + 60_000, body };73 return body;74}75