Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(interface): normalized api

Corentin Thomasset44b5b9fd5af1
diff --git a/README.md b/README.md
index ce059fa7..4dae0988 100644
--- a/README.md
+++ b/README.md
@@ -31,31 +31,20 @@ You can import the library using ES Modules or CommonJS syntax:
```javascript
// ES Modules
-import { extractText, extractTextFromBlob } from '@papra/lecture';
-```
+import { extractText } from '@papra/lecture';
+// or CommonJS
+const { extractText } = require('@papra/lecture');
-```javascript
-// CommonJS
-const { extractText, extractTextFromBlob } = require('@papra/lecture');
+const { textContent } = await extractText({arrayBuffer: file, mimeType: 'application/pdf'});
+
+console.log('Extracted Text:', textContent);
```
### Functions
#### `extractText`
-Extracts text from a file's binary data using its MIME type.
-
-**Parameters**:
-
-- `arrayBuffer` (`ArrayBuffer`): The binary content of the file.
-- `mimeType` (`string`): The MIME type of the file.
-
-**Returns**:
-A promise that resolves to an object with the following properties:
-
-- `extractorName` (`string | undefined`): The name of the extractor used.
-- `textContent` (`string | undefined`): The extracted text content, if available.
-- `error` (`Error | undefined`): An error object, if an issue occurred during extraction.
+Extracts text from an arrayBuffer using its MIME type.
**Example**:
@@ -63,38 +52,33 @@ A promise that resolves to an object with the following properties:
const file = await fetch('example.pdf').then(res => res.arrayBuffer());
const mimeType = 'application/pdf';
-const result = await extractText({ arrayBuffer: file, mimeType });
+const { textContent } = await extractText({ arrayBuffer: file, mimeType });
-if (result.textContent) {
- console.log('Extracted Text:', result.textContent);
-} else {
- console.error('Error:', result.error);
-}
+console.log('Extracted Text:', textContent);
```
#### `extractTextFromBlob`
Extracts text from a `Blob` object (e.g., files or data retrieved from APIs).
-**Parameters**:
+```javascript
+const blob = new Blob(['Hello, World!'], { type: 'text/plain' });
-- `blob` (`Blob`): A Blob representing the file content.
+const { textContent } = await extractTextFromBlob({ blob });
-**Returns**:
-A promise that resolves with the same structure as `extractText`.
+console.log('Extracted Text:', textContent);
+```
-**Example**:
+#### `extractTextFromFile`
+
+Extracts text from a file using its MIME type.
```javascript
-const inputFile = document.querySelector('#file-input').files[0]; // HTML File Input
+const file = document.querySelector('#file-input').files[0]; // HTML File Input
-const result = await extractTextFromBlob(inputFile);
+const { textContent } = await extractTextFromFile({ file });
-if (result.textContent) {
- console.log('Extracted Text:', result.textContent);
-} else {
- console.error('Error:', result.error);
-}
+console.log('Extracted Text:', textContent);
```
## Supported File Formats
diff --git a/src/extractors.usecases.ts b/src/extractors.usecases.ts
index b3ab1f8c..5162945a 100644
--- a/src/extractors.usecases.ts
+++ b/src/extractors.usecases.ts
@@ -30,9 +30,13 @@ export async function extractText({ arrayBuffer, mimeType }: { arrayBuffer: Arra
}
}
-export async function extractTextFromBlob(blob: Blob) {
+export async function extractTextFromBlob({ blob }: { blob: Blob }) {
const arrayBuffer = await blob.arrayBuffer();
const mimeType = blob.type;
return extractText({ arrayBuffer, mimeType });
}
+
+export async function extractTextFromFile({ file }: { file: File }) {
+ return extractTextFromBlob({ blob: file });
+}
diff --git a/src/index.ts b/src/index.ts
index 97e11614..1dc686d1 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1 +1,5 @@
-export { extractText } from './extractors.usecases';
+export {
+ extractText,
+ extractTextFromBlob,
+ extractTextFromFile,
+} from './extractors.usecases';