()
| 49 | }; |
| 50 | |
| 51 | const getDynamicRouter = async () => { |
| 52 | // Creates a Cache System that is disabled during development mode |
| 53 | const cachedMarkdownFiles = createCachedMarkdownCache(); |
| 54 | |
| 55 | // Keeps the map of pathnames to filenames |
| 56 | const pathnameToFilename = new Map(); |
| 57 | |
| 58 | // Pre-compute the pages directory path to avoid Turbopack's overly broad |
| 59 | // file pattern analysis when using path.join() with dynamic segments |
| 60 | const pagesDirectory = join(process.cwd(), 'pages'); |
| 61 | |
| 62 | const websitePages = await getMarkdownFiles( |
| 63 | process.cwd(), |
| 64 | `pages/${defaultLocale.code}` |
| 65 | ); |
| 66 | |
| 67 | websitePages.forEach(filename => { |
| 68 | // This Regular Expression is used to remove the `index.md(x)` suffix |
| 69 | // of a name and to remove the `.md(x)` extensions of a filename. |
| 70 | let pathname = filename.replace(/((\/)?(index))?\.mdx?$/i, ''); |
| 71 | |
| 72 | if (pathname.length > 1 && pathname.endsWith(sep)) { |
| 73 | pathname = pathname.substring(0, pathname.length - 1); |
| 74 | } |
| 75 | |
| 76 | pathname = normalize(pathname).replace('.', ''); |
| 77 | |
| 78 | // We map the pathname to the filename to be able to quickly |
| 79 | // resolve the filename for a given pathname |
| 80 | pathnameToFilename.set(pathname, filename); |
| 81 | }); |
| 82 | |
| 83 | /** |
| 84 | * This method returns a list of all routes that exist |
| 85 | * Note: It will only match routes that have at least one pathname. |
| 86 | |
| 87 | * @returns {Promise<Array<string>>} |
| 88 | */ |
| 89 | const getAllRoutes = async () => |
| 90 | [...pathnameToFilename.keys()].filter(pathname => pathname.length); |
| 91 | |
| 92 | /** |
| 93 | * This method attempts to retrieve either a localized Markdown file |
| 94 | * or the English version of the Markdown file if no localized version exists |
| 95 | * and then returns the contents of the file and the name of the file (not the path) |
| 96 | * |
| 97 | * @param {string} locale |
| 98 | * @param {string} pathname |
| 99 | * @returns {Promise<{ source: string; filename: string }>} |
| 100 | */ |
| 101 | const _getMarkdownFile = async (locale = '', pathname = '') => { |
| 102 | const normalizedPathname = normalize(pathname).replace('.', ''); |
| 103 | |
| 104 | // This verifies if the given pathname actually exists on our Map |
| 105 | // meaning that the route exists on the website and can be rendered |
| 106 | if (pathnameToFilename.has(normalizedPathname)) { |
| 107 | const filename = pathnameToFilename.get(normalizedPathname); |
| 108 | const filepath = normalize(`${pagesDirectory}/${locale}/${filename}`); |
no test coverage detected