Skip to content

Commit 2a44862

Browse files
committed
fixup!
1 parent 7717fdc commit 2a44862

File tree

4 files changed

+41
-75
lines changed

4 files changed

+41
-75
lines changed

apps/site/app/[locale]/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { joinNested } from '#site/util/array';
2323
import type { PageParams } from '#site/router/page';
2424
import type { FC } from 'react';
2525

26+
2627
// Generates all possible static paths based on the locales and environment configuration
2728
// - Returns an empty array if static export is disabled (`ENABLE_STATIC_EXPORT` is false)
2829
// - If `ENABLE_STATIC_EXPORT_LOCALE` is true, generates paths for all available locales

apps/site/next.helpers.mjs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
'use strict';
22

33
import { glob } from 'node:fs/promises';
4+
import { join } from 'node:path';
45
import { fileURLToPath } from 'node:url';
56

6-
/**
7-
* We create a locale cache of Glob Promises
8-
* to avoid reading the file system multiple times
9-
* this is done since we don't need to constantly re-run the glob
10-
* query as it is only needed once
11-
*
12-
* @type {Map<string, Promise<string>>} */
13-
const globCacheByPath = new Map();
7+
import { defaultLocale } from '@node-core/website-i18n/index.mjs';
148

159
/**
1610
* This gets the relative path from `import.meta.url`
@@ -20,28 +14,27 @@ const globCacheByPath = new Map();
2014
*/
2115
export const getRelativePath = path => fileURLToPath(new URL('.', path));
2216

