ocr-extractor-api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Johnathan Ritzi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # ocr-extractor-api
2
+
3
+ This package provides types and helpers for the public API of the
4
+ [OCR Extractor](https://community.obsidian.md/plugins/ocr-extractor) Obsidian
5
+ plugin. The API allows another plugin or script to run the user's configured OCR
6
+ engine on an attachment and get the extracted text.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install ocr-extractor-api
12
+ ```
13
+
14
+ ## Quick start
15
+
16
+ ```ts
17
+ import { getOcrExtractorApi, isOcrError } from "ocr-extractor-api";
18
+
19
+ const api = getOcrExtractorApi(app);
20
+ if (!api) {
21
+ // OCR Extractor isn't installed or enabled
22
+ return;
23
+ }
24
+
25
+ try {
26
+ const { text } = await api.extractText(file); // `file` is the attachment TFile
27
+ // `text` is the extracted text (or `""` if none was found)
28
+ } catch (error) {
29
+ // Use `isOcrError`, not `instanceof`
30
+ if (isOcrError(error) && error.code === "unsupported-file") {
31
+ // The configured engine can't process this file type
32
+ return;
33
+ }
34
+ throw error; // an unexpected error
35
+ }
36
+ ```
37
+
38
+ ## Documentation
39
+
40
+ The full API documentation is available in the [API reference](./docs).
41
+
42
+ ## Versioning
43
+
44
+ This package is versioned independently of the plugin, and its version is the API
45
+ version. See the [changelog](./CHANGELOG.md) for changes.
46
+
47
+ ## License
48
+
49
+ `ocr-extractor-api` is licensed under the [MIT License](./LICENSE).
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Identifies which kind of {@link OcrError} occurred.
3
+ *
4
+ * - `"unsupported-file"`: the OCR engine can't process this file (e.g. an
5
+ * unsupported file type)
6
+ * - `"extraction-failed"`: any other failure during extraction (network,
7
+ * auth, etc.)
8
+ *
9
+ * @since 1.0.0
10
+ */
11
+ export type OcrErrorCode = "unsupported-file" | "extraction-failed";
12
+ /**
13
+ * The error thrown by {@link OcrExtractorApi.extractText} when extraction
14
+ * fails. Identify it with {@link isOcrError} (not `instanceof`, which won't
15
+ * work across the plugin and npm package boundary). The original error,
16
+ * if any, is included as `cause`.
17
+ *
18
+ * @since 1.0.0
19
+ */
20
+ export interface OcrError extends Error {
21
+ readonly code: OcrErrorCode;
22
+ readonly cause?: unknown;
23
+ }
24
+ /**
25
+ * Checks whether a caught value is an {@link OcrError}.
26
+ *
27
+ * @param error - The caught error to check
28
+ * @returns `true` if `error` is an {@link OcrError}, `false` otherwise
29
+ * @since 1.0.0
30
+ * @example
31
+ * ```ts
32
+ * try {
33
+ * await api.extractText(file);
34
+ * } catch (error) {
35
+ * if (isOcrError(error) && error.code === "unsupported-file") return;
36
+ * throw error;
37
+ * }
38
+ * ```
39
+ */
40
+ export declare function isOcrError(error: unknown): error is OcrError;
41
+ /**
42
+ * Used by the plugin to construct an {@link OcrError} with the given code and
43
+ * message.
44
+ *
45
+ * @param code - Which kind of error this is
46
+ * @param message - A human-readable message
47
+ * @param options - Optional settings
48
+ * @param options.cause - The underlying error, if any
49
+ * @returns The new {@link OcrError}
50
+ * @since 1.0.0
51
+ */
52
+ export declare function ocrError(code: OcrErrorCode, message?: string, options?: {
53
+ cause?: unknown;
54
+ }): OcrError;
55
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG,kBAAkB,GAAG,mBAAmB,CAAC;AAEpE;;;;;;;GAOG;AACH,MAAM,WAAW,QAAS,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAM5D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,GAC5B,QAAQ,CAGV"}
package/dist/errors.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Checks whether a caught value is an {@link OcrError}.
3
+ *
4
+ * @param error - The caught error to check
5
+ * @returns `true` if `error` is an {@link OcrError}, `false` otherwise
6
+ * @since 1.0.0
7
+ * @example
8
+ * ```ts
9
+ * try {
10
+ * await api.extractText(file);
11
+ * } catch (error) {
12
+ * if (isOcrError(error) && error.code === "unsupported-file") return;
13
+ * throw error;
14
+ * }
15
+ * ```
16
+ */
17
+ export function isOcrError(error) {
18
+ return (error instanceof Error &&
19
+ error.name === "OcrError" &&
20
+ typeof error.code === "string");
21
+ }
22
+ /**
23
+ * Used by the plugin to construct an {@link OcrError} with the given code and
24
+ * message.
25
+ *
26
+ * @param code - Which kind of error this is
27
+ * @param message - A human-readable message
28
+ * @param options - Optional settings
29
+ * @param options.cause - The underlying error, if any
30
+ * @returns The new {@link OcrError}
31
+ * @since 1.0.0
32
+ */
33
+ export function ocrError(code, message, options) {
34
+ const error = new Error(message ?? code, options);
35
+ return Object.assign(error, { name: "OcrError", code });
36
+ }
37
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAyBA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,KAAK,CAAC,IAAI,KAAK,UAAU;QACzB,OAAQ,KAA2B,CAAC,IAAI,KAAK,QAAQ,CACtD,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,QAAQ,CACtB,IAAkB,EAClB,OAAgB,EAChB,OAA6B;IAE7B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,IAAI,IAAI,EAAE,OAAO,CAAC,CAAC;IAClD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Public API for the OCR Extractor Obsidian plugin
3
+ *
4
+ * Access it with {@link getOcrExtractorApi}.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ import type { App, TFile } from "obsidian";
9
+ export { type OcrError, type OcrErrorCode, isOcrError, ocrError, } from "./errors.js";
10
+ /**
11
+ * The API version
12
+ *
13
+ * @since 1.0.0
14
+ */
15
+ export declare const OCR_EXTRACTOR_API_VERSION = "1.0.0";
16
+ /**
17
+ * The result of a successful extraction
18
+ *
19
+ * @since 1.0.0
20
+ */
21
+ export interface OcrExtractionResult {
22
+ /** Extracted text (or `""` if the file was processed but contained no text) */
23
+ text: string;
24
+ }
25
+ /**
26
+ * The OCR Extractor plugin's public API, obtained via {@link getOcrExtractorApi}
27
+ *
28
+ * @since 1.0.0
29
+ */
30
+ export interface OcrExtractorApi {
31
+ /**
32
+ * The API version (i.e. {@link OCR_EXTRACTOR_API_VERSION})
33
+ *
34
+ * @since 1.0.0
35
+ */
36
+ readonly version: string;
37
+ /**
38
+ * Run the user's configured OCR engine on an attachment and return the
39
+ * extracted text. Does not modify any note.
40
+ *
41
+ * @param file - The attachment's `TFile`
42
+ * @param options - Optional settings
43
+ * @param options.signal - An optional `AbortSignal` to cancel the extraction
44
+ * @returns An {@link OcrExtractionResult} with the extracted `text` (`""` if none found)
45
+ * @throws An {@link OcrError} with an error `code` ({@link OcrErrorCode}), or
46
+ * an `AbortError` if canceled via `options.signal`
47
+ * @since 1.0.0
48
+ * @example
49
+ * ```ts
50
+ * const { text } = await api.extractText(file);
51
+ * ```
52
+ */
53
+ extractText(file: TFile, options?: {
54
+ signal?: AbortSignal;
55
+ }): Promise<OcrExtractionResult>;
56
+ }
57
+ /**
58
+ * Get the OCR Extractor API from the Obsidian `app`.
59
+ *
60
+ * @param app - The Obsidian app instance
61
+ * @returns The API, or `undefined` if the plugin isn't installed or enabled
62
+ * @since 1.0.0
63
+ * @example
64
+ * ```ts
65
+ * const api = getOcrExtractorApi(app);
66
+ * if (!api) return;
67
+ * ```
68
+ */
69
+ export declare function getOcrExtractorApi(app: App): OcrExtractorApi | undefined;
70
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAE3C,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,UAAU,EACV,QAAQ,GACT,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,UAAU,CAAC;AAEjD;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,+EAA+E;IAC/E,IAAI,EAAE,MAAM,CAAC;CACd;AAOD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CACT,IAAI,EAAE,KAAK,EACX,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GACjC,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACjC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,eAAe,GAAG,SAAS,CAGxE"}
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Public API for the OCR Extractor Obsidian plugin
3
+ *
4
+ * Access it with {@link getOcrExtractorApi}.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export { isOcrError, ocrError, } from "./errors.js";
9
+ /**
10
+ * The API version
11
+ *
12
+ * @since 1.0.0
13
+ */
14
+ export const OCR_EXTRACTOR_API_VERSION = "1.0.0";
15
+ /**
16
+ * Get the OCR Extractor API from the Obsidian `app`.
17
+ *
18
+ * @param app - The Obsidian app instance
19
+ * @returns The API, or `undefined` if the plugin isn't installed or enabled
20
+ * @since 1.0.0
21
+ * @example
22
+ * ```ts
23
+ * const api = getOcrExtractorApi(app);
24
+ * if (!api) return;
25
+ * ```
26
+ */
27
+ export function getOcrExtractorApi(app) {
28
+ const plugins = app.plugins?.plugins;
29
+ return plugins?.["ocr-extractor"]?.api;
30
+ }
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAGL,UAAU,EACV,QAAQ,GACT,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,OAAO,CAAC;AAoDjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAQ;IACzC,MAAM,OAAO,GAAI,GAAsB,CAAC,OAAO,EAAE,OAAO,CAAC;IACzD,OAAO,OAAO,EAAE,CAAC,eAAe,CAAC,EAAE,GAAG,CAAC;AACzC,CAAC"}
package/docs/README.md ADDED
@@ -0,0 +1,29 @@
1
+ **ocr-extractor-api**
2
+
3
+ ***
4
+
5
+ # ocr-extractor-api
6
+
7
+ Public API for the OCR Extractor Obsidian plugin
8
+
9
+ Access it with [getOcrExtractorApi](functions/getOcrExtractorApi.md).
10
+
11
+ ## Interfaces
12
+
13
+ - [OcrError](interfaces/OcrError.md)
14
+ - [OcrExtractionResult](interfaces/OcrExtractionResult.md)
15
+ - [OcrExtractorApi](interfaces/OcrExtractorApi.md)
16
+
17
+ ## Type Aliases
18
+
19
+ - [OcrErrorCode](type-aliases/OcrErrorCode.md)
20
+
21
+ ## Variables
22
+
23
+ - [OCR\_EXTRACTOR\_API\_VERSION](variables/OCR_EXTRACTOR_API_VERSION.md)
24
+
25
+ ## Functions
26
+
27
+ - [getOcrExtractorApi](functions/getOcrExtractorApi.md)
28
+ - [isOcrError](functions/isOcrError.md)
29
+ - [ocrError](functions/ocrError.md)
@@ -0,0 +1,38 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / getOcrExtractorApi
6
+
7
+ # Function: getOcrExtractorApi()
8
+
9
+ > **getOcrExtractorApi**(`app`): [`OcrExtractorApi`](../interfaces/OcrExtractorApi.md) \| `undefined`
10
+
11
+ Defined in: [index.ts:87](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L87)
12
+
13
+ Get the OCR Extractor API from the Obsidian `app`.
14
+
15
+ ## Parameters
16
+
17
+ ### app
18
+
19
+ `App`
20
+
21
+ The Obsidian app instance
22
+
23
+ ## Returns
24
+
25
+ [`OcrExtractorApi`](../interfaces/OcrExtractorApi.md) \| `undefined`
26
+
27
+ The API, or `undefined` if the plugin isn't installed or enabled
28
+
29
+ ## Since
30
+
31
+ 1.0.0
32
+
33
+ ## Example
34
+
35
+ ```ts
36
+ const api = getOcrExtractorApi(app);
37
+ if (!api) return;
38
+ ```
@@ -0,0 +1,42 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / isOcrError
6
+
7
+ # Function: isOcrError()
8
+
9
+ > **isOcrError**(`error`): `error is OcrError`
10
+
11
+ Defined in: [errors.ts:42](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L42)
12
+
13
+ Checks whether a caught value is an [OcrError](../interfaces/OcrError.md).
14
+
15
+ ## Parameters
16
+
17
+ ### error
18
+
19
+ `unknown`
20
+
21
+ The caught error to check
22
+
23
+ ## Returns
24
+
25
+ `error is OcrError`
26
+
27
+ `true` if `error` is an [OcrError](../interfaces/OcrError.md), `false` otherwise
28
+
29
+ ## Since
30
+
31
+ 1.0.0
32
+
33
+ ## Example
34
+
35
+ ```ts
36
+ try {
37
+ await api.extractText(file);
38
+ } catch (error) {
39
+ if (isOcrError(error) && error.code === "unsupported-file") return;
40
+ throw error;
41
+ }
42
+ ```
@@ -0,0 +1,48 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / ocrError
6
+
7
+ # Function: ocrError()
8
+
9
+ > **ocrError**(`code`, `message?`, `options?`): [`OcrError`](../interfaces/OcrError.md)
10
+
11
+ Defined in: [errors.ts:61](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L61)
12
+
13
+ Used by the plugin to construct an [OcrError](../interfaces/OcrError.md) with the given code and
14
+ message.
15
+
16
+ ## Parameters
17
+
18
+ ### code
19
+
20
+ [`OcrErrorCode`](../type-aliases/OcrErrorCode.md)
21
+
22
+ Which kind of error this is
23
+
24
+ ### message?
25
+
26
+ `string`
27
+
28
+ A human-readable message
29
+
30
+ ### options?
31
+
32
+ Optional settings
33
+
34
+ #### cause?
35
+
36
+ `unknown`
37
+
38
+ The underlying error, if any
39
+
40
+ ## Returns
41
+
42
+ [`OcrError`](../interfaces/OcrError.md)
43
+
44
+ The new [OcrError](../interfaces/OcrError.md)
45
+
46
+ ## Since
47
+
48
+ 1.0.0
@@ -0,0 +1,42 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / OcrError
6
+
7
+ # Interface: OcrError
8
+
9
+ Defined in: [errors.ts:21](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L21)
10
+
11
+ The error thrown by [OcrExtractorApi.extractText](OcrExtractorApi.md#extracttext) when extraction
12
+ fails. Identify it with [isOcrError](../functions/isOcrError.md) (not `instanceof`, which won't
13
+ work across the plugin and npm package boundary). The original error,
14
+ if any, is included as `cause`.
15
+
16
+ ## Since
17
+
18
+ 1.0.0
19
+
20
+ ## Extends
21
+
22
+ - `Error`
23
+
24
+ ## Properties
25
+
26
+ ### cause?
27
+
28
+ > `readonly` `optional` **cause?**: `unknown`
29
+
30
+ Defined in: [errors.ts:23](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L23)
31
+
32
+ #### Overrides
33
+
34
+ `Error.cause`
35
+
36
+ ***
37
+
38
+ ### code
39
+
40
+ > `readonly` **code**: [`OcrErrorCode`](../type-aliases/OcrErrorCode.md)
41
+
42
+ Defined in: [errors.ts:22](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L22)
@@ -0,0 +1,25 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / OcrExtractionResult
6
+
7
+ # Interface: OcrExtractionResult
8
+
9
+ Defined in: [index.ts:30](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L30)
10
+
11
+ The result of a successful extraction
12
+
13
+ ## Since
14
+
15
+ 1.0.0
16
+
17
+ ## Properties
18
+
19
+ ### text
20
+
21
+ > **text**: `string`
22
+
23
+ Defined in: [index.ts:32](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L32)
24
+
25
+ Extracted text (or `""` if the file was processed but contained no text)
@@ -0,0 +1,79 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / OcrExtractorApi
6
+
7
+ # Interface: OcrExtractorApi
8
+
9
+ Defined in: [index.ts:45](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L45)
10
+
11
+ The OCR Extractor plugin's public API, obtained via [getOcrExtractorApi](../functions/getOcrExtractorApi.md)
12
+
13
+ ## Since
14
+
15
+ 1.0.0
16
+
17
+ ## Properties
18
+
19
+ ### version
20
+
21
+ > `readonly` **version**: `string`
22
+
23
+ Defined in: [index.ts:51](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L51)
24
+
25
+ The API version (i.e. [OCR\_EXTRACTOR\_API\_VERSION](../variables/OCR_EXTRACTOR_API_VERSION.md))
26
+
27
+ #### Since
28
+
29
+ 1.0.0
30
+
31
+ ## Methods
32
+
33
+ ### extractText()
34
+
35
+ > **extractText**(`file`, `options?`): `Promise`\<[`OcrExtractionResult`](OcrExtractionResult.md)\>
36
+
37
+ Defined in: [index.ts:69](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L69)
38
+
39
+ Run the user's configured OCR engine on an attachment and return the
40
+ extracted text. Does not modify any note.
41
+
42
+ #### Parameters
43
+
44
+ ##### file
45
+
46
+ `TFile`
47
+
48
+ The attachment's `TFile`
49
+
50
+ ##### options?
51
+
52
+ Optional settings
53
+
54
+ ###### signal?
55
+
56
+ `AbortSignal`
57
+
58
+ An optional `AbortSignal` to cancel the extraction
59
+
60
+ #### Returns
61
+
62
+ `Promise`\<[`OcrExtractionResult`](OcrExtractionResult.md)\>
63
+
64
+ An [OcrExtractionResult](OcrExtractionResult.md) with the extracted `text` (`""` if none found)
65
+
66
+ #### Throws
67
+
68
+ An [OcrError](OcrError.md) with an error `code` ([OcrErrorCode](../type-aliases/OcrErrorCode.md)), or
69
+ an `AbortError` if canceled via `options.signal`
70
+
71
+ #### Since
72
+
73
+ 1.0.0
74
+
75
+ #### Example
76
+
77
+ ```ts
78
+ const { text } = await api.extractText(file);
79
+ ```
@@ -0,0 +1,22 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / OcrErrorCode
6
+
7
+ # Type Alias: OcrErrorCode
8
+
9
+ > **OcrErrorCode** = `"unsupported-file"` \| `"extraction-failed"`
10
+
11
+ Defined in: [errors.ts:11](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/errors.ts#L11)
12
+
13
+ Identifies which kind of [OcrError](../interfaces/OcrError.md) occurred.
14
+
15
+ - `"unsupported-file"`: the OCR engine can't process this file (e.g. an
16
+ unsupported file type)
17
+ - `"extraction-failed"`: any other failure during extraction (network,
18
+ auth, etc.)
19
+
20
+ ## Since
21
+
22
+ 1.0.0
@@ -0,0 +1,17 @@
1
+ [**ocr-extractor-api**](../README.md)
2
+
3
+ ***
4
+
5
+ [ocr-extractor-api](../README.md) / OCR\_EXTRACTOR\_API\_VERSION
6
+
7
+ # Variable: OCR\_EXTRACTOR\_API\_VERSION
8
+
9
+ > `const` **OCR\_EXTRACTOR\_API\_VERSION**: `"1.0.0"` = `"1.0.0"`
10
+
11
+ Defined in: [index.ts:23](https://github.com/jritzi/ocr-extractor/blob/e7f98a2c103b20be831bfca50d478c0c3b12ddec/ocr-extractor-api/src/index.ts#L23)
12
+
13
+ The API version
14
+
15
+ ## Since
16
+
17
+ 1.0.0
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "ocr-extractor-api",
3
+ "version": "1.0.0",
4
+ "description": "Types and helpers for the OCR Extractor Obsidian plugin's public API",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Johnathan Ritzi",
8
+ "homepage": "https://community.obsidian.md/plugins/ocr-extractor",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/jritzi/ocr-extractor.git",
12
+ "directory": "ocr-extractor-api"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/jritzi/ocr-extractor/issues"
16
+ },
17
+ "keywords": [
18
+ "ocr-extractor",
19
+ "obsidian",
20
+ "types",
21
+ "ocr"
22
+ ],
23
+ "main": "dist/index.js",
24
+ "types": "dist/index.d.ts",
25
+ "files": [
26
+ "dist",
27
+ "src",
28
+ "docs",
29
+ "README.md",
30
+ "LICENSE"
31
+ ],
32
+ "peerDependencies": {
33
+ "obsidian": "*"
34
+ },
35
+ "devDependencies": {
36
+ "obsidian": "^1.12.3",
37
+ "typedoc": "^0.28.19",
38
+ "typedoc-plugin-markdown": "^4.12.0",
39
+ "typescript": "6.0.3"
40
+ },
41
+ "scripts": {
42
+ "build": "tsc -p tsconfig.build.json",
43
+ "generate-docs": "typedoc",
44
+ "version": "node scripts/version-bump.mjs"
45
+ },
46
+ "exports": {
47
+ ".": {
48
+ "types": "./dist/index.d.ts",
49
+ "default": "./dist/index.js"
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,44 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { isOcrError, ocrError } from "./errors.js";
3
+
4
+ describe("errors.ts", () => {
5
+ describe("isOcrError", () => {
6
+ it("returns true for an error built by ocrError", () => {
7
+ expect(isOcrError(ocrError("unsupported-file"))).toBe(true);
8
+ });
9
+
10
+ it("returns true for a duck-typed error from another bundle", () => {
11
+ const fromOtherBundle = Object.assign(new Error(), {
12
+ name: "OcrError",
13
+ code: "extraction-failed",
14
+ });
15
+ expect(isOcrError(fromOtherBundle)).toBe(true);
16
+ });
17
+
18
+ it("returns false for anything other than a real OcrError", () => {
19
+ expect(isOcrError(new Error("error"))).toBe(false);
20
+ expect(isOcrError(Object.assign(new Error(), { name: "OcrError" }))).toBe(
21
+ false,
22
+ );
23
+ expect(isOcrError({ name: "OcrError", code: "extraction-failed" })).toBe(
24
+ false,
25
+ );
26
+ });
27
+ });
28
+
29
+ describe("ocrError", () => {
30
+ it("returns an error with the OcrError name and a code", () => {
31
+ const error = ocrError("unsupported-file");
32
+ expect(error).toBeInstanceOf(Error);
33
+ expect(error.name).toBe("OcrError");
34
+ expect(error.code).toBe("unsupported-file");
35
+ });
36
+
37
+ it("includes a provided message and cause", () => {
38
+ const cause = new Error();
39
+ const error = ocrError("extraction-failed", "request failed", { cause });
40
+ expect(error.message).toBe("request failed");
41
+ expect(error.cause).toBe(cause);
42
+ });
43
+ });
44
+ });
package/src/errors.ts ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Identifies which kind of {@link OcrError} occurred.
3
+ *
4
+ * - `"unsupported-file"`: the OCR engine can't process this file (e.g. an
5
+ * unsupported file type)
6
+ * - `"extraction-failed"`: any other failure during extraction (network,
7
+ * auth, etc.)
8
+ *
9
+ * @since 1.0.0
10
+ */
11
+ export type OcrErrorCode = "unsupported-file" | "extraction-failed";
12
+
13
+ /**
14
+ * The error thrown by {@link OcrExtractorApi.extractText} when extraction
15
+ * fails. Identify it with {@link isOcrError} (not `instanceof`, which won't
16
+ * work across the plugin and npm package boundary). The original error,
17
+ * if any, is included as `cause`.
18
+ *
19
+ * @since 1.0.0
20
+ */
21
+ export interface OcrError extends Error {
22
+ readonly code: OcrErrorCode;
23
+ readonly cause?: unknown;
24
+ }
25
+
26
+ /**
27
+ * Checks whether a caught value is an {@link OcrError}.
28
+ *
29
+ * @param error - The caught error to check
30
+ * @returns `true` if `error` is an {@link OcrError}, `false` otherwise
31
+ * @since 1.0.0
32
+ * @example
33
+ * ```ts
34
+ * try {
35
+ * await api.extractText(file);
36
+ * } catch (error) {
37
+ * if (isOcrError(error) && error.code === "unsupported-file") return;
38
+ * throw error;
39
+ * }
40
+ * ```
41
+ */
42
+ export function isOcrError(error: unknown): error is OcrError {
43
+ return (
44
+ error instanceof Error &&
45
+ error.name === "OcrError" &&
46
+ typeof (error as Partial<OcrError>).code === "string"
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Used by the plugin to construct an {@link OcrError} with the given code and
52
+ * message.
53
+ *
54
+ * @param code - Which kind of error this is
55
+ * @param message - A human-readable message
56
+ * @param options - Optional settings
57
+ * @param options.cause - The underlying error, if any
58
+ * @returns The new {@link OcrError}
59
+ * @since 1.0.0
60
+ */
61
+ export function ocrError(
62
+ code: OcrErrorCode,
63
+ message?: string,
64
+ options?: { cause?: unknown },
65
+ ): OcrError {
66
+ const error = new Error(message ?? code, options);
67
+ return Object.assign(error, { name: "OcrError", code });
68
+ }
package/src/index.ts ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Public API for the OCR Extractor Obsidian plugin
3
+ *
4
+ * Access it with {@link getOcrExtractorApi}.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+
9
+ import type { App, TFile } from "obsidian";
10
+
11
+ export {
12
+ type OcrError,
13
+ type OcrErrorCode,
14
+ isOcrError,
15
+ ocrError,
16
+ } from "./errors.js";
17
+
18
+ /**
19
+ * The API version
20
+ *
21
+ * @since 1.0.0
22
+ */
23
+ export const OCR_EXTRACTOR_API_VERSION = "1.0.0";
24
+
25
+ /**
26
+ * The result of a successful extraction
27
+ *
28
+ * @since 1.0.0
29
+ */
30
+ export interface OcrExtractionResult {
31
+ /** Extracted text (or `""` if the file was processed but contained no text) */
32
+ text: string;
33
+ }
34
+
35
+ /** The Obsidian `App` with plugin registry (not present in public types) */
36
+ type AppWithPlugins = App & {
37
+ plugins?: { plugins?: Record<string, { api?: OcrExtractorApi }> };
38
+ };
39
+
40
+ /**
41
+ * The OCR Extractor plugin's public API, obtained via {@link getOcrExtractorApi}
42
+ *
43
+ * @since 1.0.0
44
+ */
45
+ export interface OcrExtractorApi {
46
+ /**
47
+ * The API version (i.e. {@link OCR_EXTRACTOR_API_VERSION})
48
+ *
49
+ * @since 1.0.0
50
+ */
51
+ readonly version: string;
52
+
53
+ /**
54
+ * Run the user's configured OCR engine on an attachment and return the
55
+ * extracted text. Does not modify any note.
56
+ *
57
+ * @param file - The attachment's `TFile`
58
+ * @param options - Optional settings
59
+ * @param options.signal - An optional `AbortSignal` to cancel the extraction
60
+ * @returns An {@link OcrExtractionResult} with the extracted `text` (`""` if none found)
61
+ * @throws An {@link OcrError} with an error `code` ({@link OcrErrorCode}), or
62
+ * an `AbortError` if canceled via `options.signal`
63
+ * @since 1.0.0
64
+ * @example
65
+ * ```ts
66
+ * const { text } = await api.extractText(file);
67
+ * ```
68
+ */
69
+ extractText(
70
+ file: TFile,
71
+ options?: { signal?: AbortSignal },
72
+ ): Promise<OcrExtractionResult>;
73
+ }
74
+
75
+ /**
76
+ * Get the OCR Extractor API from the Obsidian `app`.
77
+ *
78
+ * @param app - The Obsidian app instance
79
+ * @returns The API, or `undefined` if the plugin isn't installed or enabled
80
+ * @since 1.0.0
81
+ * @example
82
+ * ```ts
83
+ * const api = getOcrExtractorApi(app);
84
+ * if (!api) return;
85
+ * ```
86
+ */
87
+ export function getOcrExtractorApi(app: App): OcrExtractorApi | undefined {
88
+ const plugins = (app as AppWithPlugins).plugins?.plugins;
89
+ return plugins?.["ocr-extractor"]?.api;
90
+ }