Skip to content

Commit 825a5aa

Browse files
authored
fix: fix javascript btoa function can not handle Unicode strings issue (#38)
1 parent e4b261a commit 825a5aa

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/app/search/components/codePreviewPanel/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import { fetchFileSource } from "@/app/api/(client)/client";
4-
import { getCodeHostFilePreviewLink } from "@/lib/utils";
4+
import { getCodeHostFilePreviewLink, base64Decode } from "@/lib/utils";
55
import { useQuery } from "@tanstack/react-query";
66
import { CodePreview, CodePreviewFile } from "./codePreview";
77
import { SearchResultFile } from "@/lib/types";
@@ -32,7 +32,7 @@ export const CodePreviewPanel = ({
3232
// @todo : refector this to use the templates provided by zoekt.
3333
const link = getCodeHostFilePreviewLink(fileMatch.Repository, fileMatch.FileName)
3434

35-
const decodedSource = atob(source);
35+
const decodedSource = base64Decode(source);
3636

3737
// Filter out filename matches
3838
const filteredMatches = fileMatch.ChunkMatches.filter((match) => {

src/app/search/components/searchResultsPanel/fileMatch.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useMemo } from "react";
44
import { CodePreview } from "./codePreview";
55
import { SearchResultFile, SearchResultFileMatch } from "@/lib/types";
6+
import { base64Decode } from "@/lib/utils";
67

78

89
interface FileMatchProps {
@@ -17,7 +18,7 @@ export const FileMatch = ({
1718
onOpen,
1819
}: FileMatchProps) => {
1920
const content = useMemo(() => {
20-
return atob(match.Content);
21+
return base64Decode(match.Content);
2122
}, [match.Content]);
2223

2324
// If it's just the title, don't show a code preview

src/lib/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@ export const getEnvBoolean = (env: string | undefined, defaultValue: boolean) =>
9595
return defaultValue;
9696
}
9797
return env === 'true' || env === '1';
98-
}
98+
}
99+
100+
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
101+
export const base64Decode = (base64: string): string => {
102+
const binString = atob(base64);
103+
return Buffer.from(Uint8Array.from(binString, (m) => m.codePointAt(0)!).buffer).toString();
104+
}

0 commit comments

Comments
 (0)