@@ -51,12 +51,11 @@ const getIndexFrameworkFromUrl = (
51
51
const paramValue = params . get ( "framework" )
52
52
if ( ! paramValue ) return null
53
53
54
- const pascalCase = paramValue . replace (
55
- / ( ^ | - ) ( [ a - z ] ) / g,
56
- ( _ , _separator , letter ) => letter . toUpperCase ( )
57
- )
54
+ const decodedValue = decodeURI ( paramValue )
58
55
59
- const index = Object . keys ( frameworks ) . findIndex ( ( key ) => key === pascalCase )
56
+ const index = Object . values ( frameworks ) . findIndex (
57
+ ( value ) => value === decodedValue
58
+ )
60
59
return index === - 1 ? null : index
61
60
}
62
61
@@ -75,26 +74,33 @@ const getIndexFrameworkFromStorage = (
75
74
}
76
75
77
76
const updateFrameworkStorage = (
78
- frameworkKey : string ,
77
+ frameworkURI : string ,
79
78
frameworks : Record < string , string > ,
80
79
isAllFrameworks : boolean
81
80
) : void => {
82
- const index = Object . keys ( frameworks ) . findIndex ( ( key ) => key === frameworkKey )
81
+ const index = Object . values ( frameworks ) . findIndex (
82
+ ( value ) => encodeURI ( value ) === frameworkURI
83
+ )
83
84
if ( index === - 1 ) return
84
85
85
86
const storageKey = isAllFrameworks ? AUTHJS_TAB_KEY_ALL : AUTHJS_TAB_KEY
86
87
window . localStorage . setItem ( storageKey , index . toString ( ) )
87
88
88
89
// Update other storage if framework exists in other object
89
- const otherFrameworksKeys = Object . keys (
90
+ const otherFrameworksValues = Object . values (
90
91
isAllFrameworks ? baseFrameworks : allFrameworks
91
92
)
92
93
const otherStorageKey = isAllFrameworks ? AUTHJS_TAB_KEY : AUTHJS_TAB_KEY_ALL
93
94
94
- const existsInOther = otherFrameworksKeys . includes ( frameworkKey )
95
+ const encodedFrameworksValues = otherFrameworksValues . map ( ( value ) =>
96
+ encodeURI ( value )
97
+ )
98
+ const existsInOther = encodedFrameworksValues . some (
99
+ ( encodedFramework ) => encodedFramework === frameworkURI
100
+ )
95
101
if ( existsInOther ) {
96
- const otherIndex = otherFrameworksKeys . findIndex (
97
- ( key ) => key === frameworkKey
102
+ const otherIndex = otherFrameworksValues . findIndex (
103
+ ( encodedFramework ) => encodedFramework === frameworkURI
98
104
)
99
105
window . localStorage . setItem ( otherStorageKey , otherIndex . toString ( ) )
100
106
// see https://github.com/shuding/nextra/blob/7ae958f02922e608151411042f658480b75164a6/packages/nextra/src/client/components/tabs/index.client.tsx#L106
@@ -120,12 +126,11 @@ export function Code({ children }: ChildrenProps) {
120
126
121
127
const renderedFrameworks = withNextJsPages ? allFrameworks : baseFrameworks
122
128
123
- const updateFrameworkInUrl = ( frameworkKey : string ) : void => {
129
+ const updateFrameworkInUrl = ( frameworkURI : string ) : void => {
130
+ if ( frameworkURI === "undefined" ) return
131
+
124
132
const params = new URLSearchParams ( searchParams ?. toString ( ) )
125
- const kebabCaseValue = frameworkKey
126
- . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, "$1-$2" )
127
- . toLowerCase ( )
128
- params . set ( "framework" , kebabCaseValue )
133
+ params . set ( "framework" , frameworkURI )
129
134
130
135
router . push ( `${ router . pathname } ?${ params . toString ( ) } ` , undefined , {
131
136
scroll : false ,
@@ -137,19 +142,17 @@ export function Code({ children }: ChildrenProps) {
137
142
const { textContent } = event . target as unknown as HTMLDivElement
138
143
if ( ! textContent ) return
139
144
140
- const frameworkKey = findFrameworkKey ( textContent , renderedFrameworks )
141
- if ( frameworkKey ) {
142
- updateFrameworkInUrl ( frameworkKey )
143
- updateFrameworkStorage ( frameworkKey , renderedFrameworks , withNextJsPages )
144
-
145
- // Focus and scroll to maintain position when code blocks above are expanded
146
- const element = event . target as HTMLButtonElement
147
- const rect = element . getBoundingClientRect ( )
148
- requestAnimationFrame ( ( ) => {
149
- element . focus ( )
150
- window . scrollBy ( 0 , element . getBoundingClientRect ( ) . top - rect . top )
151
- } )
152
- }
145
+ const frameworkURI = encodeURI ( textContent )
146
+ updateFrameworkInUrl ( frameworkURI )
147
+ updateFrameworkStorage ( frameworkURI , renderedFrameworks , withNextJsPages )
148
+
149
+ // Focus and scroll to maintain position when code blocks above are expanded
150
+ const element = event . target as HTMLButtonElement
151
+ const rect = element . getBoundingClientRect ( )
152
+ requestAnimationFrame ( ( ) => {
153
+ element . focus ( )
154
+ window . scrollBy ( 0 , element . getBoundingClientRect ( ) . top - rect . top )
155
+ } )
153
156
}
154
157
155
158
useEffect ( ( ) => {
@@ -164,15 +167,15 @@ export function Code({ children }: ChildrenProps) {
164
167
165
168
if ( indexFrameworkFromStorage === null ) {
166
169
updateFrameworkStorage (
167
- Object . keys ( renderedFrameworks ) [ indexFrameworkFromUrl ?? 0 ] ,
170
+ encodeURI ( renderedFrameworks [ indexFrameworkFromUrl ?? 0 ] ) ,
168
171
renderedFrameworks ,
169
172
withNextJsPages
170
173
)
171
174
}
172
175
173
176
if ( ! indexFrameworkFromUrl ) {
174
177
const index = indexFrameworkFromStorage ?? 0
175
- updateFrameworkInUrl ( Object . keys ( renderedFrameworks ) [ index ] )
178
+ updateFrameworkInUrl ( encodeURI ( renderedFrameworks [ index ] ) )
176
179
}
177
180
} , [ router . pathname , renderedFrameworks , withNextJsPages ] )
178
181
0 commit comments