Skip to content

Build your personal knowledge base with Trilium Notes

fix(ocr): port the office processor to officeparser 8

Elian Doran4cc3884e9379
diff --git a/apps/server/src/services/ocr/processors/office_processor.spec.ts b/apps/server/src/services/ocr/processors/office_processor.spec.ts
index 88d6edc92e..b1f1234537 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,7 @@ describe('OfficeProcessor', () => {
it('extracts text and reports high confidence when text is present', async () => {
const processor = new OfficeProcessor();
- mockParseOffice.mockResolvedValue({ toText: () => ' document body ' });
+ mockParseOffice.mockResolvedValue({ to: async () => ({ value: ' document body ' }) });
const result = await processor.extractText(buffer, { mimeType: DOCX, language: 'deu' });
@@ -62,7 +62,6 @@ describe('OfficeProcessor', () => {
expect(result.pageCount).toBe(1);
expect(result.language).toBe('deu');
expect(mockParseOffice).toHaveBeenCalledWith(buffer, {
- outputErrorToConsole: false,
newlineDelimiter: '\n',
ignoreNotes: false
});
@@ -70,13 +69,12 @@ describe('OfficeProcessor', () => {
it('passes an explicit fileType hint for RTF, whose buffer auto-detection is unreliable', async () => {
const processor = new OfficeProcessor();
- mockParseOffice.mockResolvedValue({ toText: () => 'rtf body' });
+ mockParseOffice.mockResolvedValue({ to: async () => ({ value: 'rtf body' }) });
const result = await processor.extractText(buffer, { mimeType: RTF });
expect(result.text).toBe('rtf body');
expect(mockParseOffice).toHaveBeenCalledWith(buffer, {
- outputErrorToConsole: false,
newlineDelimiter: '\n',
ignoreNotes: false,
fileType: 'rtf'
@@ -85,13 +83,12 @@ describe('OfficeProcessor', () => {
it('passes an explicit fileType hint for EPUB, which shares the ZIP container with DOCX/ODT', async () => {
const processor = new OfficeProcessor();
- mockParseOffice.mockResolvedValue({ toText: () => 'epub body' });
+ mockParseOffice.mockResolvedValue({ to: async () => ({ value: 'epub body' }) });
const result = await processor.extractText(buffer, { mimeType: EPUB });
expect(result.text).toBe('epub body');
expect(mockParseOffice).toHaveBeenCalledWith(buffer, {
- outputErrorToConsole: false,
newlineDelimiter: '\n',
ignoreNotes: false,
fileType: 'epub'
@@ -100,7 +97,7 @@ describe('OfficeProcessor', () => {
it('reports zero confidence and defaults language when no text is extracted', async () => {
const processor = new OfficeProcessor();
- mockParseOffice.mockResolvedValue({ toText: () => ' ' });
+ mockParseOffice.mockResolvedValue({ to: async () => ({ value: ' ' }) });
const result = await processor.extractText(buffer, { mimeType: ODT });
diff --git a/apps/server/src/services/ocr/processors/office_processor.ts b/apps/server/src/services/ocr/processors/office_processor.ts
index 4680dd8039..ff93341083 100644
--- a/apps/server/src/services/ocr/processors/office_processor.ts
+++ b/apps/server/src/services/ocr/processors/office_processor.ts
@@ -6,7 +6,6 @@ import { OCRProcessingOptions, OCRResult } from '../ocr_service.js';
import { FileProcessor } from './file_processor.js';
const PARSER_CONFIG: OfficeParserConfig = {
- outputErrorToConsole: false,
newlineDelimiter: '\n',
ignoreNotes: false
};
@@ -39,7 +38,11 @@ export class OfficeProcessor extends FileProcessor {
// Dynamically imported so officeparser only loads when an Office file is actually processed.
const { OfficeParser } = await import('officeparser');
const ast = await OfficeParser.parseOffice(buffer, config);
- const trimmed = ast.toText().trim();
+ // `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 } });
+ const trimmed = value.trim();
return {
text: trimmed,
diff --git a/packages/trilium-core/src/services/office_preview.ts b/packages/trilium-core/src/services/office_preview.ts
index 8120d5a101..7b19873a4d 100644
--- a/packages/trilium-core/src/services/office_preview.ts
+++ b/packages/trilium-core/src/services/office_preview.ts
@@ -56,13 +56,15 @@ export async function convertOfficeToHtml(content: string | Uint8Array, mime: st
const { OfficeConverter } = await import("officeparser");
const { value } = await OfficeConverter.convert(buffer, "html", {
// Pass the explicit fileType only when auto-detection is unreliable (RTF/EPUB).
- // Embedded images need no extra config: the parser always emits them as data: URIs,
- // which the client-side sanitizer permits on <img>.
parseConfig: fileType ? { fileType } : undefined,
generatorConfig: {
// Charts would be emitted as a Chart.js <script> (loaded from a CDN) that the
// client-side sanitizer strips anyway, so don't bother generating them.
includeCharts: false,
+ // Inline every embedded image as a data: URI, which the client-side sanitizer permits
+ // on <img>. Above the default cap a fragment emits `<img src="<attachment name>">`,
+ // which resolves against the note's URL and 404s.
+ maxInlineImageBytes: Infinity,
// Emit an embeddable fragment instead of a full standalone <html> document.
htmlConfig: { standalone: false }
}