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

Encode sublinks in url to fix broken routes #1389

Merged
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
19 changes: 19 additions & 0 deletions src/client/utils/__tests__/getUrl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ describe('getUrl', () => {
expect(result).toBe('/styleguide/#/Documentation/%40foo%2Fcomponents');
});

it('should return a route path with encoded name if sections (hashPath) has inappropriate symbols', () => {
expect(
getUrl({ name: '@foo/components', slug, hashPath: ['@foo/bar-documentation'] }, loc)
).toBe('/styleguide/#/%40foo%2Fbar-documentation/%40foo%2Fcomponents');

expect(
getUrl(
{
name: '@foo/components',
slug,
hashPath: ['@foo/bar-documentation', '@foo/bar-activations-section'],
},
loc
)
).toBe(
'/styleguide/#/%40foo%2Fbar-documentation/%40foo%2Fbar-activations-section/%40foo%2Fcomponents'
);
});

it('should return a route path with a param id=foobar', () => {
const result = getUrl({ name, slug, hashPath: ['Documentation'], id: true }, loc);
expect(result).toBe('/styleguide/#/Documentation?id=foobar');
Expand Down
5 changes: 3 additions & 2 deletions src/client/utils/getUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export default function getUrl(
}

if (hashPath) {
let encodedHashPath = hashPath.map(encodeURIComponent);
if (!id) {
hashPath = [...hashPath, encodedName];
encodedHashPath = [...encodedHashPath, encodedName];
}
url += `#/${hashPath.join('/')}`;
url += `#/${encodedHashPath.join('/')}`;
}

if (id) {
Expand Down