Skip to content

Build your personal knowledge base with Trilium Notes

perf(spreadsheet): render the parsed workbook instead of its JSON

Elian Doranc0e82b71ce4c
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
index e5a779057d..90b3ef04b3 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -39,6 +39,28 @@ function withInlineStyles(html: string): string {
import { BorderStyle, HorizontalAlign, WrapStrategy } from "./workbook_model.js";
describe("renderSpreadsheetToHtml", () => {
+ it("accepts an already-parsed workbook as well as its JSON", () => {
+ // The XLSX preview holds the workbook `parseXlsxToWorkbook` returns, so it renders it
+ // directly instead of serializing it for the renderer to parse back.
+ const data = {
+ version: 1,
+ workbook: {
+ sheetOrder: ["sheet1"],
+ styles: { boldStyle: { bl: 1 } },
+ sheets: {
+ sheet1: {
+ id: "sheet1",
+ name: "Sheet1",
+ cellData: { 0: { 0: { v: "Hello", t: 1, s: "boldStyle" } } }
+ }
+ }
+ }
+ };
+
+ expect(renderRaw(data)).toBe(renderRaw(JSON.stringify(data)));
+ expect(renderRaw(data)).toContain("Hello");
+ });
+
it("renders a basic spreadsheet with values and styles", () => {
const input = JSON.stringify({
version: 1,
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 6849eb7ab2..24470dcc08 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -34,18 +34,19 @@ import {
type ITextRotation,
type IWorksheetData,
parseWorkbookData,
+ type PersistedData,
resolveCellStyle,
VerticalAlign,
WrapStrategy
} from "./workbook_model.js";
/**
- * Parses the raw JSON content of a spreadsheet note and renders it as HTML.
- * 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.
+ * Renders a spreadsheet note as HTML, from either its raw JSON content or an already-parsed
+ * 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(jsonContent: string): string {
- const { ok, data } = parseWorkbookData(jsonContent);
+export function renderSpreadsheetToHtml(content: string | PersistedData): string {
+ const { ok, data } = parseWorkbookData(content);
if (!ok) {
return "<p>Unable to parse spreadsheet data.</p>";
}
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts b/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
index c82fd2b298..56ac5b8898 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
@@ -7,6 +7,8 @@ import {
getFloatingDrawings,
type IWorkbookData,
normalizeDataStream,
+ parseWorkbookData,
+ type PersistedData,
SHEET_DATA_VALIDATION_RESOURCE,
SHEET_DRAWING_RESOURCE
} from "./workbook_model.js";
@@ -16,6 +18,27 @@ function workbookWithResource(name: string, data: string): IWorkbookData {
return { sheetOrder: ["sheet1"], sheets: {}, resources: [{ name, data }] };
}
+describe("parseWorkbookData", () => {
+ it("parses a JSON string and reports unparseable content", () => {
+ expect(parseWorkbookData('{"version":1}')).toEqual({ ok: true, data: { version: 1 } });
+ // Valid JSON that holds no workbook still parses; only broken JSON is `ok: false`.
+ expect(parseWorkbookData("null")).toEqual({ ok: true, data: null });
+ expect(parseWorkbookData("{")).toEqual({ ok: false, data: null });
+ });
+
+ it("passes an already-parsed workbook through without serializing it", () => {
+ const data: PersistedData = {
+ version: 1,
+ workbook: { sheetOrder: ["sheet1"], sheets: { sheet1: { id: "sheet1", name: "Sheet1", cellData: {} } } }
+ };
+
+ const result = parseWorkbookData(data);
+
+ expect(result.ok).toBe(true);
+ expect(result.data).toBe(data);
+ });
+});
+
describe("getFloatingDrawings", () => {
it("returns the sheet's drawings in their stored z-order", () => {
const workbook = workbookWithResource(SHEET_DRAWING_RESOURCE, JSON.stringify({
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index 8bdd706cef..d2ea37d875 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -256,15 +256,22 @@ export interface WorkbookParseResult {
}
/**
- * Parses the raw JSON content of a spreadsheet note. `ok` is `false` only when the content
- * is not valid JSON; valid-but-empty JSON (e.g. `null`, `{}`) returns `ok: true` with the
- * parsed value, so callers can distinguish an unparseable payload from a structurally empty
- * one. Callers should additionally check `data.workbook?.sheets` and report an empty/invalid
- * workbook in their own format.
+ * Reads the content of a spreadsheet note. `ok` is `false` only when a string is not valid
+ * JSON; valid-but-empty JSON (e.g. `null`, `{}`) returns `ok: true` with the parsed value, so
+ * callers can distinguish an unparseable payload from a structurally empty one. Callers should
+ * additionally check `data.workbook?.sheets` and report an empty/invalid workbook in their own
+ * format.
+ *
+ * Already-parsed data is passed through, so a caller holding a workbook — the XLSX preview,
+ * which gets one from `parseXlsxToWorkbook` — does not serialize it just to have it parsed back.
*/
-export function parseWorkbookData(jsonContent: string): WorkbookParseResult {
+export function parseWorkbookData(content: string | PersistedData): WorkbookParseResult {
+ if (typeof content !== "string") {
+ return { ok: true, data: content };
+ }
+
try {
- return { ok: true, data: JSON.parse(jsonContent) };
+ return { ok: true, data: JSON.parse(content) };
} catch {
return { ok: false, data: null };
}
diff --git a/packages/trilium-core/src/services/office_preview.ts b/packages/trilium-core/src/services/office_preview.ts
index 4419ab61a0..4670d222ee 100644
--- a/packages/trilium-core/src/services/office_preview.ts
+++ b/packages/trilium-core/src/services/office_preview.ts
@@ -68,7 +68,8 @@ export async function convertOfficeToHtml(content: string | Uint8Array, mime: st
/**
* Renders an `.xlsx` workbook through the native spreadsheet pipeline: the same
- * parse-to-Univer-JSON step the importer uses, then the shared/print HTML renderer.
+ * parse-to-Univer-JSON step the importer uses, then the shared/print HTML renderer, which
+ * takes the parsed workbook directly.
* Dynamically imported so exceljs only loads when an `.xlsx` is actually previewed,
* keeping it out of the core barrel (and the standalone/browser bundle).
*/
@@ -80,7 +81,5 @@ async function convertXlsxToHtml(buffer: Uint8Array): Promise<string> {
const workbook = await parseXlsxToWorkbook(buffer);
- // The renderer consumes the persisted note-content shape, which is the stringified
- // parse result (see importSpreadsheet in the import service).
- return renderSpreadsheetToHtml(JSON.stringify(workbook));
+ return renderSpreadsheetToHtml(workbook);
}