17+
// TODO(@avivkeller): Turbopack doesn't support `import.meta.resolve`-ing this path
18+
export const contentBase = join(
19+
process.cwd(),
20+
`../../packages/content/src/${defaultLocale.code}/`
21+
);
22+
2323
/**
2424
* This method is responsible for retrieving a glob of all files that exist
2525
* within a given language directory
2626
*
2727
* Note that we ignore the blog directory for static builds as otherwise generating
2828
* that many pages would be too much for the build process to handle.
2929
*
30-
* @param {string} root the root directory to search from
31-
* @param {string} cwd the given locale code
32-
* @param {Array<string>} exclude an array of glob patterns to ignore
30+
* @param {import('node:fs').GlobOptions} options
3331
* @returns {Promise<Array<string>>} a promise containing an array of paths
3432
*/
35-
export const getMarkdownFiles = async (root, cwd, exclude = []) => {
36-
const cacheKey = `${root}${cwd}${exclude.join('')}`;
37-
38-
if (!globCacheByPath.has(cacheKey)) {
39-
const result = Array.fromAsync(
40-
glob('**/*.{md,mdx}', { root, cwd, exclude })
41-
);
42-
43-
globCacheByPath.set(cacheKey, result);
44-
}
45-
46-
return globCacheByPath.get(cacheKey);
33+
export const getMarkdownFiles = async (options = {}) => {
34+
return Array.fromAsync(
35+
glob('**/*.{md,mdx}', {
36+
cwd: contentBase,
37+
...options,
38+
})
39+
);
4740
};

apps/site/router/index.ts

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { PAGE_METADATA } from './constants';
1818

1919
// Constants
2020
const BASE_URL_PATH = `${BASE_URL}${BASE_PATH}`;
21-
const MARKDOWN_EXTENSIONS = ['.mdx', '.md', '/index.mdx', '/index.md'] as const;
2221
const INDEX_PATTERN = /((\/)?(index))?\.mdx?$/i;
2322

2423
// Cache for pathname to filename mappings
@@ -28,7 +27,9 @@ const pathnameToFilename = new Map<string, string>();
2827
* Normalizes a path to always use forward slashes
2928
*/
3029
const normalizePath = (path: string): string =>
31-
normalize(path).replace(/\\/g, '/');
30+
normalize(path)
31+
.replace(/\\/g, '/')
32+
.replace(/^\. \//, '');
3233

3334
/**
3435
* Normalizes a filename into a pathname by removing file extensions and index suffixes
@@ -41,29 +42,26 @@ const normalizePathname = (filename: string): string => {
4142
pathname = pathname.slice(0, -1);
4243
}
4344

44-
return normalizePath(pathname).replace(/^\. \//, '');
45+
return normalizePath(pathname);
4546
};
4647

4748
/**
48-
* Constructs the import path for a markdown file
49-
*/
50-
const buildImportPath = (
51-
locale: string,
52-
pathname: string,
53-
extension: string
54-
): string => `../../../packages/content/src/${locale}/${pathname}${extension}`;
55-
56-
/**
57-
* Attempts to import a markdown file with a specific extension
49+
* Attempts to import a markdown file for a given locale and pathname
5850
*/
59-
const tryImportWithExtension = async (
51+
const tryImportMarkdown = async (
6052
locale: string,
61-
normalizedPath: string,
62-
extension: string
53+
pathname: string
6354
): Promise<MarkdownFile | null> => {
55+
const normalizedPath = normalizePath(pathname);
56+
const filename = pathnameToFilename.get(normalizedPath.replace(/^\.\//, ''));
57+
58+
if (!filename) {
59+
return null;
60+
}
61+
6462
try {
6563
const mod = await import(
66-
buildImportPath(locale, normalizedPath, extension)
64+
`../../../packages/content/src/${locale}/${filename}`
6765
);
6866

6967
return {
@@ -72,37 +70,13 @@ const tryImportWithExtension = async (
7270
readingTime: mod.readingTime,
7371
headings: mod.headings,
7472
pathname: `/${normalizedPath}`,
75-
filename: pathnameToFilename.get(normalizedPath.replace(/^\.\//, ''))!,
73+
filename,
7674
};
7775
} catch {
7876
return null;
7977
}
8078
};
8179

82-
/**
83-
* Attempts to import a markdown file for a given locale and pathname
84-
* Tries multiple file extensions in order of preference
85-
*/
86-
const tryImportMarkdown = async (
87-
locale: string,
88-
pathname: string
89-
): Promise<MarkdownFile | null> => {
90-
const normalizedPath = normalizePath(pathname);
91-
92-
for (const extension of MARKDOWN_EXTENSIONS) {
93-
const result = await tryImportWithExtension(
94-
locale,
95-
normalizedPath,
96-
extension
97-
);
98-
if (result) {
99-
return result;
100-
}
101-
}
102-
103-
return null;
104-
};
105-
10680
/**
10781
* Retrieves a markdown file with locale fallback support
10882
* Tries the requested locale first, then falls back to the default locale
@@ -207,9 +181,7 @@ export const getPageMetadata = cache(async (locale: string, path: string) => {
207181
/**
208182
* All available routes in the application
209183
*/
210-
export const allRoutes = (
211-
await getMarkdownFiles(process.cwd(), `pages/${defaultLocale.code}`)
212-
)
184+
export const allRoutes = (await getMarkdownFiles())
213185
.map(filename => {
214186
const pathname = normalizePathname(filename);
215187
pathnameToFilename.set(pathname, filename);

apps/site/scripts/blog-data/generate.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import readline from 'node:readline';
66

77
import graymatter from 'gray-matter';
88

9-
import { getMarkdownFiles } from '#site/next.helpers.mjs';
9+
import { contentBase, getMarkdownFiles } from '#site/next.helpers.mjs';
1010

11-
// gets the current blog path based on local module path
12-
const blogPath = join(process.cwd(), 'pages/en/blog');
11+
const blogBase = join(contentBase, 'blog');
1312

1413
/**
1514
* This method parses the source (raw) Markdown content into Frontmatter
@@ -55,9 +54,10 @@ const getFrontMatter = (filename, source) => {
5554
*/
5655
const generateBlogData = async () => {
5756
// We retrieve the full pathnames of all Blog Posts to read each file individually
58-
const filenames = await getMarkdownFiles(process.cwd(), 'pages/en/blog', [
59-
'**/index.md',
60-
]);
57+
const filenames = await getMarkdownFiles({
58+
cwd: blogBase,
59+
exclude: ['**/index.md'],
60+
});
6161

6262
/**
6363
* This contains the metadata of all available blog categories
@@ -69,7 +69,7 @@ const generateBlogData = async () => {
6969
filename =>
7070
new Promise(resolve => {
7171
// We create a stream for reading a file instead of reading the files
72-
const _stream = createReadStream(join(blogPath, filename));
72+
const _stream = createReadStream(join(blogBase, filename));
7373

7474
// We create a readline interface to read the file line-by-line
7575
const _readLine = readline.createInterface({ input: _stream });

0 commit comments

Comments
 (0)