Branchwell

Become a Pro / website · becomeapro.ai / scripts / check-lessons.mjs

Raw 1.5 KB on f63bc385c0fa362af985bfb576a8b5aa2a3df60f
1// Checks lesson MDX files compile and follow the basic rules in docs/LESSONS.md.
2// Usage: node scripts/check-lessons.mjs [dir] (default content/lessons)
3import fs from "fs";
4import path from "path";
5import matter from "gray-matter";
6import { compile } from "@mdx-js/mdx";
7import remarkGfm from "remark-gfm";
8
9const root = process.argv[2] || "content/lessons";
10const files = [];
11(function walk(dir) {
12 for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
13 const full = path.join(dir, entry.name);
14 if (entry.isDirectory()) walk(full);
15 else if (entry.name.endsWith(".mdx")) files.push(full);
16 }
17})(root);
18
19let failed = 0;
20for (const file of files) {
21 const problems = [];
22 const raw = fs.readFileSync(file, "utf8");
23 const { data, content } = matter(raw);
24 for (const key of ["title", "description", "updated", "minutes"]) if (!data[key]) problems.push(`missing ${key}`);
25 if (/\b(business|businesses|branch|branches)\b/i.test(raw)) problems.push('uses the word "business" or "branch"');
26 if (raw.includes("—")) problems.push("has an em dash");
27 if (/youtube|<video|<iframe/i.test(content)) problems.push("has video or an embed");
28 try {
29 await compile(content, { remarkPlugins: [remarkGfm] });
30 } catch (error) {
31 problems.push(`MDX error: ${error.message}`);
32 }
33 if (problems.length) {
34 failed++;
35 console.log(`${file}\n - ${problems.join("\n - ")}`);
36 }
37}
38console.log(`${files.length} lessons checked, ${failed} with problems`);
39process.exit(failed ? 1 : 0);