diff --git a/next.constants.mjs b/next.constants.mjs index 4d8041b92be8f..1949bd464ff39 100644 --- a/next.constants.mjs +++ b/next.constants.mjs @@ -91,6 +91,12 @@ export const DEFAULT_LOCALE_CODE = defaultLocale.code; */ export const LEGACY_JAVASCRIPT_FILE = `${BASE_PATH}/static/js/legacyMain.js`; +/** + * This is the current Node.js Working directory. We usually use it to resolve pathnames + * relative to the content of the Node.js Website. + */ +export const CURRENT_WORKING_DIRECTORY = process.cwd(); + /** * This is a list of all static routes or pages from the Website that we do not * want to allow to be statically built on our Static Export Build. diff --git a/next.dynamic.mjs b/next.dynamic.mjs index 1c64202530a62..685b58963b267 100644 --- a/next.dynamic.mjs +++ b/next.dynamic.mjs @@ -10,11 +10,15 @@ import rehypeSlug from 'rehype-slug'; import { serialize } from 'next-mdx-remote/serialize'; import { availableLocales } from './next.locales.mjs'; import { getMarkdownFiles } from './next.helpers.mjs'; -import { DEFAULT_LOCALE_CODE, MD_EXTENSION_REGEX } from './next.constants.mjs'; +import { + DEFAULT_LOCALE_CODE, + MD_EXTENSION_REGEX, + CURRENT_WORKING_DIRECTORY, +} from './next.constants.mjs'; // allows us to run a glob to get markdown files based on a language folder const getPathsByLanguage = async (locale = DEFAULT_LOCALE_CODE, ignored = []) => - getMarkdownFiles(process.cwd(), `pages/${locale}`, ignored); + getMarkdownFiles(CURRENT_WORKING_DIRECTORY, `pages/${locale}`, ignored); /** * This method is responsible for generating a Collection of all available paths that @@ -41,6 +45,7 @@ const getAllPaths = async () => { sourcePages.map(filename => { // remove the index.md(x) suffix from a pathname let pathname = filename.replace(MD_EXTENSION_REGEX, ''); + // remove trailing slash for correct Windows pathing of the index files if (pathname.length > 1 && pathname.endsWith(sep)) { pathname = pathname.substring(0, pathname.length - 1); @@ -110,7 +115,7 @@ export const getMarkdownFile = ( // gets the full pathname for the file (absolute path) metadata.filename = join( - process.cwd(), + CURRENT_WORKING_DIRECTORY, 'pages', localeToUse, route.filename diff --git a/pages/[...path].tsx b/pages/[...path].tsx index 2305f96c9561d..d9247e14438be 100644 --- a/pages/[...path].tsx +++ b/pages/[...path].tsx @@ -1,3 +1,4 @@ +import { sep } from 'node:path'; import Theme from '@/theme'; import * as nextDynamic from '@/next.dynamic.mjs'; import * as nextConstants from '@/next.constants.mjs'; @@ -28,7 +29,9 @@ const getRouteWrite = (pathname: string) => // This maps a pathname into an actual route object that can be used const mapPathnameToRoute = (pathname: string) => ({ - params: { path: pathname.split('/') }, + // we use a platform-specific separator to split the pathname + // since we're using filepaths here and not URL paths + params: { path: pathname.split(sep) }, }); // This method receives the props from `getStaticProps` and renders/builds the Markdown