|
| 1 | +import type { MetadataRoute } from "next"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const BASE_URL = "https://cur8d.dev/typescript"; |
| 6 | +// Next.js runs from the project root during build |
| 7 | +const CONTENT_DIR = path.join(process.cwd(), "content"); |
| 8 | + |
| 9 | +function getMdxFiles(dir: string, baseDir: string): string[] { |
| 10 | + if (!fs.existsSync(dir)) { |
| 11 | + return []; |
| 12 | + } |
| 13 | + |
| 14 | + const files = fs.readdirSync(dir); |
| 15 | + let paths: string[] = []; |
| 16 | + |
| 17 | + for (const file of files) { |
| 18 | + const fullPath = path.join(dir, file); |
| 19 | + const stat = fs.statSync(fullPath); |
| 20 | + |
| 21 | + if (stat.isDirectory()) { |
| 22 | + paths = paths.concat(getMdxFiles(fullPath, baseDir)); |
| 23 | + } else if (file.endsWith(".mdx") || file.endsWith(".md")) { |
| 24 | + let relativePath = path.relative(baseDir, fullPath); |
| 25 | + // Normalize path separators for URLs (handle Windows) |
| 26 | + relativePath = relativePath.split(path.sep).join("/"); |
| 27 | + // Remove extension |
| 28 | + relativePath = relativePath.replace(/\.mdx?$/, ""); |
| 29 | + // Handle index files |
| 30 | + if (relativePath.endsWith("/index")) { |
| 31 | + relativePath = relativePath.slice(0, -6); |
| 32 | + } else if (relativePath === "index") { |
| 33 | + relativePath = ""; |
| 34 | + } |
| 35 | + paths.push(relativePath); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return paths; |
| 40 | +} |
| 41 | + |
| 42 | +export const dynamic = "force-static"; |
| 43 | + |
| 44 | +export default function sitemap(): MetadataRoute.Sitemap { |
| 45 | + const paths = getMdxFiles(CONTENT_DIR, CONTENT_DIR); |
| 46 | + |
| 47 | + return paths.map((p) => ({ |
| 48 | + url: `${BASE_URL}/${p}${p ? "/" : ""}`, |
| 49 | + lastModified: new Date(), |
| 50 | + changeFrequency: "monthly", |
| 51 | + priority: p === "" ? 1 : 0.8, |
| 52 | + })); |
| 53 | +} |
0 commit comments