fix: When alias contains parameters, append ext error (#1855)

This commit is contained in:
Luffy
2025-04-10 09:13:47 +08:00
committed by GitHub
parent 7652bd4711
commit 1a32fb7334
2 changed files with 46 additions and 7 deletions
+15 -7
View File
@@ -1,10 +1,10 @@
import {
cleanPath,
getPath,
isAbsolutePath,
stringifyQuery,
cleanPath,
replaceSlug,
resolvePath,
stringifyQuery,
} from '../util.js';
import { noop } from '../../util/core.js';
@@ -32,11 +32,19 @@ export class History {
}
#getFileName(path, ext) {
return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path)
? path
: /\/$/g.test(path)
? `${path}README${ext}`
: `${path}${ext}`;
const [basePath, query] = path.split('?');
const hasValidExt = new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`).test(
basePath,
);
const updatedPath = hasValidExt
? basePath
: /\/$/g.test(basePath)
? `${basePath}README${ext}`
: `${basePath}${ext}`;
return query ? `${updatedPath}?${query}` : updatedPath;
}
getBasePath() {
+31
View File
@@ -66,4 +66,35 @@ describe('router/history/base', () => {
expect(url).toBe('/README');
});
});
// getFile test
// ---------------------------------------------------------------------------
describe('getFile', () => {
// Tests
// -------------------------------------------------------------------------
test('path is url', () => {
const file = history.getFile('https://some/raw/url/README.md');
expect(file).toBe('https://some/raw/url/README.md');
});
test('path is url, but ext is .html', () => {
const file = history.getFile('https://foo.com/index.html');
expect(file).toBe('https://foo.com/index.html');
});
test('path is url, but with parameters', () => {
const file = history.getFile(
'https://some/raw/url/README.md?token=Mytoken',
);
expect(file).toBe('https://some/raw/url/README.md?token=Mytoken');
});
test('path is url, but ext is different', () => {
history = new MockHistory({ ext: '.ext' });
const file = history.getFile('https://some/raw/url/README.md');
expect(file).toBe('https://some/raw/url/README.md.ext');
});
});
});