AI Resource Pro / website · airesourcepro.com / src / lib / tutorials.ts
| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | import matter from "gray-matter"; |
| 4 | import { slugify } from "@/lib/utils"; |
| 5 | import { TRACKS, type TrackSlug } from "@/lib/tracks"; |
| 6 | import type { TutorialMeta } from "@/lib/tutorial-types"; |
| 7 | |
| 8 | export * from "@/lib/tutorial-types"; |
| 9 | |
| 10 | export type Tutorial = TutorialMeta & { |
| 11 | body: string; |
| 12 | headings: { id: string; text: string; depth: 2 | 3 }[]; |
| 13 | }; |
| 14 | |
| 15 | const CONTENT_DIR = path.join(process.cwd(), "content", "tutorials"); |
| 16 | const COVERS_DIR = path.join(process.cwd(), "public", "covers"); |
| 17 | |
| 18 | // Covers are rendered by `npm run covers`; a tutorial without its own uses the track's |
| 19 | function coverFor(track: TrackSlug, slug: string): string { |
| 20 | return fs.existsSync(path.join(COVERS_DIR, track, `${slug}.webp`)) ? `/covers/${track}/${slug}.webp` : `/covers/${track}.webp`; |
| 21 | } |
| 22 | |
| 23 | // gray-matter parses unquoted YAML dates into Date objects |
| 24 | function isoDay(value: unknown): string { |
| 25 | return value instanceof Date ? value.toISOString().slice(0, 10) : String(value ?? "").slice(0, 10); |
| 26 | } |
| 27 | |
| 28 | // A full ISO 8601 date-time with an explicit offset, e.g. 2026-11-05T07:51:29-08:00 (Google needs the time zone) |
| 29 | const DATE_TIME_WITH_OFFSET = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/; |
| 30 | |
| 31 | // The video's original publish time, sent as the VideoObject's uploadDate |
| 32 | function videoUploadedAt(value: unknown, where: string): string | undefined { |
| 33 | if (value === undefined || value === null || value === "") return undefined; |
| 34 | const text = value instanceof Date ? value.toISOString() : String(value).trim(); |
| 35 | if (!DATE_TIME_WITH_OFFSET.test(text) || Number.isNaN(Date.parse(text))) { |
| 36 | throw new Error(`Tutorial ${where} has an invalid videoUploadedAt "${text}": use the original publish time with a time zone, e.g. "2026-11-05T07:51:29-08:00"`); |
| 37 | } |
| 38 | return text; |
| 39 | } |
| 40 | |
| 41 | function readTutorial(track: TrackSlug, file: string): Tutorial { |
| 42 | const raw = fs.readFileSync(path.join(CONTENT_DIR, track, file), "utf8"); |
| 43 | const { data, content } = matter(raw); |
| 44 | const slug = file.replace(/\.mdx?$/, ""); |
| 45 | |
| 46 | if (!data.title || !data.description) { |
| 47 | throw new Error(`Tutorial ${track}/${file} is missing a title or description`); |
| 48 | } |
| 49 | |
| 50 | if (data.video && !data.videoUploadedAt) { |
| 51 | throw new Error(`Tutorial ${track}/${file} has a video but no videoUploadedAt (the video's publish time)`); |
| 52 | } |
| 53 | |
| 54 | // Headings outside fenced code blocks feed the "On this page" outline |
| 55 | const headings: Tutorial["headings"] = []; |
| 56 | let inFence = false; |
| 57 | for (const line of content.split("\n")) { |
| 58 | if (line.trim().startsWith("```")) inFence = !inFence; |
| 59 | const match = !inFence && line.match(/^(##|###)\s+(.+)$/); |
| 60 | if (match) { |
| 61 | const text = match[2].trim(); |
| 62 | headings.push({ id: slugify(text), text, depth: match[1].length as 2 | 3 }); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return { |
| 67 | slug, |
| 68 | track, |
| 69 | title: data.title, |
| 70 | description: data.description, |
| 71 | level: data.level ?? "Beginner", |
| 72 | minutes: data.minutes ?? 10, |
| 73 | publishedAt: isoDay(data.publishedAt), |
| 74 | updatedAt: data.updatedAt ? isoDay(data.updatedAt) : undefined, |
| 75 | video: data.video, |
| 76 | videoCaptions: data.videoCaptions === true, |
| 77 | videoUploadedAt: videoUploadedAt(data.videoUploadedAt, `${track}/${file}`), |
| 78 | videoComingSoon: data.videoComingSoon === true, |
| 79 | draft: data.draft === true, |
| 80 | cover: coverFor(track, slug), |
| 81 | body: content, |
| 82 | headings, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | export function getAllTutorials(): Tutorial[] { |
| 87 | return TRACKS.flatMap((track) => { |
| 88 | const dir = path.join(CONTENT_DIR, track.slug); |
| 89 | if (!fs.existsSync(dir)) return []; |
| 90 | return fs |
| 91 | .readdirSync(dir) |
| 92 | .filter((f) => /\.mdx?$/.test(f)) |
| 93 | .map((f) => readTutorial(track.slug, f)); |
| 94 | }).sort((a, b) => b.publishedAt.localeCompare(a.publishedAt)); |
| 95 | } |
| 96 | |
| 97 | export function getTutorialMetas(): TutorialMeta[] { |
| 98 | return getAllTutorials().map(({ body, headings, ...meta }) => meta); |
| 99 | } |
| 100 | |
| 101 | export function getTutorialsByTrack(track: TrackSlug): TutorialMeta[] { |
| 102 | return getTutorialMetas().filter((t) => t.track === track); |
| 103 | } |
| 104 | |
| 105 | export function getVideoTutorials(): TutorialMeta[] { |
| 106 | return getTutorialMetas().filter((t) => t.video); |
| 107 | } |
| 108 | |
| 109 | // What goes in the sitemap and search results: drafts stay out until `draft: true` is removed |
| 110 | export function getPublishedTutorials(): TutorialMeta[] { |
| 111 | return getTutorialMetas().filter((t) => !t.draft); |
| 112 | } |
| 113 | |
| 114 | export function getTutorial(track: string, slug: string): Tutorial | undefined { |
| 115 | return getAllTutorials().find((t) => t.track === track && t.slug === slug); |
| 116 | } |