Skip to content

Commit 1eb0155

Browse files
authored
docs: update decoding of shared examples
1 parent ae82344 commit 1eb0155

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

packages/website/src/components/Editor/encodeURL.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@ function encodeURL(str) {
2121

2222
function decodeURL(str) {
2323
// restore base64 format
24-
const base64 = str.replace(/-/g, "+").replace(/_/g, "/");
25-
const binary = atob(base64);
26-
const bytes = new Uint8Array(binary.length);
27-
28-
for (let i = 0; i < binary.length; i++) {
29-
bytes[i] = binary.charCodeAt(i);
24+
try {
25+
26+
const base64 = str.replace(/-/g, "+").replace(/_/g, "/");
27+
const binary = atob(base64);
28+
const bytes = new Uint8Array(binary.length);
29+
30+
for (let i = 0; i < binary.length; i++) {
31+
bytes[i] = binary.charCodeAt(i);
32+
}
33+
34+
return pako.inflate(bytes, { to: "string" });
35+
} catch (e) {
36+
return decodeBase64URL(str);
3037
}
31-
32-
return pako.inflate(bytes, { to: "string" });
38+
}
39+
40+
const decodeBase64URL = (str) => {
41+
const base64 = str.replaceAll('-', '+').replaceAll('_', '/');
42+
const binary = atob(base64);
43+
const bytes = Uint8Array.from(binary, (m) => m.codePointAt(0));
44+
return new TextDecoder().decode(bytes);
3345
}
3446

3547
// const testString = 'helljkh"ao⛳❤️🧀';

0 commit comments

Comments
 (0)