Skip to content

Commit bfa775a

Browse files
diedu89ndom91
authored andcommitted
fix: use encoded framework values for url param and persistence logic
1 parent 2771dab commit bfa775a

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

docs/components/Code/index.tsx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ const getIndexFrameworkFromUrl = (
5151
const paramValue = params.get("framework")
5252
if (!paramValue) return null
5353

54-
const pascalCase = paramValue.replace(
55-
/(^|-)([a-z])/g,
56-
(_, _separator, letter) => letter.toUpperCase()
57-
)
54+
const decodedValue = decodeURI(paramValue)
5855

59-
const index = Object.keys(frameworks).findIndex((key) => key === pascalCase)
56+
const index = Object.values(frameworks).findIndex(
57+
(value) => value === decodedValue
58+
)
6059
return index === -1 ? null : index
6160
}
6261

@@ -75,26 +74,33 @@ const getIndexFrameworkFromStorage = (
7574
}
7675

7776
const updateFrameworkStorage = (
78-
frameworkKey: string,
77+
frameworkURI: string,
7978
frameworks: Record<string, string>,
8079
isAllFrameworks: boolean
8180
): void => {
82-
const index = Object.keys(frameworks).findIndex((key) => key === frameworkKey)
81+
const index = Object.values(frameworks).findIndex(
82+
(value) => encodeURI(value) === frameworkURI
83+
)
8384
if (index === -1) return
8485

8586
const storageKey = isAllFrameworks ? AUTHJS_TAB_KEY_ALL : AUTHJS_TAB_KEY
8687
window.localStorage.setItem(storageKey, index.toString())
8788

8889
// Update other storage if framework exists in other object
89-
const otherFrameworksKeys = Object.keys(
90+
const otherFrameworksValues = Object.values(
9091
isAllFrameworks ? baseFrameworks : allFrameworks
9192
)
9293
const otherStorageKey = isAllFrameworks ? AUTHJS_TAB_KEY : AUTHJS_TAB_KEY_ALL
9394

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+
)
95101
if (existsInOther) {
96-
const otherIndex = otherFrameworksKeys.findIndex(
97-
(key) => key === frameworkKey
102+
const otherIndex = otherFrameworksValues.findIndex(
103+
(encodedFramework) => encodedFramework === frameworkURI
98104
)
99105
window.localStorage.setItem(otherStorageKey, otherIndex.toString())
100106
// 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) {
120126

121127
const renderedFrameworks = withNextJsPages ? allFrameworks : baseFrameworks
122128

123-
const updateFrameworkInUrl = (frameworkKey: string): void => {
129+
const updateFrameworkInUrl = (frameworkURI: string): void => {
130+
if (frameworkURI === "undefined") return
131+
124132
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)
129134

130135
router.push(`${router.pathname}?${params.toString()}`, undefined, {
131136
scroll: false,
@@ -137,19 +142,17 @@ export function Code({ children }: ChildrenProps) {
137142
const { textContent } = event.target as unknown as HTMLDivElement
138143
if (!textContent) return
139144

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+
})
153156
}
154157

155158
useEffect(() => {
@@ -164,15 +167,15 @@ export function Code({ children }: ChildrenProps) {
164167

165168
if (indexFrameworkFromStorage === null) {
166169
updateFrameworkStorage(
167-
Object.keys(renderedFrameworks)[indexFrameworkFromUrl ?? 0],
170+
encodeURI(renderedFrameworks[indexFrameworkFromUrl ?? 0]),
168171
renderedFrameworks,
169172
withNextJsPages
170173
)
171174
}
172175

173176
if (!indexFrameworkFromUrl) {
174177
const index = indexFrameworkFromStorage ?? 0
175-
updateFrameworkInUrl(Object.keys(renderedFrameworks)[index])
178+
updateFrameworkInUrl(encodeURI(renderedFrameworks[index]))
176179
}
177180
}, [router.pathname, renderedFrameworks, withNextJsPages])
178181

0 commit comments

Comments
 (0)