-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(indexer): add pdf support (closes #90)
- Loading branch information
Showing
9 changed files
with
465 additions
and
76 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export interface Document { | ||
filename: string; | ||
type: string; | ||
category: string; | ||
sections: Section[]; | ||
} | ||
|
||
export interface Section { | ||
id: string; | ||
content: string; | ||
category: string; | ||
sourcepage: string; | ||
sourcefile: string; | ||
embedding?: number[]; | ||
} | ||
|
||
export interface ContentPage { | ||
content: string; | ||
offset: number; | ||
page: number; | ||
} | ||
|
||
export interface ContentSection { | ||
content: string; | ||
page: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './pdf.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as pdfjs from 'pdfjs-dist'; | ||
import { type TextItem } from 'pdfjs-dist/types/src/display/api.js'; | ||
import { type ContentPage } from '../document.js'; | ||
|
||
export async function extractTextFromPdf(data: Buffer): Promise<ContentPage[]> { | ||
const pages: ContentPage[] = []; | ||
const pdf = await pdfjs.getDocument(new Uint8Array(data)).promise; | ||
let offset = 0; | ||
|
||
for (let i = 1; i <= pdf.numPages; i++) { | ||
const page = await pdf.getPage(i); | ||
const textContent = await page.getTextContent(); | ||
let previousY = 0; | ||
const text = textContent.items | ||
.filter((item) => 'str' in item) | ||
.map((item) => { | ||
const textItem = item as TextItem; | ||
const y = textItem.transform[5]; | ||
let textContent = textItem.str; | ||
if (y !== previousY && previousY !== 0) { | ||
// If the Y coordinate changes, we're on a new line | ||
textContent = '\n' + textContent; | ||
} | ||
previousY = y; | ||
return textContent; | ||
}) | ||
.join(''); | ||
|
||
pages.push({ content: text + '\n', offset, page: i }); | ||
offset += text.length; | ||
} | ||
return pages; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters