Skip to content
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
9 changes: 9 additions & 0 deletions packages/docusaurus-utils/src/__tests__/urlUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ describe('toURLPath', () => {
hash: '',
});
});

it('pathname + hash containing a question mark (no query)', () => {
const url = parseURLOrPath('/pathname#hash?notquery');
expect(toURLPath(url)).toEqual({
pathname: '/pathname',
search: undefined,
hash: 'hash?notquery',
});
});
});

describe('parseLocalURLPath', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-utils/src/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@ export type URLPath = {pathname: string; search?: string; hash?: string};
export function toURLPath(url: URL): URLPath {
const {pathname} = url;

// Only the part before the fragment can contain the query string. A "?"
// inside the hash (e.g. "/foo#bar?baz") must not be read as an empty query.
const beforeHash = url.hash ? url.href.slice(0, -url.hash.length) : url.href;

// Fixes annoying url.search behavior
// "" => undefined
// "?" => ""
// "?param => "param"
const search = url.search
? url.search.slice(1)
: url.href.includes('?')
: beforeHash.includes('?')
? ''
: undefined;

Expand Down