Skip to content

Commit

Permalink
fix: converting text alignment caused columns to disappear
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Feb 28, 2024
1 parent ab0136f commit 172df38
Show file tree
Hide file tree
Showing 7 changed files with 751 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function convertHTMLTablesToDataSet(
slice: string;
type: ColumnType;
}[] = [];
let columnConfigs: Table["attributes"]["columns"] = [];
let columnConfigs: Exclude<Table["attributes"]["columns"], undefined> = [];
let dataRows: Record<string, { slice: string; jsonValue: JSON }>[] = [];

let slices: SliceAnnotation[] = [];
Expand Down Expand Up @@ -45,7 +45,10 @@ export function convertHTMLTablesToDataSet(
attributes: { refs: [] },
});
doc.replaceAnnotation(headCell, slice);
let columnName = doc.content.slice(slice.start, slice.end).trim();
let columnName = doc.content
.slice(slice.start, slice.end)
.replace(/[^\p{Letter}\p{White_Space}]/gu, "")
.replace(/\s+/, " ");
dataColumnHeaders.push({
slice: slice.id,
name: columnName.length ? columnName : `column ${index + 1}`,
Expand All @@ -59,11 +62,15 @@ export function convertHTMLTablesToDataSet(
);

if (groups?.alignment) {
columnConfigs?.push({
columnConfigs.push({
name: columnName,
textAlign: groups.alignment as "left" | "right" | "center",
textAlign: groups?.alignment as "left" | "right" | "center",
});
} else {
columnConfigs.push({ name: columnName });
}
} else {
columnConfigs.push({ name: columnName });
}
});

Expand Down Expand Up @@ -134,7 +141,7 @@ export function convertHTMLTablesToDataSet(
attributes: { dataSet: dataSet.id },
});

if (columnConfigs?.length) {
if (columnConfigs.length) {
offsetTable.attributes.columns = columnConfigs;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tables with column headings center alignment 1`] = `
"| column 1 | column 2 |
|:--------:| -------- |
| data 1.1 | data 1.2 |
"
`;

exports[`tables with column headings mixed alignment 1`] = `
"| column 1 | column 2 |
|:-------- | --------:|
| data 1.1 | data 1.2 |
"
`;

exports[`tables with column headings no alignment 1`] = `
"| column 1 | column 2 |
| -------- | -------- |
| data 1.1 | data 1.2 |
"
`;

exports[`tables with column headings omitting columns 1`] = `
"| column 2 |
| -------- |
| data 1.2 |
"
`;

exports[`tables with column headings reordering columns 1`] = `
"| column 2 | column 1 |
| -------- | -------- |
| data 1.2 | data 1.1 |
"
`;

This file was deleted.

179 changes: 179 additions & 0 deletions packages/@atjson/renderer-commonmark/test/table.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { Block, deserialize } from "@atjson/document";
import OffsetSource, { Table } from "@atjson/offset-annotations";
import CommonmarkRenderer from "../src";

function testTable(tableAttributes: Block<Table>["attributes"]) {
return deserialize(
{
text: "column 1 column 2 data 1.1 data 1.2",
blocks: [
{
type: "table",
id: "tableId",
attributes: tableAttributes,
parents: [],
},
{
type: "data-set",
id: "dataSetId",
attributes: {
columns: [
{
name: "column 1",
slice: "column1Id",
type: "peritext",
},
{
name: "column 2",
slice: "column2Id",
type: "peritext",
},
],
rows: [
{
"column 1": {
slice: "cell1_1Id",
jsonValue: "data 1.1",
},
"column 2": {
slice: "cell1_2Id",
jsonValue: "data 1.2",
},
},
],
},
parents: ["table"],
},
],
marks: [
{
attributes: {
refs: ["dataSetId"],
},
id: "column1Id",
range: "(2..10]",
type: "slice",
},
{
attributes: {
refs: ["dataSetId"],
},
id: "column2Id",
range: "(11..19]",
type: "slice",
},
{
attributes: {
refs: ["dataSetId"],
},
id: "cell1_1Id",
range: "(20..28]",
type: "slice",
},
{
attributes: {
refs: ["dataSetId"],
},
id: "cell1_2Id",
range: "(29..37]",
type: "slice",
},
],
},
OffsetSource
);
}

describe("tables", () => {
describe("with column headings", () => {
test("no alignment", () => {
let document = testTable({
columns: [
{
name: "column 1",
},
{
name: "column 2",
},
],
dataSet: "dataSetId",
showColumnHeaders: true,
});

const markdown = CommonmarkRenderer.render(document);
expect(markdown).toMatchSnapshot();
});

test("mixed alignment", () => {
let document = testTable({
columns: [
{
name: "column 1",
textAlign: "left",
},
{
name: "column 2",
textAlign: "right",
},
],
dataSet: "dataSetId",
showColumnHeaders: true,
});

const markdown = CommonmarkRenderer.render(document);
expect(markdown).toMatchSnapshot();
});

test("center alignment", () => {
let document = testTable({
columns: [
{
name: "column 1",
textAlign: "center",
},
{
name: "column 2",
},
],
dataSet: "dataSetId",
showColumnHeaders: true,
});

const markdown = CommonmarkRenderer.render(document);
expect(markdown).toMatchSnapshot();
});

test("reordering columns", () => {
let document = testTable({
columns: [
{
name: "column 2",
},
{
name: "column 1",
},
],
dataSet: "dataSetId",
showColumnHeaders: true,
});

const markdown = CommonmarkRenderer.render(document);
expect(markdown).toMatchSnapshot();
});

test("omitting columns", () => {
let document = testTable({
columns: [
{
name: "column 2",
},
],
dataSet: "dataSetId",
showColumnHeaders: true,
});

const markdown = CommonmarkRenderer.render(document);
expect(markdown).toMatchSnapshot();
});
});
});
103 changes: 0 additions & 103 deletions packages/@atjson/renderer-commonmark/test/tables.test.ts

This file was deleted.

Loading

0 comments on commit 172df38

Please sign in to comment.