Skip to content

Commit

Permalink
improve table parsing (Rebilly#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weetbix authored Aug 2, 2023
1 parent 831c383 commit 21825dc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
13 changes: 12 additions & 1 deletion dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion src/readability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,34 @@ More text after the list.
);

expect(stripped).toMatchInlineSnapshot(`
"Chance of processing a transaction.
"
Chance of processing a transaction.
"
`);
});

it('should convert tables without compressing surrounding text', () => {
const stripped = preprocessMarkdown(
`
### Some heading text here
Some content text.
|A table header here|A table header here|
|---|---|
|Cell contents goes here|Cell contents goes here|
This content comes after the table
`
);

expect(stripped).toMatchInlineSnapshot(`
"Some content text.
A table header here.
A table header here.
Cell contents goes here.
Cell contents goes here.
This content comes after the table "
`);
});

Expand Down
16 changes: 15 additions & 1 deletion src/readability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const replaceNodesWithTheirTextContent: Plugin = () => (tree) => {
'linkReference',
'table',
'tableRow',
'tableCell',
];

visit(tree, nodeTypesToReplace, (node, index, parent) => {
Expand Down Expand Up @@ -193,6 +192,21 @@ const convertTableToText: Plugin = () => (tree) => {
tableCellNode.children = [];
}
});

// Encapsulate with a paragraph, replacing the table cell
visit(tree, 'tableCell', (node, index, parent) => {
const newNode = {
type: 'paragraph',
// @ts-ignore
children: node.children,
};

// @ts-ignore
parent?.children.splice(index, 1, newNode);

// Do not traverse `node`, continue at the node *now* at `index`.
return [SKIP, index];
});
};

// Remark plugin to remove list items that have less than 4 words.
Expand Down

0 comments on commit 21825dc

Please sign in to comment.