Become a Pro / website · becomeapro.ai / scripts / check-lessons.mjs
| 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) |
| 3 | import fs from "fs"; |
| 4 | import path from "path"; |
| 5 | import matter from "gray-matter"; |
| 6 | import { compile } from "@mdx-js/mdx"; |
| 7 | import remarkGfm from "remark-gfm"; |
| 8 | |
| 9 | const root = process.argv[2] || "content/lessons"; |
| 10 | const 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 | |
| 19 | let failed = 0; |
| 20 | for (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 | const shown = `${data.title} ${data.description} ${content}`; |
| 26 | if (/\b(business|businesses|branch|branches)\b/i.test(shown)) problems.push('uses the word "business" or "branch"'); |
| 27 | if (raw.includes("—")) problems.push("has an em dash"); |
| 28 | if (/youtube|<video|<iframe/i.test(content)) problems.push("has video or an embed"); |
| 29 | const prose = content.replace(/```[\s\S]*?```/g, "").replace(/`[^`\n]*`/g, "").replace(/https?:\/\/(localhost|example\.com)\S*/g, ""); |
| 30 | if (/https?:\/\/|<Resources?\b/.test(prose)) problems.push("shows an external link or credit (put URLs in front matter sources)"); |
| 31 | if (data.sources && !Array.isArray(data.sources)) problems.push("sources must be a list of URLs"); |
| 32 | try { |
| 33 | await compile(content, { remarkPlugins: [remarkGfm] }); |
| 34 | } catch (error) { |
| 35 | problems.push(`MDX error: ${error.message}`); |
| 36 | } |
| 37 | if (problems.length) { |
| 38 | failed++; |
| 39 | console.log(`${file}\n - ${problems.join("\n - ")}`); |
| 40 | } |
| 41 | } |
| 42 | console.log(`${files.length} lessons checked, ${failed} with problems`); |
| 43 | process.exit(failed ? 1 : 0); |