Skip to content

Commit

Permalink
fix: remove extra slice covering the dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Feb 28, 2024
1 parent ee1c6b9 commit fad3309
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,19 @@ export function convertHTMLTablesToDataSet(
},
});

let dataSetSlice = new SliceAnnotation({
...dataSet,
id: undefined,
attributes: { refs: [] },
});

let offsetTable = new Table({
...table,
id: undefined,
attributes: { dataSet: dataSetSlice.id },
attributes: { dataSet: dataSet.id },
});

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

slices.forEach((slice) => slice.attributes.refs.push(dataSet.id));
dataSetSlice.attributes.refs.push(offsetTable.id);

doc.replaceAnnotation(table, dataSet, dataSetSlice, offsetTable);
doc.replaceAnnotation(table, dataSet, offsetTable);
});

doc.where({ type: `-${vendor}-thead` }).remove();
Expand Down
19 changes: 14 additions & 5 deletions packages/@atjson/renderer-commonmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,24 +742,33 @@ export default class CommonmarkRenderer extends Renderer {
return rawText.join("");
}

*Table(table: Block<Table>): Generator<void, string, string[]> {
*DataSet(): Generator<void, string, string[]> {
return "";
}

*Table(
table: Block<Table>,
context: Context
): Generator<void, string, string[]> {
if (this.state.inlineOnly) {
return "";
}

const previousState = this.state;
this.state.inlineOnly = true;

const dataSetSlice = this.getSlice(table.attributes.dataSet);
const dataSetAnnotation = dataSetSlice?.blocks.find(
(block) => block.type === "data-set"
const dataSetAnnotation = context.document.blocks.find(
(block) => block.id === table.attributes.dataSet
) as Block<DataSet> | undefined;

if (!dataSetAnnotation) {
/**
* invalid dataset ref
*/
return "";

throw new Error(
`table ${table.id} references nonexistent dataset ${table.attributes.dataSet}`
);
}

let text = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ exports[`tables with column headings 1`] = `
"columns": [
{
"name": "column 1",
"slice": "M00000001",
"slice": "M00000000",
"type": "peritext",
},
{
"name": "column 2",
"slice": "M00000002",
"slice": "M00000001",
"type": "peritext",
},
],
"rows": [
{
"column 1": {
"jsonValue": "data 1.1",
"slice": "M00000003",
"slice": "M00000002",
},
"column 2": {
"jsonValue": "data 1.2",
"slice": "M00000004",
"slice": "M00000003",
},
},
],
Expand All @@ -47,7 +47,7 @@ exports[`tables with column headings 1`] = `
"textAlign": "right",
},
],
"dataSet": "M00000000",
"dataSet": "B00000000",
"showColumnHeaders": true,
},
"id": "B00000001",
Expand All @@ -60,50 +60,40 @@ exports[`tables with column headings 1`] = `
{
"attributes": {
"refs": [
"B00000001",
"B00000000",
],
},
"id": "M00000000",
"range": "(0..36]",
"type": "slice",
},
{
"attributes": {
"refs": [
"M00000000",
],
},
"id": "M00000001",
"range": "(1..9]",
"type": "slice",
},
{
"attributes": {
"refs": [
"M00000000",
"B00000000",
],
},
"id": "M00000002",
"id": "M00000001",
"range": "(10..18]",
"type": "slice",
},
{
"attributes": {
"refs": [
"M00000000",
"B00000000",
],
},
"id": "M00000003",
"id": "M00000002",
"range": "(19..27]",
"type": "slice",
},
{
"attributes": {
"refs": [
"M00000000",
"B00000000",
],
},
"id": "M00000004",
"id": "M00000003",
"range": "(28..36]",
"type": "slice",
},
Expand Down
7 changes: 1 addition & 6 deletions packages/@atjson/renderer-commonmark/test/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("tables", () => {
annotations: [
{
type: "-offset-data-set",
id: "dataSetId",
start: 0,
end: 35,
attributes: {
Expand All @@ -24,12 +25,6 @@ describe("tables", () => {
],
},
},
new SliceAnnotation({
id: "dataSetId",
start: 0,
end: 35,
attributes: { refs: ["tableId"] },
}),
new SliceAnnotation({
id: "column1id",
start: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ exports[`tables converting 1`] = `
"columns": [
{
"name": "name",
"slice": "M00000001",
"slice": "M00000000",
"type": "peritext",
},
{
"name": "age",
"slice": "M00000002",
"slice": "M00000001",
"type": "peritext",
},
],
"rows": [
{
"age": {
"jsonValue": "20",
"slice": "M00000004",
"slice": "M00000003",
},
"name": {
"jsonValue": "laios",
"slice": "M00000003",
"slice": "M00000002",
},
},
{
"age": {
"jsonValue": "500",
"slice": "M00000006",
"slice": "M00000005",
},
"name": {
"jsonValue": "marcille",
"slice": "M00000005",
"slice": "M00000004",
},
},
],
Expand All @@ -47,7 +47,7 @@ exports[`tables converting 1`] = `
},
{
"attributes": {
"dataSet": "M00000000",
"dataSet": "B00000000",
},
"id": "B00000001",
"parents": [
Expand All @@ -58,23 +58,13 @@ exports[`tables converting 1`] = `
},
],
"marks": [
{
"attributes": {
"refs": [
"B00000001",
],
},
"id": "M00000000",
"range": "(0..33]",
"type": "slice",
},
{
"attributes": {
"refs": [
"B00000000",
],
},
"id": "M00000001",
"id": "M00000000",
"range": "(3..7]",
"type": "slice",
},
Expand All @@ -84,7 +74,7 @@ exports[`tables converting 1`] = `
"B00000000",
],
},
"id": "M00000002",
"id": "M00000001",
"range": "(8..11]",
"type": "slice",
},
Expand All @@ -94,7 +84,7 @@ exports[`tables converting 1`] = `
"B00000000",
],
},
"id": "M00000003",
"id": "M00000002",
"range": "(12..17]",
"type": "slice",
},
Expand All @@ -104,7 +94,7 @@ exports[`tables converting 1`] = `
"B00000000",
],
},
"id": "M00000004",
"id": "M00000003",
"range": "(18..20]",
"type": "slice",
},
Expand All @@ -114,7 +104,7 @@ exports[`tables converting 1`] = `
"B00000000",
],
},
"id": "M00000005",
"id": "M00000004",
"range": "(21..29]",
"type": "slice",
},
Expand All @@ -124,7 +114,7 @@ exports[`tables converting 1`] = `
"B00000000",
],
},
"id": "M00000006",
"id": "M00000005",
"range": "(30..33]",
"type": "slice",
},
Expand Down

0 comments on commit fad3309

Please sign in to comment.