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: When alias contains parameters, append ext error #1855

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions src/core/router/history/base.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
cleanPath,
getPath,
isAbsolutePath,
stringifyQuery,
cleanPath,
replaceSlug,
resolvePath,
stringifyQuery,
} from '../util.js';
import { noop } from '../../util/core.js';

Expand Down Expand Up @@ -32,11 +32,20 @@ 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)$`,
'g',
).test(basePath);

const updatedPath = hasValidExt
? basePath
: /\/$/g.test(basePath)
? `${basePath}README${ext}`
: `${basePath}${ext}`;

return query ? `${updatedPath}?${query}` : updatedPath;
}

getBasePath() {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/router-history-base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Loading