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(pdf): highlights not processed correctly
Elian Doran7311e1a734bb
diff --git a/apps/client/src/types-pdfjs.d.ts b/apps/client/src/types-pdfjs.d.tsindex cdc0927050..3202dafb63 100644--- a/apps/client/src/types-pdfjs.d.ts+++ b/apps/client/src/types-pdfjs.d.ts@@ -111,6 +111,7 @@ interface PdfAnnotationInfo {id: string;type: string;contents: string;+ highlightedText: string;author: string;pageNumber: number;color: string | null;diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.css b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.cssindex 4a905d062b..ebabc1a974 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.css+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.css@@ -42,6 +42,16 @@min-width: 0;}+.pdf-annotation-highlighted-text {+ font-size: 13px;+ font-style: italic;+ color: var(--muted-text-color);+ overflow: hidden;+ display: -webkit-box;+ -webkit-line-clamp: 2;+ -webkit-box-orient: vertical;+}+.pdf-annotation-contents {font-size: 13px;color: var(--main-text-color);diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsxindex 2780156eea..54354388ea 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx@@ -86,6 +86,9 @@ function PdfAnnotationItem({<Icon icon={icon} /></div><div className="pdf-annotation-info">+ {annotation.highlightedText && (+ <div className="pdf-annotation-highlighted-text">{annotation.highlightedText}</div>+ )}{annotation.contents && (<div className="pdf-annotation-contents">{annotation.contents}</div>)}diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.tsnew file mode 100644index 0000000000..f948d9784a--- /dev/null+++ b/packages/pdfjs-viewer/src/annotations.spec.ts@@ -0,0 +1,81 @@+import { describe, expect, it } from "vitest";+import { processAnnotation, AnnotationType, rgbToHex } from "./annotations";++const SAMPLE_HIGHLIGHT = {+ annotationType: 9,+ annotationFlags: 4,+ borderStyle: { width: 0, rawWidth: 1, style: 1, dashArray: [3], horizontalCornerRadius: 0, verticalCornerRadius: 0 },+ color: { "0": 128, "1": 235, "2": 255 },+ backgroundColor: null,+ borderColor: null,+ rotation: 0,+ contentsObj: { str: "Comment goes here.", dir: "ltr" },+ hasAppearance: true,+ id: "18R",+ modificationDate: null,+ rect: [352.43, 276.30, 489.91, 314.61],+ subtype: "Highlight",+ hasOwnCanvas: false,+ noRotate: false,+ noHTML: false,+ isEditable: true,+ structParent: -1,+ titleObj: { str: "", dir: "ltr" },+ creationDate: "D:20260425093822",+ popupRef: "24R",+ opacity: 1,+ quadPoints: { "0": 353.35, "1": 313.98, "2": 489.05, "3": 313.98, "4": 353.35, "5": 276.95, "6": 489.05, "7": 276.95 },+ overlaidText: "First slide"+};++describe("processAnnotation", () => {+ it("extracts all fields from a highlight annotation", () => {+ const result = processAnnotation(SAMPLE_HIGHLIGHT, 3)!;+ expect(result).not.toBeNull();+ expect(result.id).toBe("18R");+ expect(result.type).toBe("highlight");+ expect(result.contents).toBe("Comment goes here.");+ expect(result.highlightedText).toBe("First slide");+ expect(result.author).toBe("");+ expect(result.pageNumber).toBe(3);+ expect(result.color).toBe("#80ebff");+ expect(result.creationDate).toBe("D:20260425093822");+ expect(result.modificationDate).toBeNull();+ });++ it("extracts author from titleObj.str", () => {+ const withAuthor = { ...SAMPLE_HIGHLIGHT, titleObj: { str: "John Doe", dir: "ltr" } };+ expect(processAnnotation(withAuthor, 1)!.author).toBe("John Doe");+ });++ it("skips non-comment types and empty annotations", () => {+ const link = { ...SAMPLE_HIGHLIGHT, annotationType: AnnotationType.LINK };+ expect(processAnnotation(link, 1)).toBeNull();++ const empty = { ...SAMPLE_HIGHLIGHT, contentsObj: { str: "", dir: "ltr" }, overlaidText: "" };+ expect(processAnnotation(empty, 1)).toBeNull();+ });++ it("keeps annotations with only one of contents or highlightedText", () => {+ const highlightOnly = { ...SAMPLE_HIGHLIGHT, contentsObj: { str: "", dir: "ltr" } };+ expect(processAnnotation(highlightOnly, 1)).not.toBeNull();+ expect(processAnnotation(highlightOnly, 1)!.highlightedText).toBe("First slide");++ const commentOnly = { ...SAMPLE_HIGHLIGHT, annotationType: AnnotationType.TEXT, overlaidText: undefined };+ expect(processAnnotation(commentOnly, 1)).not.toBeNull();+ expect(processAnnotation(commentOnly, 1)!.contents).toBe("Comment goes here.");+ });++ it("handles null color", () => {+ expect(processAnnotation({ ...SAMPLE_HIGHLIGHT, color: null }, 1)!.color).toBeNull();+ });+});++describe("rgbToHex", () => {+ it("converts various color formats to hex", () => {+ expect(rgbToHex({ 0: 128, 1: 235, 2: 255 })).toBe("#80ebff");+ expect(rgbToHex([255, 0, 0])).toBe("#ff0000");+ expect(rgbToHex([0, 0, 0])).toBe("#000000");+ expect(rgbToHex([255, 255, 255])).toBe("#ffffff");+ });+});diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.tsindex 214304b3fa..f9276ef679 100644--- a/packages/pdfjs-viewer/src/annotations.ts+++ b/packages/pdfjs-viewer/src/annotations.ts@@ -1,5 +1,5 @@// PDF annotation type constants (from PDF spec / pdfjs-dist AnnotationType)-const AnnotationType = {+export const AnnotationType = {TEXT: 1,LINK: 2,FREETEXT: 3,@@ -54,6 +54,36 @@ const TYPE_NAMES: Record<number, string> = {[AnnotationType.CARET]: "caret"};+/**+ * Process a raw PDF.js annotation object into a normalized PdfAnnotationInfo,+ * or return null if it should be skipped.+ */+export function processAnnotation(ann: Record<string, any>, pageNumber: number): PdfAnnotationInfo | null {+ if (!COMMENT_TYPES.has(ann.annotationType)) {+ return null;+ }++ const contents = ann.contentsObj?.str || "";+ const highlightedText = ann.overlaidText || "";++ // Skip annotations that have no meaningful content+ if (!contents && !highlightedText) {+ return null;+ }++ return {+ id: ann.id,+ type: TYPE_NAMES[ann.annotationType] ?? "unknown",+ contents,+ highlightedText,+ author: ann.titleObj?.str || "",+ pageNumber,+ color: ann.color ? rgbToHex(ann.color) : null,+ creationDate: ann.creationDate || null,+ modificationDate: ann.modificationDate || null+ };+}+export async function setupPdfAnnotations() {await extractAndSendAnnotations();@@ -78,20 +108,10 @@ async function extractAndSendAnnotations() {const pageAnnotations = await page.getAnnotations({ intent: "display" });for (const ann of pageAnnotations) {- if (!COMMENT_TYPES.has(ann.annotationType)) continue;- // Skip annotations that have no meaningful content- if (!ann.contents && !ann.richText && ann.annotationType !== AnnotationType.HIGHLIGHT) continue;-- annotations.push({- id: ann.id,- type: TYPE_NAMES[ann.annotationType] ?? "unknown",- contents: ann.contents || "",- author: ann.titleObj?.str || "",- pageNumber: i,- color: ann.color ? rgbToHex(ann.color) : null,- creationDate: ann.creationDate || null,- modificationDate: ann.modificationDate || null- });+ const processed = processAnnotation(ann, i);+ if (processed) {+ annotations.push(processed);+ }}}@@ -113,7 +133,7 @@ function scrollToAnnotation(pageNumber: number) {app.pdfViewer.currentPageNumber = pageNumber;}-function rgbToHex(rgb: Uint8ClampedArray | number[]): string {+export function rgbToHex(rgb: Uint8ClampedArray | Record<number, number> | number[]): string {const r = rgb[0];const g = rgb[1];const b = rgb[2];diff --git a/packages/pdfjs-viewer/vitest.config.ts b/packages/pdfjs-viewer/vitest.config.tsnew file mode 100644index 0000000000..381fd00be9--- /dev/null+++ b/packages/pdfjs-viewer/vitest.config.ts@@ -0,0 +1,18 @@+import { defineConfig } from 'vite';++export default defineConfig(() => ({+ root: __dirname,+ cacheDir: '../../node_modules/.vite/packages/pdfjs-viewer',+ plugins: [],+ test: {+ 'watch': false,+ 'globals': true,+ 'environment': "node",+ 'include': ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],+ 'reporters': ["default"],+ 'coverage': {+ 'reportsDirectory': './test-output/vitest/coverage',+ 'provider': 'v8' as const,+ }+ },+}));