Skip to content

Commit

Permalink
Add encoding detection when importing word list files
Browse files Browse the repository at this point in the history
  • Loading branch information
tth05 committed Nov 29, 2022
1 parent 5c7369d commit c00a4a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
20 changes: 18 additions & 2 deletions package-lock.json

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

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"builtin-modules": "^3.3.0",
"@codemirror/state": "^6.1.2",
"esbuild": "^0.15.11",
"obsidian": "^0.16.3",
"tslib": "2.4.0",
"typescript": "4.8.4"
}
"tslib": "2.4.0",
"typescript": "4.8.4",
"@codemirror/state": "^6.1.2",
"obsidian": "^0.16.3",
"jschardet": "^3.0.0"
}
}
22 changes: 16 additions & 6 deletions src/settings_tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import CompletrPlugin from "./main";
import {FileScanner} from "./provider/scanner_provider";
import {WordList} from "./provider/word_list_provider";
import {CompletrSettings, WordInsertionMode} from "./settings";
import {TextDecoder} from "util";
import {detect} from "jschardet";

export default class CompletrSettingsTab extends PluginSettingTab {

Expand Down Expand Up @@ -232,15 +234,23 @@ export default class CompletrSettingsTab extends PluginSettingTab {
let changed = false;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const text = await file.text();
const success = await WordList.importWordList(this.app.vault, file.name, text);
changed ||= success;

if (!success)
new Notice("Unable to import " + file.name + " because it already exists!");
try {
const buf = await file.arrayBuffer();
const encoding = detect(Buffer.from(buf.slice(0, 1024))).encoding;
const text = new TextDecoder(encoding).decode(buf);
const success = await WordList.importWordList(this.app.vault, file.name, text);
changed ||= success;

if (!success)
new Notice("Unable to import " + file.name + " because it already exists!");
} catch (e) {
console.error(e);
new Notice("Error while importing " + file.name);
}
}

//Only refresh if something was added
// Only refresh if something was added
if (!changed)
return;

Expand Down

0 comments on commit c00a4a6

Please sign in to comment.