Skip to content

Commit

Permalink
feat: auto detect encoding for text preview (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wybxc authored Dec 9, 2024
1 parent 9254a9f commit e3eb093
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"artplayer-plugin-danmuku": "^5.0.1",
"asciinema-player": "^3.6.3",
"axios": "^1.0.0",
"chardet": "^2.0.0",
"clsx": "^2.0.0",
"copy-to-clipboard": "^3.3.2",
"crypto-js": "^4.2.0",
Expand Down
26 changes: 17 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/components/EncodingSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Box } from "@hope-ui/solid"
import { SelectWrapper } from "./Base"
import chardet from "chardet"
import { createEffect } from "solid-js"

export function EncodingSelect(props: {
encoding: string
setEncoding: (encoding: string) => void
referenceText?: string | ArrayBuffer
}) {
const encodingLabels = [
"utf-8",
Expand Down Expand Up @@ -48,6 +51,25 @@ export function EncodingSelect(props: {
"x-user-defined",
"iso-2022-cn",
]

createEffect(() => {
if (props.referenceText) {
let buffer: Uint8Array
if (typeof props.referenceText === "string") {
buffer = new TextEncoder().encode(props.referenceText)
} else {
buffer = new Uint8Array(props.referenceText)
}
for (let encoding of chardet.analyse(buffer)) {
const encodingLabel = encoding.name.toLowerCase()
if (encodingLabels.includes(encodingLabel)) {
props.setEncoding(encodingLabel)
return
}
}
}
})

return (
<Box
pos="absolute"
Expand Down
6 changes: 5 additions & 1 deletion src/components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,11 @@ export function Markdown(props: {
/>
</Show>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.children}
/>
</Show>
<MarkdownToc disabled={!props.toc} markdownRef={markdownRef()!} />
</Box>
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/previews/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function Html(props: { children?: string | ArrayBuffer }) {
srcdoc={text(encoding())}
/>
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.children}
/>
</Show>
</BoxWithFullScreen>
)
Expand Down
6 changes: 5 additions & 1 deletion src/pages/home/previews/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ function Editor(props: { data?: string | ArrayBuffer; contentType?: string }) {
return (
<VStack w="$full" alignItems="start" spacing="$2" pos="relative">
<Show when={!isString}>
<EncodingSelect encoding={encoding()} setEncoding={setEncoding} />
<EncodingSelect
encoding={encoding()}
setEncoding={setEncoding}
referenceText={props.data}
/>
</Show>
<MonacoEditorLoader
value={text(encoding())}
Expand Down

0 comments on commit e3eb093

Please sign in to comment.