diff --git a/package.json b/package.json index 356429e..294c460 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@types/jest": "29.5.2", "@types/node": "20.3.3", "@types/nunjucks": "3.2.3", + "@types/which": "^3.0.0", "@typescript-eslint/eslint-plugin": "5.61.0", "@typescript-eslint/parser": "5.61.0", "babel-jest": "29.5.0", @@ -55,6 +56,8 @@ "react": "npm:@preact/compat", "react-dom": "npm:@preact/compat", "react-json-tree": "0.18.0", - "react-select": "5.7.3" + "react-select": "5.7.3", + "shell-path": "^3.0.0", + "which": "^3.0.1" } } diff --git a/src/globals.d.ts b/src/globals.d.ts index 062d226..98038a1 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -1 +1,2 @@ declare module 'escape-path-with-spaces'; +declare module 'shell-path'; diff --git a/src/main.ts b/src/main.ts index 00c662d..6672ed1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ import { EditableFileView, Events, Plugin, TFile } from 'obsidian'; +import { shellPath } from 'shell-path'; import { DataExplorerView, viewType } from './DataExplorerView'; import { LoadingModal } from './bbt/LoadingModal'; @@ -32,6 +33,27 @@ const DEFAULT_SETTINGS: ZoteroConnectorSettings = { whichNotesToOpenAfterImport: 'first-imported-note', }; +async function fixPath() { + if (process.platform === 'win32') { + return; + } + + try { + const path = await shellPath(); + + process.env.PATH = + path || + [ + './node_modules/.bin', + '/.nodebrew/current/bin', + '/usr/local/bin', + process.env.PATH, + ].join(':'); + } catch (e) { + console.error(e); + } +} + export default class ZoteroConnector extends Plugin { settings: ZoteroConnectorSettings; emitter: Events; @@ -99,6 +121,8 @@ export default class ZoteroConnector extends Plugin { }); this.registerEditorSuggest(new CiteSuggest(this.app, this)); + + fixPath(); } onunload() { diff --git a/src/settings/settings.tsx b/src/settings/settings.tsx index 5b54271..7be6d84 100644 --- a/src/settings/settings.tsx +++ b/src/settings/settings.tsx @@ -1,6 +1,7 @@ -import { App, Platform, PluginSettingTab, debounce } from 'obsidian'; +import { App, Notice, Platform, PluginSettingTab, debounce } from 'obsidian'; import React from 'react'; import ReactDOM from 'react-dom'; +import which from 'which'; import ZoteroConnector from '../main'; import { @@ -11,6 +12,7 @@ import { import { AssetDownloader } from './AssetDownloader'; import { CiteFormatSettings } from './CiteFormatSettings'; import { ExportFormatSettings } from './ExportFormatSettings'; +import { Icon } from './Icon'; import { SettingItem } from './SettingItem'; interface SettingsComponentProps { @@ -108,6 +110,9 @@ function SettingsComponent({ [removeExportFormat] ); + const tessPathRef = React.useRef(null); + const tessDataPathRef = React.useRef(null); + return (
@@ -359,6 +364,7 @@ function SettingsComponent({ } > updateSetting( 'pdfExportImageTesseractPath', @@ -368,6 +374,30 @@ function SettingsComponent({ type="text" defaultValue={settings.pdfExportImageTesseractPath} /> +
{ + try { + const pathToTesseract = await which('tesseract'); + if (pathToTesseract) { + tessPathRef.current.value = pathToTesseract; + updateSetting('pdfExportImageTesseractPath', pathToTesseract); + } else { + new Notice( + 'Unable to find tesseract on your system. If it is installed, please manually enter a path.' + ); + } + } catch (e) { + new Notice( + 'Unable to find tesseract on your system. If it is installed, please manually enter a path.' + ); + console.error(e); + } + }} + > + +
updateSetting( 'pdfExportImageTessDataDir', @@ -420,6 +451,22 @@ function SettingsComponent({ type="text" defaultValue={settings.pdfExportImageTessDataDir} /> +
{ + const path = require('electron').remote.dialog.showOpenDialogSync({ + properties: ['openDirectory'], + }); + + if (path && path.length) { + tessDataPathRef.current.value = path[0]; + updateSetting('pdfExportImageTessDataDir', path[0]); + } + }} + > + +
); diff --git a/yarn.lock b/yarn.lock index 09934e8..4741310 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1949,6 +1949,11 @@ resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.2.tgz#6286b4c7228d58ab7866d19716f3696e03a09397" integrity sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw== +"@types/which@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/which/-/which-3.0.0.tgz#849afdd9fdcb0b67339b9cfc80fa6ea4e0253fc5" + integrity sha512-ASCxdbsrwNfSMXALlC3Decif9rwDMu+80KGp5zI2RLRotfMsTv7fHL8W8VDp24wymzDyIFudhUeSCugrgRFfHQ== + "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" @@ -2128,6 +2133,11 @@ ansi-regex@^5.0.1: resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304" integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ== +ansi-regex@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a" + integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA== + ansi-styles@^3.2.0, ansi-styles@^3.2.1: version "3.2.1" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" @@ -3041,6 +3051,11 @@ deepmerge@^4.2.2: resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.1.tgz#44b5f2147cd3b00d4b56137685966f26fd25dd4a" integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A== +default-shell@^2.0.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/default-shell/-/default-shell-2.2.0.tgz#31481c19747bfe59319b486591643eaf115a1864" + integrity sha512-sPpMZcVhRQ0nEMDtuMJ+RtCxt7iHPAMBU+I4tAlo5dU1sjRpNax0crj6nR3qKpvVnckaQ9U38enXcwW9nZJeCw== + defer-to-connect@^1.0.1: version "1.1.3" resolved "https://registry.yarnpkg.com/defer-to-connect/-/defer-to-connect-1.1.3.tgz#331ae050c08dcf789f8c83a7b81f0ed94f4ac591" @@ -3566,7 +3581,7 @@ execa@^1.0.0: signal-exit "^3.0.0" strip-eof "^1.0.0" -execa@^5.0.0: +execa@^5.0.0, execa@^5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd" integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg== @@ -6742,6 +6757,22 @@ shebang-regex@^3.0.0: resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172" integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A== +shell-env@^4.0.0: + version "4.0.1" + resolved "https://registry.yarnpkg.com/shell-env/-/shell-env-4.0.1.tgz#883302d9426095d398a39b102a851adb306b8cb8" + integrity sha512-w3oeZ9qg/P6Lu6qqwavvMnB/bwfsz67gPB3WXmLd/n6zuh7TWQZtGa3iMEdmua0kj8rivkwl+vUjgLWlqZOMPw== + dependencies: + default-shell "^2.0.0" + execa "^5.1.1" + strip-ansi "^7.0.1" + +shell-path@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/shell-path/-/shell-path-3.0.0.tgz#5c95bc68aade43c06082a0655cb5c97586e4feb0" + integrity sha512-HNIZ+W/3P0JuVTV03xjGqYKt3e3h0/Z4AH8TQWeth1LBtCusSjICgkdNdb3VZr6mI7ijE2AiFFpgkVMNKsALeQ== + dependencies: + shell-env "^4.0.0" + side-channel@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf" @@ -7031,6 +7062,13 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1: dependencies: ansi-regex "^5.0.1" +strip-ansi@^7.0.1: + version "7.1.0" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" + integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ== + dependencies: + ansi-regex "^6.0.1" + strip-bom@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878" @@ -7505,6 +7543,13 @@ which@^2.0.1, which@^2.0.2: dependencies: isexe "^2.0.0" +which@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/which/-/which-3.0.1.tgz#89f1cd0c23f629a8105ffe69b8172791c87b4be1" + integrity sha512-XA1b62dzQzLfaEOSQFTCOd5KFf/1VSzZo7/7TUjnya6u0vGGKzU96UQBZTAThCb2j4/xjBAyii1OhRLJEivHvg== + dependencies: + isexe "^2.0.0" + widest-line@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/widest-line/-/widest-line-3.1.0.tgz#8292333bbf66cb45ff0de1603b136b7ae1496eca"