|
| 1 | +import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const repoRoot = process.cwd(); |
| 5 | +const sourceDir = path.join(repoRoot, "content"); |
| 6 | +const outputDir = path.join(repoRoot, ".generated-content"); |
| 7 | + |
| 8 | +async function findMarkdownFiles(dir) { |
| 9 | + let entries; |
| 10 | + |
| 11 | + try { |
| 12 | + entries = await readdir(dir, { withFileTypes: true }); |
| 13 | + } catch (error) { |
| 14 | + if (error.code === "ENOENT") { |
| 15 | + return []; |
| 16 | + } |
| 17 | + |
| 18 | + throw error; |
| 19 | + } |
| 20 | + |
| 21 | + const files = []; |
| 22 | + |
| 23 | + for (const entry of entries) { |
| 24 | + const fullPath = path.join(dir, entry.name); |
| 25 | + |
| 26 | + if (entry.isDirectory()) { |
| 27 | + files.push(...await findMarkdownFiles(fullPath)); |
| 28 | + } else if (entry.isFile() && /\.md$/i.test(entry.name)) { |
| 29 | + files.push(fullPath); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return files; |
| 34 | +} |
| 35 | + |
| 36 | +function getFrontMatter(markdown, filePath) { |
| 37 | + if (!markdown.startsWith("---")) { |
| 38 | + throw new Error(`${filePath} must start with YAML front matter.`); |
| 39 | + } |
| 40 | + |
| 41 | + const endMatch = markdown.match(/\r?\n---\r?\n/); |
| 42 | + |
| 43 | + if (!endMatch || endMatch.index === undefined) { |
| 44 | + throw new Error(`${filePath} must include closing YAML front matter.`); |
| 45 | + } |
| 46 | + |
| 47 | + return markdown.slice(3, endMatch.index); |
| 48 | +} |
| 49 | + |
| 50 | +function readUrl(frontMatter, filePath) { |
| 51 | + const urlMatch = frontMatter.match(/^url:\s*(?<url>.+?)\s*$/m); |
| 52 | + const url = urlMatch?.groups?.url |
| 53 | + ?.replace(/^['"]|['"]$/g, "") |
| 54 | + ?.trim(); |
| 55 | + |
| 56 | + if (!url) { |
| 57 | + throw new Error(`${filePath} must include a non-empty 'url' front matter value.`); |
| 58 | + } |
| 59 | + |
| 60 | + return url; |
| 61 | +} |
| 62 | + |
| 63 | +await rm(outputDir, { recursive: true, force: true }); |
| 64 | +await mkdir(outputDir, { recursive: true }); |
| 65 | + |
| 66 | +const sourceFiles = await findMarkdownFiles(sourceDir); |
| 67 | +const slugs = new Set(); |
| 68 | + |
| 69 | +for (const sourceFile of sourceFiles) { |
| 70 | + const slug = path.basename(sourceFile, path.extname(sourceFile)); |
| 71 | + |
| 72 | + if (slug.startsWith("_")) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + if (slugs.has(slug)) { |
| 77 | + throw new Error(`Duplicate redirect slug '${slug}'. Markdown file names must be unique.`); |
| 78 | + } |
| 79 | + |
| 80 | + slugs.add(slug); |
| 81 | + |
| 82 | + const markdown = await readFile(sourceFile, "utf8"); |
| 83 | + const frontMatter = getFrontMatter(markdown, sourceFile); |
| 84 | + const url = readUrl(frontMatter, sourceFile); |
| 85 | + const generatedMarkdown = [ |
| 86 | + "---", |
| 87 | + `title: ${JSON.stringify(slug)}`, |
| 88 | + `redirect_url: ${JSON.stringify(url)}`, |
| 89 | + `source_slug: ${JSON.stringify(slug)}`, |
| 90 | + "---", |
| 91 | + "" |
| 92 | + ].join("\n"); |
| 93 | + |
| 94 | + await writeFile(path.join(outputDir, `${slug}.md`), generatedMarkdown, "utf8"); |
| 95 | +} |
| 96 | + |
| 97 | +console.log(`Prepared ${slugs.size} redirect page${slugs.size === 1 ? "" : "s"}.`); |
0 commit comments