feat(ocr): display OCR text in search results

This commit is contained in:
Elian Doran 2025-07-26 12:41:30 +03:00
parent f295592134
commit 6212ea0304
No known key found for this signature in database
2 changed files with 40 additions and 4 deletions

View File

@ -46,9 +46,9 @@ async function getRenderedContent(this: {} | { ctx: string }, entity: FNote | FA
} else if (type === "code") { } else if (type === "code") {
await renderCode(entity, $renderedContent); await renderCode(entity, $renderedContent);
} else if (["image", "canvas", "mindMap"].includes(type)) { } else if (["image", "canvas", "mindMap"].includes(type)) {
renderImage(entity, $renderedContent, options); await renderImage(entity, $renderedContent, options);
} else if (!options.tooltip && ["file", "pdf", "audio", "video"].includes(type)) { } else if (!options.tooltip && ["file", "pdf", "audio", "video"].includes(type)) {
renderFile(entity, type, $renderedContent); await renderFile(entity, type, $renderedContent);
} else if (type === "mermaid") { } else if (type === "mermaid") {
await renderMermaid(entity, $renderedContent); await renderMermaid(entity, $renderedContent);
} else if (type === "render" && entity instanceof FNote) { } else if (type === "render" && entity instanceof FNote) {
@ -133,7 +133,7 @@ async function renderCode(note: FNote | FAttachment, $renderedContent: JQuery<HT
await applySingleBlockSyntaxHighlight($codeBlock, normalizeMimeTypeForCKEditor(note.mime)); await applySingleBlockSyntaxHighlight($codeBlock, normalizeMimeTypeForCKEditor(note.mime));
} }
function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>, options: Options = {}) { async function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>, options: Options = {}) {
const encodedTitle = encodeURIComponent(entity.title); const encodedTitle = encodeURIComponent(entity.title);
let url; let url;
@ -173,9 +173,39 @@ function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLE
} }
imageContextMenuService.setupContextMenu($img); imageContextMenuService.setupContextMenu($img);
// Add OCR text display for image notes
if (entity instanceof FNote) {
await addOCRTextIfAvailable(entity, $renderedContent);
}
} }
function renderFile(entity: FNote | FAttachment, type: string, $renderedContent: JQuery<HTMLElement>) { 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" style="margin: 10px 0; padding: 10px; background: var(--accented-background-color); border-radius: 5px; border-left: 3px solid var(--main-text-color);">
<div class="ocr-header" style="font-weight: bold; margin-bottom: 8px; font-size: 0.9em; color: var(--muted-text-color);">
<span class="bx bx-text"></span> ${t("ocr.extracted_text")}
</div>
<div class="ocr-content" style="max-height: 150px; overflow-y: auto; font-size: 0.9em; line-height: 1.4; white-space: pre-wrap;"></div>
</div>
`);
$ocrSection.find('.ocr-content').text(data.text);
$content.append($ocrSection);
}
}
} catch (error) {
// Silently fail if OCR API is not available
console.debug('Failed to fetch OCR text:', error);
}
}
async function renderFile(entity: FNote | FAttachment, type: string, $renderedContent: JQuery<HTMLElement>) {
let entityType, entityId; let entityType, entityId;
if (entity instanceof FNote) { if (entity instanceof FNote) {
@ -211,6 +241,11 @@ function renderFile(entity: FNote | FAttachment, type: string, $renderedContent:
$content.append($videoPreview); $content.append($videoPreview);
} }
// Add OCR text display for file notes
if (entity instanceof FNote) {
await addOCRTextIfAvailable(entity, $content);
}
if (entityType === "notes" && "noteId" in entity) { if (entityType === "notes" && "noteId" in entity) {
// TODO: we should make this available also for attachments, but there's a problem with "Open externally" support // TODO: we should make this available also for attachments, but there's a problem with "Open externally" support
// in attachment list // in attachment list

View File

@ -2005,6 +2005,7 @@
"add-column": "Add Column" "add-column": "Add Column"
}, },
"ocr": { "ocr": {
"extracted_text": "Extracted Text (OCR)",
"extracted_text_title": "Extracted Text (OCR)", "extracted_text_title": "Extracted Text (OCR)",
"loading_text": "Loading OCR text...", "loading_text": "Loading OCR text...",
"no_text_available": "No OCR text available", "no_text_available": "No OCR text available",