From 54a4897cbc22e4bcde6a3c66d3f2752a0e239fb7 Mon Sep 17 00:00:00 2001 From: Colin-Alexa Robinson Date: Tue, 5 Mar 2024 17:14:57 -0500 Subject: [PATCH] fix: allow tables with no columns[number]['slice'] values --- .../@atjson/renderer-commonmark/src/index.ts | 18 +++++++++------ packages/@atjson/renderer-html/src/index.ts | 23 +++++++++++-------- .../@atjson/renderer-html/test/table.test.ts | 8 +++---- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/packages/@atjson/renderer-commonmark/src/index.ts b/packages/@atjson/renderer-commonmark/src/index.ts index 6ef22c1a1..9a2e8d6d6 100644 --- a/packages/@atjson/renderer-commonmark/src/index.ts +++ b/packages/@atjson/renderer-commonmark/src/index.ts @@ -782,14 +782,18 @@ export default class CommonmarkRenderer extends Renderer { > = {}; for (let { name, slice: sliceId, textAlign } of table.attributes.columns) { - const slice = this.getSlice(sliceId); - if (!slice) { - throw new Error(`column heading slice not found ${sliceId}`); + let headerText = ""; + if (table.attributes.showColumnHeaders) { + if (sliceId) { + const slice = this.getSlice(sliceId); + if (!slice) { + throw new Error(`column heading slice not found ${sliceId}`); + } + headerText = this.render(slice); + } else { + headerText = name; + } } - - let headerText = table.attributes.showColumnHeaders - ? this.render(slice) - : ""; columns[name] = { header: headerText, rows: [], diff --git a/packages/@atjson/renderer-html/src/index.ts b/packages/@atjson/renderer-html/src/index.ts index 3db22c7ab..ae7bee1a3 100644 --- a/packages/@atjson/renderer-html/src/index.ts +++ b/packages/@atjson/renderer-html/src/index.ts @@ -287,18 +287,23 @@ export default class HTMLRenderer extends Renderer { header += ""; for (let { name, slice: sliceId, textAlign } of table.attributes .columns) { - let slice = this.getSlice(sliceId); - - if (!slice) { - throw new Error( - `Table ${table.id} ${ - table.range || "" - } could not find column heading slice for ${name} ${sliceId}` - ); + let headerText = name; + if (sliceId) { + const slice = this.getSlice(sliceId); + if (!slice) { + throw new Error( + `Table ${table.id} ${ + table.range || "" + } could not find column heading slice for ${name} ${sliceId}` + ); + } + + headerText = this.render(slice); } + header += `${this.render(slice)}`; + }>${headerText}`; } header += ""; diff --git a/packages/@atjson/renderer-html/test/table.test.ts b/packages/@atjson/renderer-html/test/table.test.ts index 9d722c024..d4d0f10a0 100644 --- a/packages/@atjson/renderer-html/test/table.test.ts +++ b/packages/@atjson/renderer-html/test/table.test.ts @@ -378,10 +378,10 @@ describe("tables", () => { test("no column headings", () => { let document = testTable({ columns: [ - { name: "name", slice: "M00000000" }, - { name: "age", slice: "M00000001" }, - { name: "job", slice: "M00000002" }, - { name: "notes", slice: "M00000005" }, + { name: "name" }, + { name: "age" }, + { name: "job" }, + { name: "notes" }, ], showColumnHeaders: false, });