Skip to content

Commit

Permalink
fix: handle duplicate columns and tables with no header
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Jul 23, 2024
1 parent c563e60 commit 5594128
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 172 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-config-prettier": "8.8.0",
"eslint-plugin-jest": "27.2.1",
"eslint-plugin-prettier": "4.2.1",
"hexoid": "^1.0.0",
"husky": "8.0.3",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand Down
18 changes: 18 additions & 0 deletions packages/@atjson/offset-annotations/src/annotations/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,26 @@ export class Table extends BlockAnnotation<{
* them from rendering.
*/
columns: Array<{
/**
* `name` corresponds to the fields on the schema of the corresponding DataSet
*/
name: string;

/**
* `plaintextName`, if present, is a stringified version of the displayed column header text
*/
plaintextName?: string;

/**
* `slice` is a reference to the slice of the document to be rendered as the column header
*/
slice?: string;

/**
* `textAlign` is the intended alignment of all text in this column
* This corresponds to the behavior of tables in markdown, where
* text alignment can only be set at the column level
*/
textAlign?: "left" | "right" | "center";
}>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hexoid from "hexoid";
import {
JSON,
SliceAnnotation,
Expand All @@ -8,6 +9,8 @@ import {

import OffsetSource, { DataSet, Table, ColumnType } from "../index";

const generateId = hexoid(8);

function extractPlainContents(
doc: OffsetSource,
annotation: { start: number; end: number }
Expand Down Expand Up @@ -80,15 +83,27 @@ function extractAlignment(tableCell: Annotation<{ style: string }>) {
return undefined;
}

function generateFieldName(
columnName: string | null,
index: number,
tableIdentifier: string
) {
return (
(columnName?.length ? columnName : `column ${index + 1}`) +
`(${tableIdentifier}-${generateId()})`
);
}

export function convertHTMLTablesToDataSet(
doc: OffsetSource,
vendor: string
): void {
): Table[] {
let convertedTables: Table[] = [];
doc.where({ type: `-${vendor}-table` }).forEach((table) => {
if (isInvalidTable(doc, table, vendor)) {
return;
}

let tableIdentifier = generateId();
let dataSetSchemaEntries: [name: string, type: ColumnType][] = [];
let columnConfigs: Exclude<Table["attributes"]["columns"], undefined> = [];
let dataRows: Record<string, { slice: string; jsonValue: JSON }>[] = [];
Expand Down Expand Up @@ -120,16 +135,15 @@ export function convertHTMLTablesToDataSet(
});
doc.replaceAnnotation(headCell, slice);
let columnName = extractPlainContents(doc, slice);
let fieldName = generateFieldName(columnName, index, tableIdentifier);

dataSetSchemaEntries.push([
columnName.length ? columnName : `column ${index + 1}`,
ColumnType.RICH_TEXT,
]);
dataSetSchemaEntries.push([fieldName, ColumnType.RICH_TEXT]);

slices.push(slice);

let columnConfig: (typeof columnConfigs)[number] = {
name: columnName,
name: fieldName,
plaintextName: columnName,
slice: slice.id,
};

Expand Down Expand Up @@ -174,7 +188,7 @@ export function convertHTMLTablesToDataSet(
}

if (!hasColumnHeaders) {
let columnName = `column ${index + 1}`;
let columnName = generateFieldName(null, index, tableIdentifier);

let columnConfig: (typeof columnConfigs)[number] = {
name: columnName,
Expand Down Expand Up @@ -234,8 +248,12 @@ export function convertHTMLTablesToDataSet(
slices.forEach((slice) => slice.attributes.refs.push(dataSet.id));

doc.replaceAnnotation(table, dataSet, offsetTable);

convertedTables.push(offsetTable);
});

doc.where({ type: `-${vendor}-thead` }).remove();
doc.where({ type: `-${vendor}-tbody` }).remove();

return convertedTables;
}
15 changes: 14 additions & 1 deletion packages/@atjson/source-commonmark/src/converter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,20 @@ CommonmarkSource.defineConverterTo(
.set({ type: "-offset-paragraph" });
doc.where({ type: "-commonmark-strong" }).set({ type: "-offset-bold" });

convertHTMLTablesToDataSet(doc, "commonmark");
convertHTMLTablesToDataSet(doc, "commonmark").forEach((table) => {
/**
* if a markdown table has no text in its column headers, we have decided to
* store that as a table with no header section at all
*/
if (
table.attributes.columns.every(
(column) =>
column.plaintextName === "" || column.plaintextName == null
)
) {
table.attributes.showColumnHeaders = false;
}
});

return doc;
}
Expand Down
Loading

0 comments on commit 5594128

Please sign in to comment.