Skip to content

Commit

Permalink
fix: allow tables with no columns[number]['slice'] values
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Mar 5, 2024
1 parent fb386d9 commit 54a4897
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
18 changes: 11 additions & 7 deletions packages/@atjson/renderer-commonmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
23 changes: 14 additions & 9 deletions packages/@atjson/renderer-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,23 @@ export default class HTMLRenderer extends Renderer {
header += "<thead><tr>";
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 += `<th${
textAlign ? ` style="text-align: ${textAlign};"` : ""
}>${this.render(slice)}</th>`;
}>${headerText}</th>`;
}

header += "</tr></thead>";
Expand Down
8 changes: 4 additions & 4 deletions packages/@atjson/renderer-html/test/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 54a4897

Please sign in to comment.