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
perf(office): render only the corner of a workbook for a card preview
Elian Doran7392f5a125a0
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex bfa61f92d6..d990a3b970 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -406,7 +406,7 @@ describe("getRenderedContent office rendering", () => {note.mime = DOCX;const { type, $renderedContent } = await getRenderedContent(note);expect(type).toBe("office");- expect(renderOfficeToHtml).toHaveBeenCalledWith("notes", note.noteId);+ expect(renderOfficeToHtml).toHaveBeenCalledWith("notes", note.noteId, { trim: undefined });// the padded body sits inside a dedicated, unpadded scroll hostexpect($renderedContent.find(".office-preview-scroll > .office-preview-body").html()).toContain("converted");// the file remains downloadable / openable@@ -452,7 +452,7 @@ describe("getRenderedContent office rendering", () => {const att = buildAttachment({ role: "file", mime: "application/vnd.oasis.opendocument.spreadsheet" });const { type, $renderedContent } = await getRenderedContent(att);expect(type).toBe("office");- expect(renderOfficeToHtml).toHaveBeenCalledWith("attachments", att.attachmentId);+ expect(renderOfficeToHtml).toHaveBeenCalledWith("attachments", att.attachmentId, { trim: undefined });expect($renderedContent.find(".file-footer").length).toBe(0);});});diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 34abb4b7fa..f04f86f3bb 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -108,7 +108,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 && type === "office") {- await renderOffice(entity, $renderedContent);+ await renderOffice(entity, $renderedContent, options);} else if (!options.tooltip && ["file", "pdf", "audio", "video"].includes(type)) {await renderFile(entity, type, $renderedContent, options);} else if (type === "mermaid") {@@ -348,9 +348,9 @@ async function renderFile(entity: FNote | FAttachment, type: string, $renderedCo* Renders an inline preview of an office document (DOCX/XLSX/PPTX, ODT/ODS/ODP, RTF and* EPUB) by fetching the server-rendered HTML preview and sanitizing it. On failure it* falls back to a notice plus the usual download/open actions, so the file is never left- * unreachable.+ * unreachable. `options.trim` asks for the corner of a workbook rather than all of it.*/-async function renderOffice(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>) {+async function renderOffice(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>, options: RenderOptions) {const { entityType, entityId } = getEntityTypeAndId(entity);// The scroll host is a separate, unpadded element (like the note view's .scrolling-container)@@ -364,7 +364,9 @@ async function renderOffice(entity: FNote | FAttachment, $renderedContent: JQuer$renderedContent.append($content);try {- const { css, html } = await renderOfficeToHtml(entityType, entityId);+ // A note list asks for a trimmed render: a card shows a few rows, and a workbook that+ // fills one runs to megabytes the browser would parse and lay out to display none of.+ const { css, html } = await renderOfficeToHtml(entityType, entityId, { trim: options.trim });$body.html(html);if (css) {// Built as an element with its text set, never parsed as markup, so a cell's stylingdiff --git a/apps/client/src/services/office_renderer.spec.ts b/apps/client/src/services/office_renderer.spec.tsindex dfe7f447c0..0d46717603 100644--- a/apps/client/src/services/office_renderer.spec.ts+++ b/apps/client/src/services/office_renderer.spec.ts@@ -118,4 +118,13 @@ describe("renderOfficeToHtml", () => {expect(html).not.toContain("#000080");expect(html).toContain('<a href="#a">x</a>');});++ it("asks the route to trim when the caller wants a card-sized preview", async () => {+ await renderOfficeToHtml("notes", "n1", { trim: true });+ expect(serverGet).toHaveBeenCalledWith("notes/n1/office-preview?trim=1", undefined, true);++ // Left off otherwise, so the whole document and the corner never share a cache entry.+ await renderOfficeToHtml("notes", "n1");+ expect(serverGet).toHaveBeenLastCalledWith("notes/n1/office-preview", undefined, true);+ });});diff --git a/apps/client/src/services/office_renderer.ts b/apps/client/src/services/office_renderer.tsindex bc6ea2a88c..7bd77f8a82 100644--- a/apps/client/src/services/office_renderer.ts+++ b/apps/client/src/services/office_renderer.ts@@ -17,9 +17,14 @@ import server from "./server.js";* Throws if the document is too large, unsupported, or conversion fails — callers should* catch and fall back to the usual download / open-externally affordance.*/-export async function renderOfficeToHtml(entityType: "notes" | "attachments", entityId: string): Promise<OfficePreview> {+export async function renderOfficeToHtml(+ entityType: "notes" | "attachments",+ entityId: string,+ { trim }: { trim?: boolean } = {}+): Promise<OfficePreview> {// `raw` keeps the response a plain string; the route sends the fragment as the body.- const body = await server.get<string>(`${entityType}/${entityId}/office-preview`, undefined, true);+ const url = `${entityType}/${entityId}/office-preview${trim ? "?trim=1" : ""}`;+ const body = await server.get<string>(url, undefined, true);const { css, html } = splitStylesheet(body);const sanitized = sanitizeNoteContentHtml(html);diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 59fae026aa..6d682a9990 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -2791,3 +2791,72 @@ describe("reuse of a style across cells", () => {.toEqual(["&", "<", ">", """, "'"]);});});++describe("trimmed rendering", () => {+ const bigWorkbook = () => {+ const cellData: Record<string, Record<string, unknown>> = {};+ for (let row = 0; row < 60; row++) {+ cellData[row] = {};+ for (let col = 0; col < 40; col++) cellData[row][col] = { v: `r${row}c${col}`, t: 1 };+ }+ return JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1", "s2"],+ styles: {},+ sheets: {+ s1: {+ id: "s1", name: "First", hidden: 0, rowCount: 100, columnCount: 50,+ mergeData: [], cellData, rowData: {}, columnData: {}+ },+ s2: {+ id: "s2", name: "Second", hidden: 0, rowCount: 10, columnCount: 5,+ mergeData: [], cellData: { "0": { "0": { v: "elsewhere", t: 1 } } },+ rowData: {}, columnData: {}+ }+ }+ }+ });+ };++ it("keeps the corner of the first sheet and drops the rest", () => {+ const full = renderRaw(bigWorkbook());+ const trimmed = renderRaw(bigWorkbook(), { trim: true });++ // 60x40 plus the second sheet's one cell, down to the 20x15 corner a card has room for.+ expect((full.match(/<td/g) ?? []).length).toBe(60 * 40 + 1);+ expect((trimmed.match(/<td/g) ?? []).length).toBe(20 * 15);+ expect(trimmed).toContain("r0c0");+ expect(trimmed).toContain("r19c14");+ expect(trimmed).not.toContain("r20c0");+ expect(trimmed).not.toContain("r0c15");+ expect(trimmed.length).toBeLessThan(full.length / 5);+ });++ it("renders only the first visible sheet, without its heading", () => {+ const trimmed = renderRaw(bigWorkbook(), { trim: true });++ expect(trimmed).not.toContain("elsewhere");+ // One sheet needs no heading to tell it apart.+ expect(trimmed).not.toContain("<h3>");+ expect((trimmed.match(/<table/g) ?? []).length).toBe(1);+ });++ it("leaves a sheet smaller than the corner alone", () => {+ const small = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"], styles: {},+ sheets: {+ s1: {+ id: "s1", name: "S", hidden: 0, rowCount: 10, columnCount: 5,+ mergeData: [], cellData: { "0": { "0": { v: "a", t: 1 }, "1": { v: "b", t: 1 } } },+ rowData: {}, columnData: {}+ }+ }+ }+ });++ expect(renderRaw(small, { trim: true })).toBe(renderRaw(small));+ });+});diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.tsindex e70ed3025b..24234d1e6a 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts@@ -51,7 +51,7 @@ import {* workbook. Returns an HTML string containing one `<table>` per visible sheet, preceded by a* `<style>` holding the cell styling (see {@link StyleClasses}) when the workbook has any.*/-export function renderSpreadsheetToHtml(content: string | PersistedData): string {+export function renderSpreadsheetToHtml(content: string | PersistedData, options: SpreadsheetRenderOptions = {}): string {const { ok, data } = parseWorkbookData(content);if (!ok) {return "<p>Unable to parse spreadsheet data.</p>";@@ -68,25 +68,43 @@ export function renderSpreadsheetToHtml(content: string | PersistedData): stringreturn "<p>Empty spreadsheet.</p>";}+ // A trimmed render answers a card, which shows a few rows of one sheet: the rest is weight+ // nobody sees. Floating images are left out with it — they are positioned in absolute pixels+ // from A1, so one anchored past the corner would hang off a grid that no longer reaches it.+ const sheets = options.trim ? visibleSheets.slice(0, 1) : visibleSheets;+const classes = new StyleClasses();const parts: string[] = [];- for (const sheet of visibleSheets) {- if (visibleSheets.length > 1) {+ for (const sheet of sheets) {+ if (sheets.length > 1) {parts.push(`<h3>${escapeHtml(sheet.name)}</h3>`);}- const images = placeFloatingImages(sheet, getFloatingDrawings(workbook, sheet.id));- const table = renderSheet(sheet, workbook.styles ?? {}, images, classes);+ const images = options.trim ? [] : placeFloatingImages(sheet, getFloatingDrawings(workbook, sheet.id));+ const table = renderSheet(sheet, workbook.styles ?? {}, images, classes, options);parts.push(wrapWithFloatingImages(table, images));}// The stylesheet is only complete once every sheet has been rendered, so it is built last// and put in front, where a consumer that has to lift it out finds it without a parse.- const stylesheet = buildStylesheet(visibleSheets, classes);+ const stylesheet = buildStylesheet(sheets, classes);const body = parts.join("\n");return stylesheet ? `${stylesheet}\n${body}` : body;}+export interface SpreadsheetRenderOptions {+ /**+ * Renders only the top-left corner of the first visible sheet, for a preview shown in a card+ * or a list rather than opened. A workbook that fills a card runs to megabytes otherwise, of+ * which a card shows the first handful of rows.+ */+ trim?: boolean;+}++/** How much of a sheet a trimmed render keeps — comfortably more than a card has room for. */+const TRIMMED_ROWS = 20;+const TRIMMED_COLUMNS = 15;+/*** Builds the `<style>` a render is preceded by: the gridline rule when any sheet asks for* gridlines, then one rule per distinct style. Returns "" when there is nothing to write.@@ -373,7 +391,7 @@ function trackIndexAtPx(targetPx: number, count: number | undefined, size: (inde// #endregion-function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>, images: PlacedImage[], classes: StyleClasses): string {+function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>, images: PlacedImage[], classes: StyleClasses, options: SpreadsheetRenderOptions = {}): string {const { cellData, mergeData = [], columnData = {}, rowData = {} } = sheet;// A sheet often carries formatting far past its data: a fill applied to whole rows leaves tens@@ -385,7 +403,11 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |// pull the sheet back out to the far edge of the grid.const bounds = computeBounds(cellData, boundingMerges(mergeData, sheet), (cell) => holdsSomething(cell, styles))?? computeBounds(cellData, mergeData);- const { maxRow, maxCol } = extendBoundsForImages(sheet, bounds?.maxRow ?? -1, bounds?.maxCol ?? -1, images);+ const extended = extendBoundsForImages(sheet, bounds?.maxRow ?? -1, bounds?.maxCol ?? -1, images);+ // A trimmed render keeps the corner a card has room for. The cap applies last, so it bounds+ // whatever the data, a merge or an image asked for rather than competing with them.+ const maxRow = options.trim ? Math.min(extended.maxRow, TRIMMED_ROWS - 1) : extended.maxRow;+ const maxCol = options.trim ? Math.min(extended.maxCol, TRIMMED_COLUMNS - 1) : extended.maxCol;if (maxRow < 0 || maxCol < 0) {return "<p>Empty sheet.</p>";}diff --git a/packages/trilium-core/src/routes/api/files.spec.ts b/packages/trilium-core/src/routes/api/files.spec.tsindex 4323a956ac..812273807c 100644--- a/packages/trilium-core/src/routes/api/files.spec.ts+++ b/packages/trilium-core/src/routes/api/files.spec.ts@@ -382,6 +382,24 @@ describe("Files API (core)", () => {expect(res.body).toContain("Hello");});+ it("answers with the corner of a workbook when the caller asks to trim it", async () => {+ const wb = new ExcelJS.Workbook();+ const sheet = wb.addWorksheet("Data");+ for (let row = 1; row <= 60; row++) {+ for (let col = 1; col <= 40; col++) sheet.getCell(row, col).value = `r${row}c${col}`;+ }+ const noteId = await createXlsxNote(Buffer.from(await wb.xlsx.writeBuffer()));++ const whole = await api.get<string>(`/api/notes/${noteId}/office-preview`);+ const corner = await api.get<string>(`/api/notes/${noteId}/office-preview?trim=1`);++ expect(corner.status).toBe(200);+ expect((corner.body.match(/<td/g) ?? []).length).toBe(20 * 15);+ expect(corner.body).toContain("r1c1");+ expect(corner.body).not.toContain("r60c40");+ expect(corner.body.length).toBeLessThan(whole.body.length / 5);+ });+it("rejects an unsupported MIME type with 400", async () => {const { noteId } = await createTextNote(api, { content: "<p>not office</p>" });diff --git a/packages/trilium-core/src/routes/api/files.ts b/packages/trilium-core/src/routes/api/files.tsindex 7565b8e70c..af3600f707 100644--- a/packages/trilium-core/src/routes/api/files.ts+++ b/packages/trilium-core/src/routes/api/files.ts@@ -105,17 +105,29 @@ function updateAttachment(req: FileRequest<{ attachmentId: string }>) {* The fragment is sent as the response body rather than wrapped in a JSON object: a* spreadsheet preview runs to megabytes, and an envelope would escape every attribute* quote and make the client parse the whole document before it can use it.+ *+ * `?trim=1` answers with the corner of a workbook instead of the whole of it, for a caller+ * showing a card rather than the document.*/async function getNoteOfficePreview(req: Request<{ noteId: string }>) {const note = becca.getNoteOrThrow(req.params.noteId);- return await convertOfficeToHtml(note.getContent(), note.mime);+ return await convertOfficeToHtml(note.getContent(), note.mime, { trim: isTrimmed(req) });}async function getAttachmentOfficePreview(req: Request<{ attachmentId: string }>) {const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);- return await convertOfficeToHtml(attachment.getContent(), attachment.mime);+ return await convertOfficeToHtml(attachment.getContent(), attachment.mime, { trim: isTrimmed(req) });+}++/**+ * Whether the caller asked for the corner of the document rather than all of it, which a note+ * list does for every card it shows. The answer varies with the query, and so does the URL, so a+ * trimmed preview and a whole one never share a cache entry.+ */+function isTrimmed(req: Request): boolean {+ return req.query.trim === "1";}function openPartialInt(noteOrAttachment: BNote | BAttachment, req: Request, res: Response) {diff --git a/packages/trilium-core/src/services/office_preview.ts b/packages/trilium-core/src/services/office_preview.tsindex 4670d222ee..8120d5a101 100644--- a/packages/trilium-core/src/services/office_preview.ts+++ b/packages/trilium-core/src/services/office_preview.ts@@ -12,6 +12,11 @@ const MAX_OFFICE_PREVIEW_BYTES = 20 * 1024 * 1024;// MIME of an `.xlsx` upload (Office Open XML spreadsheet), previewed through the native// spreadsheet pipeline rather than officeparser.+export interface OfficePreviewOptions {+ /** Renders the corner of the first sheet only, for a preview shown in a card or a list. */+ trim?: boolean;+}+const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";/**@@ -24,8 +29,11 @@ const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.s* initial standalone bundle.** The result is NOT sanitized — the client must sanitize it before injecting into the DOM.+ *+ * `trim` reaches the spreadsheet pipeline only; the other formats convert whole, since none of+ * them turns a page of content into the megabytes a workbook can.*/-export async function convertOfficeToHtml(content: string | Uint8Array, mime: string): Promise<string> {+export async function convertOfficeToHtml(content: string | Uint8Array, mime: string, options: OfficePreviewOptions = {}): Promise<string> {if (!isOfficeMimeType(mime)) {throw new ValidationError(`MIME type '${mime}' is not a supported office format.`);}@@ -37,7 +45,7 @@ export async function convertOfficeToHtml(content: string | Uint8Array, mime: stif (mime === XLSX_MIME) {try {- return await convertXlsxToHtml(buffer);+ return await convertXlsxToHtml(buffer, options);} catch {// Not a workbook exceljs can read (e.g. a mislabeled or exotic file) —// fall through and let officeparser have a go at it.@@ -73,7 +81,7 @@ export async function convertOfficeToHtml(content: string | Uint8Array, mime: st* Dynamically imported so exceljs only loads when an `.xlsx` is actually previewed,* keeping it out of the core barrel (and the standalone/browser bundle).*/-async function convertXlsxToHtml(buffer: Uint8Array): Promise<string> {+async function convertXlsxToHtml(buffer: Uint8Array, options: OfficePreviewOptions): Promise<string> {const [{ parseXlsxToWorkbook }, { renderSpreadsheetToHtml }] = await Promise.all([import("@triliumnext/commons/src/lib/spreadsheet/parse_from_xlsx.js"),import("@triliumnext/commons/src/lib/spreadsheet/render_to_html.js")@@ -81,5 +89,5 @@ async function convertXlsxToHtml(buffer: Uint8Array): Promise<string> {const workbook = await parseXlsxToWorkbook(buffer);- return renderSpreadsheetToHtml(workbook);+ return renderSpreadsheetToHtml(workbook, options);}