Skip to content

Commit

Permalink
fix: refactor conversion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-Alexa Robinson committed Feb 28, 2024
1 parent ee33a5b commit 1ca7670
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
SliceAnnotation,
compareAnnotations,
} from "@atjson/document";
import OffsetSource, {
DataSet,
Table,
ColumnType,
} from "@atjson/offset-annotations";

export function convertTables(doc: OffsetSource): void {
doc.where({ type: "-commonmark-table" }).forEach((table) => {
import OffsetSource, { DataSet, Table, ColumnType } from "../index";

export function convertHTMLTablesToDataSet(
doc: OffsetSource,
vendor: string
): void {
doc.where({ type: `-${vendor}-table` }).forEach((table) => {
let dataColumnHeaders: {
name: string;
slice: string;
Expand All @@ -25,6 +24,7 @@ export function convertTables(doc: OffsetSource): void {
doc
.where(
(annotation) =>
annotation.vendorPrefix === vendor &&
annotation.type === "th" &&
annotation.start >= table.start &&
annotation.end <= table.end
Expand Down Expand Up @@ -69,6 +69,7 @@ export function convertTables(doc: OffsetSource): void {

let tableRows = doc.where(
(annotation) =>
annotation.vendorPrefix === vendor &&
annotation.type === "tr" &&
annotation.start >= table.start &&
annotation.end <= table.end
Expand All @@ -81,6 +82,7 @@ export function convertTables(doc: OffsetSource): void {
doc
.where(
(annotation) =>
annotation.vendorPrefix === vendor &&
annotation.type === "td" &&
annotation.start >= row.start &&
annotation.end <= row.end
Expand Down Expand Up @@ -148,6 +150,6 @@ export function convertTables(doc: OffsetSource): void {
doc.replaceAnnotation(table, dataSet, dataSetSlice, offsetTable);
});

doc.where({ type: "-commonmark-thead" }).remove();
doc.where({ type: "-commonmark-tbody" }).remove();
doc.where({ type: `-${vendor}-thead` }).remove();
doc.where({ type: `-${vendor}-tbody` }).remove();
}
8 changes: 7 additions & 1 deletion packages/@atjson/offset-annotations/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getClosestAspectRatio } from "./aspect-ratios";
import * as SocialURLs from "./social-urls";
import * as VideoURLs from "./video-urls";
import { convertHTMLTablesToDataSet } from "./convert-html-tables";

export { getClosestAspectRatio, SocialURLs, VideoURLs };
export {
convertHTMLTablesToDataSet,
getClosestAspectRatio,
SocialURLs,
VideoURLs,
};
9 changes: 6 additions & 3 deletions packages/@atjson/source-commonmark/src/converter/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import OffsetSource, { CodeBlock, Image } from "@atjson/offset-annotations";
import OffsetSource, {
CodeBlock,
Image,
convertHTMLTablesToDataSet,
} from "@atjson/offset-annotations";
import CommonmarkSource from "../source";
import { convertTables } from "./tables";

CommonmarkSource.defineConverterTo(
OffsetSource,
Expand Down Expand Up @@ -90,7 +93,7 @@ CommonmarkSource.defineConverterTo(
.set({ type: "-offset-paragraph" });
doc.where({ type: "-commonmark-strong" }).set({ type: "-offset-bold" });

convertTables(doc);
convertHTMLTablesToDataSet(doc, "commonmark");

return doc;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/@atjson/source-html/src/converter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import OffsetSource, {
CodeBlock,
List,
IframeEmbed,
convertHTMLTablesToDataSet,
} from "@atjson/offset-annotations";
import { OrderedList } from "../annotations";
import HTMLSource from "../source";
Expand Down Expand Up @@ -174,5 +175,7 @@ HTMLSource.defineConverterTo(OffsetSource, function HTMLToOffset(doc) {

doc.where(isSmallCaps).set({ type: "-offset-small-caps", attributes: {} });

convertHTMLTablesToDataSet(doc, "html");

return doc;
});
5 changes: 5 additions & 0 deletions packages/@atjson/source-html/test/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1419,5 +1419,10 @@ describe("@atjson/source-html", () => {
`);
});
});

describe("tables", () => {
test.todo("commonmark-like tables convert correctly");
test.todo("non-commonmark tables are correctly dropped");
});
});
});

0 comments on commit 1ca7670

Please sign in to comment.