Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 05dfb5e

Browse files
committedOct 14, 2021
sanitize uri slash
1 parent 87d654b commit 05dfb5e

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed
 

‎src/components/Link.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,39 @@ const addTrailingSlash = (uri: string) => {
88
return uri
99
}
1010

11-
if (uri.indexOf('#') > 0) {
12-
const hash = uri.substring(uri.indexOf('#'), uri.length)
13-
uri = addSlash(uri.replace(hash, ''))
14-
return uri + hash
11+
const removeSlash = (uri: string) => {
12+
return uri.replace(/\/$/, '')
1513
}
14+
15+
const getHash = (uri: string) => {
16+
if (uri.indexOf('#') > 0) {
17+
return uri.substring(uri.indexOf('#'), uri.length)
18+
}
19+
return ''
20+
}
21+
22+
const getSearch = (uri: string) => {
23+
if (uri.indexOf('?') > 0) {
24+
return uri.substring(uri.indexOf('?'), uri.length)
25+
}
26+
return ''
27+
}
28+
29+
// eg: http://localhost:8001/playground/?deploy=node-template#config
30+
// remove back slash if exist
31+
uri = removeSlash(uri)
32+
// store hash if exist and remove from uri
33+
const hash = getHash(uri)
34+
if (hash) uri = uri.replace(hash, '')
35+
// remove back slash if exist
36+
uri = removeSlash(uri)
37+
// store search query if exist and remove from uri
38+
const search = getSearch(uri)
39+
if (search) uri = uri.replace(search, '')
40+
// add slash if missing
1641
uri = addSlash(uri)
17-
return uri
42+
43+
return uri + search + hash
1844
}
1945

2046
interface InfraLinkProps {

0 commit comments

Comments
 (0)
Please sign in to comment.