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(print): carry the note's title into the exported PDF metadata
unknown5e865579f689
diff --git a/apps/client/src/print.spec.tsx b/apps/client/src/print.spec.tsxindex 171437ca98..7601a60a54 100644--- a/apps/client/src/print.spec.tsx+++ b/apps/client/src/print.spec.tsx@@ -24,7 +24,7 @@ vi.mock("./widgets/collections/NoteList", () => ({}}));-import { App, Error404, loadCustomCss, main, SingleNoteRenderer } from "./print";+import { App, applyPrintDocumentTitle, Error404, loadCustomCss, main, SingleNoteRenderer } from "./print";import froca from "./services/froca";import { buildNote } from "./test/easy-froca";@@ -225,6 +225,7 @@ describe("main", () => {it("injects @page margins (browser) plus the font link and renders the app", async () => {buildNote({ id: "main-note", title: "Main Note", type: "text" });window.location.hash = "main-note";+ document.title = "Trilium Notes";await main();@@ -232,6 +233,9 @@ describe("main", () => {expect(document.head.querySelector('link[href="api/fonts"]')).toBeTruthy();const titles = [...document.body.querySelectorAll("h1")].map((el) => el.textContent);expect(titles).toContain("Main Note");+ // Regression (#11140): the exported PDF's metadata title follows the note, not the+ // app shell default the print window inherits.+ expect(document.title).toBe("Main Note");});it("skips @page injection under Electron", async () => {@@ -246,3 +250,21 @@ describe("main", () => {expect(document.body.querySelectorAll("h1").length).toBeGreaterThan(0);});});++describe("applyPrintDocumentTitle", () => {+ const originalTitle = document.title;++ afterEach(() => {+ document.title = originalTitle;+ });++ it("keeps the inherited title when no note loaded", () => {+ document.title = "Trilium Notes";++ applyPrintDocumentTitle(null);+ applyPrintDocumentTitle(undefined);+ applyPrintDocumentTitle({ title: "" });++ expect(document.title).toBe("Trilium Notes");+ });+});diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsxindex 3c7ee275dc..fcb5040d8f 100644--- a/apps/client/src/print.tsx+++ b/apps/client/src/print.tsx@@ -49,11 +49,24 @@ export async function main() {const note = await froca.getNote(noteId);+ applyPrintDocumentTitle(note);+const bodyWrapper = document.createElement("div");render(<App note={note} noteId={noteId} />, bodyWrapper);document.body.appendChild(bodyWrapper);}+/**+ * Why (#11140): `printToPDF()` and the browser print dialog take the exported document's+ * metadata title from `document.title`; the print window inherits the app shell's+ * "Trilium Notes" default, so every export announced itself as that instead of the note.+ */+export function applyPrintDocumentTitle(note: { title: string } | null | undefined): void {+ if (note?.title) {+ document.title = note.title;+ }+}+export function App({ note, noteId }: { note: FNote | null | undefined, noteId: string }) {const sentReadyEvent = useRef(false);const onProgressChanged = useCallback((progress: number) => {