File tree 4 files changed +287
-262
lines changed
4 files changed +287
-262
lines changed Original file line number Diff line number Diff line change
1
+ import type { ParamValueOneOrMore } from "vue-router" ;
2
+
1
3
export function filepathToRkey ( filepath : string ) {
2
4
if ( filepath === '' ) throw new Error ( 'File path is empty!' ) ;
3
5
6
+ if ( filepath . includes ( ':' ) ) throw new Error ( '`:` character not allowed in file path!' ) ;
7
+
4
8
filepath = filepath . replace ( / \\ / g, '/' ) ;
5
9
6
10
if ( filepath . startsWith ( './' ) ) {
@@ -37,6 +41,26 @@ export function rkeyToFilepath(rkey: string) {
37
41
return rkey
38
42
. replace ( / : / g, '/' )
39
43
. replace ( / _ ( [ 0 - 9 a - z ] { 1 , 4 } ) _ / g, $$ => {
40
- return String . fromCharCode ( parseInt ( $$ . slice ( 1 ) , 36 ) ) ;
44
+ return String . fromCharCode ( parseInt ( $$ . slice ( 1 , - 1 ) , 36 ) ) ;
41
45
} ) ;
42
46
}
47
+
48
+ export function filepathToPathParameter ( filepath : string ) : ParamValueOneOrMore < true > {
49
+ if ( filepath === '' ) throw new Error ( 'File path is empty!' ) ;
50
+
51
+ filepath = filepath . replace ( / \\ / g, '/' ) ;
52
+
53
+ if ( filepath . startsWith ( './' ) ) {
54
+ filepath = filepath . slice ( 2 ) ;
55
+ }
56
+
57
+ if ( filepath . startsWith ( '/' ) ) {
58
+ filepath = filepath . slice ( 1 ) ;
59
+ }
60
+
61
+ if ( filepath . includes ( '../' ) || filepath . includes ( '/..' ) ) {
62
+ throw new Error ( 'Backwards directory navigation not supported in rkey' ) ;
63
+ }
64
+
65
+ return filepath . split ( '/' ) as ParamValueOneOrMore < true > ;
66
+ }
You can’t perform that action at this time.
0 commit comments