Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ocr): keep image placeholders out of extracted office text
Elian Dorancfe38e971f22
diff --git a/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts b/apps/server/src/services/ocr/processors/office_processor.integration.spec.tsindex 31762a0112..34291acdae 100644--- a/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts+++ b/apps/server/src/services/ocr/processors/office_processor.integration.spec.ts@@ -126,6 +126,18 @@ describe('OfficeProcessor (integration — real officeparser)', () => {expect(docx.text).toBe(odt.text);expect(docx.text.length).toBeGreaterThan(500);});++ it('leaves embedded images out of the extracted text', async () => {+ const processor = new OfficeProcessor();++ // The sample holds a paragraph, an image with the alt text "Architecture diagram",+ // and another paragraph.+ const result = await processor.extractText(readSample('embedded_image.odt'), {+ mimeType: ODT+ });++ expect(result.text).toBe('Text before the figure.\nText after the figure.');+ });});function readSample(fileName: string): Buffer {diff --git a/apps/server/src/services/ocr/processors/office_processor.spec.ts b/apps/server/src/services/ocr/processors/office_processor.spec.tsindex b1f1234537..8b95afe7c7 100644--- a/apps/server/src/services/ocr/processors/office_processor.spec.ts+++ b/apps/server/src/services/ocr/processors/office_processor.spec.ts@@ -53,7 +53,8 @@ describe('OfficeProcessor', () => {it('extracts text and reports high confidence when text is present', async () => {const processor = new OfficeProcessor();- mockParseOffice.mockResolvedValue({ to: async () => ({ value: ' document body ' }) });+ const to = vi.fn().mockResolvedValue({ value: ' document body ' });+ mockParseOffice.mockResolvedValue({ to });const result = await processor.extractText(buffer, { mimeType: DOCX, language: 'deu' });@@ -65,6 +66,10 @@ describe('OfficeProcessor', () => {newlineDelimiter: '\n',ignoreNotes: false});+ expect(to).toHaveBeenCalledWith('text', {+ includeImages: 'none',+ textConfig: { preserveLayout: false }+ });});it('passes an explicit fileType hint for RTF, whose buffer auto-detection is unreliable', async () => {diff --git a/apps/server/src/services/ocr/processors/office_processor.ts b/apps/server/src/services/ocr/processors/office_processor.tsindex ff93341083..2411c3b472 100644--- a/apps/server/src/services/ocr/processors/office_processor.ts+++ b/apps/server/src/services/ocr/processors/office_processor.ts@@ -41,7 +41,12 @@ export class OfficeProcessor extends FileProcessor {// `preserveLayout` pads cells into aligned columns and prefixes list markers, which the// search index has no use for and which makes a document's text depend on the format it// was authored in. A flat stream of text nodes indexes the same words either way.- const { value } = await ast.to('text', { textConfig: { preserveLayout: false } });+ // `includeImages: 'none'` drops the `[Image: <alt text>]` line the text generator+ // otherwise writes for every embedded image.+ const { value } = await ast.to('text', {+ includeImages: 'none',+ textConfig: { preserveLayout: false }+ });const trimmed = value.trim();return {diff --git a/apps/server/src/services/ocr/processors/samples/embedded_image.odt b/apps/server/src/services/ocr/processors/samples/embedded_image.odtnew file mode 100644index 0000000000..e3965a4c7eBinary files /dev/null and b/apps/server/src/services/ocr/processors/samples/embedded_image.odt differ