|
| 1 | +import { readdir, readFile } from "node:fs/promises"; |
| 2 | +import { extname, join, relative, sep } from "node:path"; |
| 3 | + |
| 4 | +const BASE_URL = "https://usememos.com"; |
| 5 | +const CONTENT_DIR = join(process.cwd(), "content"); |
| 6 | + |
| 7 | +async function listMdxFiles(dir) { |
| 8 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 9 | + const files = await Promise.all( |
| 10 | + entries.map(async (entry) => { |
| 11 | + const path = join(dir, entry.name); |
| 12 | + if (entry.isDirectory()) { |
| 13 | + return listMdxFiles(path); |
| 14 | + } |
| 15 | + |
| 16 | + return extname(entry.name) === ".mdx" ? [path] : []; |
| 17 | + }), |
| 18 | + ); |
| 19 | + |
| 20 | + return files.flat(); |
| 21 | +} |
| 22 | + |
| 23 | +function parseFrontmatter(source) { |
| 24 | + const match = source.match(/^---\r?\n([\s\S]*?)\r?\n---/); |
| 25 | + if (!match) { |
| 26 | + return {}; |
| 27 | + } |
| 28 | + |
| 29 | + const data = {}; |
| 30 | + const lines = match[1].split(/\r?\n/); |
| 31 | + for (const line of lines) { |
| 32 | + const field = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/); |
| 33 | + if (!field) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + const [, key, rawValue] = field; |
| 38 | + data[key] = rawValue.replace(/^["']|["']$/g, ""); |
| 39 | + } |
| 40 | + |
| 41 | + return data; |
| 42 | +} |
| 43 | + |
| 44 | +function toSlug(file, collection) { |
| 45 | + const collectionDir = join(CONTENT_DIR, collection); |
| 46 | + const path = relative(collectionDir, file) |
| 47 | + .replaceAll(sep, "/") |
| 48 | + .replace(/\.mdx$/, ""); |
| 49 | + return path.endsWith("/index") ? path.slice(0, -"/index".length) : path === "index" ? "" : path; |
| 50 | +} |
| 51 | + |
| 52 | +function toUrl(collection, slug) { |
| 53 | + const basePath = collection === "docs" ? "/docs" : `/${collection}`; |
| 54 | + return `${BASE_URL}${slug ? `${basePath}/${slug}` : basePath}`; |
| 55 | +} |
| 56 | + |
| 57 | +async function buildPreviews() { |
| 58 | + const docs = (await listMdxFiles(join(CONTENT_DIR, "docs"))).map((file) => { |
| 59 | + const slug = toSlug(file, "docs"); |
| 60 | + return { |
| 61 | + section: "Docs", |
| 62 | + kind: "generated", |
| 63 | + url: toUrl("docs", slug), |
| 64 | + }; |
| 65 | + }); |
| 66 | + |
| 67 | + const blogIndex = { |
| 68 | + section: "Blog", |
| 69 | + kind: "generated", |
| 70 | + url: `${BASE_URL}/blog`, |
| 71 | + }; |
| 72 | + |
| 73 | + const blog = await Promise.all( |
| 74 | + (await listMdxFiles(join(CONTENT_DIR, "blog"))).map(async (file) => { |
| 75 | + const source = await readFile(file, "utf8"); |
| 76 | + const frontmatter = parseFrontmatter(source); |
| 77 | + const slug = toSlug(file, "blog"); |
| 78 | + |
| 79 | + return { |
| 80 | + section: "Blog", |
| 81 | + kind: frontmatter.feature_image ? "explicit" : "generated", |
| 82 | + url: toUrl("blog", slug), |
| 83 | + }; |
| 84 | + }), |
| 85 | + ); |
| 86 | + |
| 87 | + const changelogIndex = { |
| 88 | + section: "Changelog", |
| 89 | + kind: "generated", |
| 90 | + url: `${BASE_URL}/changelog`, |
| 91 | + }; |
| 92 | + |
| 93 | + const changelog = (await listMdxFiles(join(CONTENT_DIR, "changelog"))).map((file) => { |
| 94 | + const slug = toSlug(file, "changelog"); |
| 95 | + return { |
| 96 | + section: "Changelog", |
| 97 | + kind: "generated", |
| 98 | + url: toUrl("changelog", slug), |
| 99 | + }; |
| 100 | + }); |
| 101 | + |
| 102 | + return [...docs, blogIndex, ...blog, changelogIndex, ...changelog].sort((a, b) => a.url.localeCompare(b.url)); |
| 103 | +} |
| 104 | + |
| 105 | +function printGroup(kind, previews) { |
| 106 | + console.log(`\n${kind} (${previews.length})`); |
| 107 | + for (const preview of previews) { |
| 108 | + console.log(` ${preview.section.padEnd(13)} ${preview.url}`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +const previews = await buildPreviews(); |
| 113 | +const groups = { |
| 114 | + default: previews.filter((preview) => preview.kind === "default"), |
| 115 | + generated: previews.filter((preview) => preview.kind === "generated"), |
| 116 | + explicit: previews.filter((preview) => preview.kind === "explicit"), |
| 117 | +}; |
| 118 | + |
| 119 | +console.log("Social preview audit for docs, blog, and changelog"); |
| 120 | +printGroup("default", groups.default); |
| 121 | +printGroup("generated", groups.generated); |
| 122 | +printGroup("explicit", groups.explicit); |
| 123 | + |
| 124 | +if (groups.default.length > 0) { |
| 125 | + console.error("\nDefault social previews remain in content routes. Assign explicit or generated images before shipping."); |
| 126 | + process.exitCode = 1; |
| 127 | +} |
0 commit comments