Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filename contain extname like suffix #1035

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading