Skip to content

Build your personal knowledge base with Trilium Notes

fix(ocr): image OCR in search results not shown

Elian Doran32f95efa5437
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index f554eb55ac..0b29513c2f 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -1,6 +1,6 @@
import "./content_renderer.css";
-import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
+import { normalizeMimeTypeForCKEditor, type TextRepresentationResponse } from "@triliumnext/commons";
import { h, render } from "preact";
import WheelZoom from 'vanilla-js-wheel-zoom';
@@ -15,6 +15,7 @@ import openService from "./open.js";
import protectedSessionService from "./protected_session.js";
import protectedSessionHolder from "./protected_session_holder.js";
import renderService from "./render.js";
+import server from "./server.js";
import { applySingleBlockSyntaxHighlight } from "./syntax_highlight.js";
import utils, { getErrorMessage } from "./utils.js";
@@ -58,7 +59,7 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNo
} else if (["image", "canvas", "mindMap", "spreadsheet"].includes(type)) {
await renderImage(entity, $renderedContent, options);
} else if (!options.tooltip && ["file", "pdf", "audio", "video"].includes(type)) {
- await renderFile(entity, type, $renderedContent);
+ await renderFile(entity, type, $renderedContent, options);
} else if (type === "mermaid") {
await renderMermaid(entity, $renderedContent);
} else if (type === "render" && entity instanceof FNote) {
@@ -153,7 +154,8 @@ async function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery
$renderedContent // styles needed for the zoom to work well
.css("display", "flex")
.css("align-items", "center")
- .css("justify-content", "center");
+ .css("justify-content", "center")
+ .css("flex-direction", "column"); // OCR text is displayed below the image.
const $img = $("<img>")
.attr("src", url || "")
@@ -180,7 +182,6 @@ async function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery
imageContextMenuService.setupContextMenu($img);
- // Add OCR text display for image notes
if (entity instanceof FNote && options.showTextRepresentation) {
await addOCRTextIfAvailable(entity, $renderedContent);
}
@@ -188,22 +189,19 @@ async function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery
async function addOCRTextIfAvailable(note: FNote, $content: JQuery<HTMLElement>) {
try {
- const response = await fetch(`api/ocr/notes/${note.noteId}/text`);
- if (response.ok) {
- const data = await response.json();
- if (data.success && data.hasOcr && data.text) {
- const $ocrSection = $(`
- <div class="ocr-text-section">
- <div class="ocr-header">
- <span class="bx bx-text"></span> ${t("ocr.extracted_text")}
- </div>
- <div class="ocr-content"></div>
+ const data = await server.get<TextRepresentationResponse>(`ocr/notes/${note.noteId}/text`);
+ if (data.success && data.hasOcr && data.text) {
+ const $ocrSection = $(`
+ <div class="ocr-text-section">
+ <div class="ocr-header">
+ <span class="bx bx-text"></span> ${t("ocr.extracted_text")}
</div>
- `);
+ <div class="ocr-content"></div>
+ </div>
+ `);
- $ocrSection.find('.ocr-content').text(data.text);
- $content.append($ocrSection);
- }
+ $ocrSection.find('.ocr-content').text(data.text);
+ $content.append($ocrSection);
}
} catch (error) {
// Silently fail if OCR API is not available
@@ -251,7 +249,6 @@ async function renderFile(entity: FNote | FAttachment, type: string, $renderedCo
$content.append($videoPreview);
}
- // Add OCR text display for file notes
if (entity instanceof FNote && options.showTextRepresentation) {
await addOCRTextIfAvailable(entity, $content);
}
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index b89765b9db..536db2d9b5 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -2643,11 +2643,11 @@ iframe.print-iframe {
}
.ocr-text-section {
- margin: 10px 0;
padding: 10px;
background: var(--accented-background-color);
border-left: 3px solid var(--main-border-color);
text-align: left;
+ width: 100%;
}
.ocr-header {
@@ -2663,4 +2663,4 @@ iframe.print-iframe {
font-size: 0.9em;
line-height: 1.4;
white-space: pre-wrap;
-}
\ No newline at end of file
+}
diff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsx
index 0b864ace25..0c37c4a08a 100644
--- a/apps/client/src/widgets/collections/NoteList.tsx
+++ b/apps/client/src/widgets/collections/NoteList.tsx
@@ -25,6 +25,7 @@ interface NoteListProps {
viewType: ViewTypeOptions | undefined;
onReady?: (data: PrintReport) => void;
onProgressChanged?(progress: number): void;
+ showTextRepresentation?: boolean;
}
type LazyLoadedComponent = ((props: ViewModeProps<any>) => VNode<any> | undefined);
@@ -67,7 +68,7 @@ export default function NoteList(props: Pick<NoteListProps, "displayOnlyCollecti
export function SearchNoteList(props: Omit<NoteListProps, "isEnabled" | "viewType">) {
const viewType = useNoteViewType(props.note);
- return <CustomNoteList {...props} isEnabled={true} viewType={viewType} />;
+ return <CustomNoteList {...props} isEnabled={true} viewType={viewType} showTextRepresentation />;
}
export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, onProgressChanged, ...restProps }: NoteListProps) {
diff --git a/apps/client/src/widgets/collections/interface.ts b/apps/client/src/widgets/collections/interface.ts
index 4a965588df..15acbfa8e2 100644
--- a/apps/client/src/widgets/collections/interface.ts
+++ b/apps/client/src/widgets/collections/interface.ts
@@ -21,4 +21,5 @@ export interface ViewModeProps<T extends object> {
media: ViewModeMedia;
onReady(data: PrintReport): void;
onProgressChanged?: ProgressChangedFn;
+ showTextRepresentation?: boolean;
}
diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
index 238817d978..0e3d7d5add 100644
--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx
@@ -23,7 +23,7 @@ import { ComponentChildren, TargetedMouseEvent } from "preact";
const contentSizeObserver = new ResizeObserver(onContentResized);
-export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens }: ViewModeProps<{}>) {
+export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens, showTextRepresentation }: ViewModeProps<{}>) {
const expandDepth = useExpansionDepth(note);
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
const { pageNotes, ...pagination } = usePagination(note, noteIds);
@@ -37,13 +37,14 @@ export function ListView({ note, noteIds: unfilteredNoteIds, highlightedTokens }
key={childNote.noteId}
note={childNote} parentNote={note}
expandDepth={expandDepth} highlightedTokens={highlightedTokens}
- currentLevel={1} includeArchived={includeArchived} />
+ currentLevel={1} includeArchived={includeArchived}
+ showTextRepresentation={showTextRepresentation} />
))}
</Card>
</NoteList>;
}
-export function GridView({ note, noteIds: unfilteredNoteIds, highlightedTokens }: ViewModeProps<{}>) {
+export function GridView({ note, noteIds: unfilteredNoteIds, highlightedTokens, showTextRepresentation }: ViewModeProps<{}>) {
const noteIds = useFilteredNoteIds(note, unfilteredNoteIds);
const { pageNotes, ...pagination } = usePagination(note, noteIds);
const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");
@@ -56,7 +57,8 @@ export function GridView({ note, noteIds: unfilteredNoteIds, highlightedTokens }
note={childNote}
parentNote={note}
highlightedTokens={highlightedTokens}
- includeArchived={includeArchived} />
+ includeArchived={includeArchived}
+ showTextRepresentation={showTextRepresentation} />
))}
</div>
</NoteList>
@@ -91,13 +93,14 @@ function NoteList(props: NoteListProps) {
</div>
}
-function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived }: {
+function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expandDepth, includeArchived, showTextRepresentation }: {
note: FNote,
parentNote: FNote,
currentLevel: number,
expandDepth: number,
highlightedTokens: string[] | null | undefined;
includeArchived: boolean;
+ showTextRepresentation?: boolean;
}) {
const [ isExpanded, setExpanded ] = useState(currentLevel <= expandDepth);
@@ -113,7 +116,8 @@ function ListNoteCard({ note, parentNote, highlightedTokens, currentLevel, expan
<NoteContent note={note}
highlightedTokens={highlightedTokens}
noChildrenList
- includeArchivedNotes={includeArchived} />
+ includeArchivedNotes={includeArchived}
+ showTextRepresentation={showTextRepresentation} />
</CardSection>
<NoteChildren note={note}
@@ -157,6 +161,7 @@ interface GridNoteCardProps {
parentNote: FNote;
highlightedTokens: string[] | null | undefined;
includeArchived: boolean;
+ showTextRepresentation?: boolean;
}
function GridNoteCard(props: GridNoteCardProps) {
@@ -185,6 +190,7 @@ function GridNoteCard(props: GridNoteCardProps) {
trim
highlightedTokens={props.highlightedTokens}
includeArchivedNotes={props.includeArchived}
+ showTextRepresentation={props.showTextRepresentation}
/>
</CardFrame>
);
@@ -201,12 +207,13 @@ function NoteAttributes({ note }: { note: FNote }) {
return <span className="note-list-attributes" ref={ref} />;
}
-export function NoteContent({ note, trim, noChildrenList, highlightedTokens, includeArchivedNotes }: {
+export function NoteContent({ note, trim, noChildrenList, highlightedTokens, includeArchivedNotes, showTextRepresentation }: {
note: FNote;
trim?: boolean;
noChildrenList?: boolean;
highlightedTokens: string[] | null | undefined;
includeArchivedNotes: boolean;
+ showTextRepresentation?: boolean;
}) {
const contentRef = useRef<HTMLDivElement>(null);
const highlightSearch = useImperativeSearchHighlighlighting(highlightedTokens);
@@ -230,7 +237,8 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, inc
trim,
noChildrenList,
noIncludedNotes: true,
- includeArchivedNotes
+ includeArchivedNotes,
+ showTextRepresentation
})
.then(({ $renderedContent, type }) => {
if (!contentRef.current) return;