Skip to content

Commit

Permalink
fix: filename contain extname like suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Apr 28, 2024
1 parent 088e8d3 commit bbe2c34
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions e2e/fixtures/auto-nav-sidebar/doc/api/config/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"type": "file",
"name": "config-build",
"overviewHeaders": [2, 3]
},
{
"type": "file",
"name": "config-extname.json"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Extname Config
2 changes: 2 additions & 0 deletions e2e/tests/auto-nav-sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ test.describe('Auto nav and sidebar test', async () => {
'Theme Config',
'Front Matter Config',
'Build Config',
'Extname Config',
'Client API Overview',
'Runtime API',
'Components',
Expand Down Expand Up @@ -102,6 +103,7 @@ test.describe('Auto nav and sidebar test', async () => {
'Theme Config',
'Front Matter Config',
'Build Config',
'Extname Config',
].join(','),
);

Expand Down
14 changes: 11 additions & 3 deletions packages/plugin-auto-nav-sidebar/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export async function detectFilePath(rawPath: string) {
const extensions = ['.mdx', '.md', '.tsx', '.jsx', '.ts', '.js'];
// The params doesn't have extension name, so we need to try to find the file with the extension name.
let realPath: string | undefined = rawPath;
const filename = path.basename(rawPath);
if (filename.indexOf('.') === -1) {
const fileExtname = path.extname(rawPath);

// pathname may contain .json, see issue: https://github.com/web-infra-dev/rspress/issues/951
if (!extensions.includes(fileExtname)) {
const pathWithExtension = extensions.map(ext => `${rawPath}${ext}`);
const pathExistInfo = await Promise.all(
pathWithExtension.map(p => fs.pathExists(p)),
Expand All @@ -23,7 +25,11 @@ export async function detectFilePath(rawPath: string) {
export async function extractTitleAndOverviewHeaders(
filePath: string,
rootDir: string,
): Promise<{ title: string; overviewHeaders: string | undefined }> {
): Promise<{
realPath: string | undefined;
title: string;
overviewHeaders: string | undefined;
}> {
const realPath = await detectFilePath(filePath);
if (!realPath) {
logger.warn(
Expand All @@ -33,6 +39,7 @@ export async function extractTitleAndOverviewHeaders(
)}".`,
);
return {
realPath,
title: '',
overviewHeaders: undefined,
};
Expand All @@ -43,6 +50,7 @@ export async function extractTitleAndOverviewHeaders(
const match = content.match(h1RegExp);
const { frontmatter } = loadFrontMatter(content, filePath, rootDir);
return {
realPath,
title: frontmatter.title || match?.[1] || fileNameWithoutExt,
overviewHeaders: frontmatter.overviewHeaders,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-auto-nav-sidebar/src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export async function scanSideMeta(
rootDir,
);
const title = label || titleAndOverviewHeaders.title;
const realPath = await detectFilePath(path.resolve(workDir, name));
const realPath = titleAndOverviewHeaders.realPath;
return {
text: title,
link: addRoutePrefix(pureLink),
Expand Down

0 comments on commit bbe2c34

Please sign in to comment.