Skip to content

Commit 55577d2

Browse files
author
Ives van Hoorne
committed
Change url-generator to encode git paths
1 parent bff356f commit 55577d2

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/app/utils/url-generator.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@ export const host = () => {
99

1010
export const protocolAndHost = () => `${location.protocol}//${host()}`;
1111

12+
export const newSandboxUrl = () => `/s/new`;
13+
14+
const sandboxGitUrl = (git: {
15+
repo: string,
16+
branch: string,
17+
username: string,
18+
path: string,
19+
}) =>
20+
'github/' + // eslint-disable-line prefer-template
21+
encodeURIComponent(git.username) +
22+
'/' +
23+
encodeURIComponent(git.repo) +
24+
'/tree/' +
25+
encodeURIComponent(git.branch) +
26+
'/' +
27+
encodeURIComponent(git.path);
28+
1229
export const sandboxUrl = (sandbox: Sandbox) => {
1330
if (sandbox.git) {
1431
const { git } = sandbox;
15-
return `/s/github/${git.username}/${git.repo}/tree/${git.branch}/${git.path}`;
32+
return `/s/${sandboxGitUrl(git)}`;
1633
}
1734

1835
return `/s/${sandbox.id}`;
1936
};
20-
export const newSandboxUrl = () => `/s/new`;
2137
export const embedUrl = (sandbox: Sandbox) => {
2238
if (sandbox.git) {
2339
const { git } = sandbox;
24-
return `/embed/github/${git.username}/${git.repo}/tree/${git.branch}/${git.path}`;
40+
return `/embed/${sandboxGitUrl(git)}`;
2541
}
2642

2743
return `/embed/${sandbox.id}`;
@@ -60,9 +76,11 @@ export const githubRepoUrl = ({
6076

6177
export const optionsToParameterizedUrl = (options: Object) => {
6278
const keyValues = Object.keys(options)
63-
.map(key => `${key}=${options[key]}`)
79+
.sort()
80+
.map(
81+
key => `${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`,
82+
)
6483
.join('&');
6584

66-
if (keyValues.length === 0) return '';
67-
return `?${keyValues}`;
85+
return keyValues ? `?${keyValues}` : '';
6886
};

0 commit comments

Comments
 (0)