Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Fixing issue where nested table parsing failed from null hAST values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gratcliff authored May 7, 2020
1 parent a8ce876 commit 0c7d2a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/markdown/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const React = require('react');
const BaseUrlContext = require('../contexts/BaseUrl');

const markdown = require('../index');
const { tableFlattening } = require('../processor/plugin/table-flattening');
const settings = require('../options.json');

test('image', () => {
Expand Down Expand Up @@ -215,6 +216,24 @@ describe('tree flattening', () => {
expect(table.children[0].value).toStrictEqual(' Col. B');
expect(table.children[1].value).toStrictEqual('Cell A1 Cell B1 Cell A2 Cell B2 Cell A3 ');
});

it('should not throw an error if missing values', () => {
const tree = {
tagName: 'table',
children: [
{
tagName: 'tHead',
},
{
tagName: 'tBody',
},
],
};

const [head, body] = tableFlattening(tree).children;
expect(head.value).toStrictEqual('');
expect(body.value).toStrictEqual('');
});
});

describe('export multiple Markdown renderers', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/markdown/processor/plugin/table-flattening.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ function transformer(ast) {
// This is necessary to pullout all the relevant strings

// Parse Header Values
const headerChildren = header.children[0].children;
const headerChildren = header.children && header.children.length ? header.children[0].children : [];
const headerValue = headerChildren.map(hc => (hc.children.length && hc.children[0].value) || '').join(' ');
// Parse Body Values
const bodyChildren = body.children.map(bc => bc.children).reduce((a, b) => a.concat(b), []);
const bodyChildren =
(body.children && body.children.map(bc => bc && bc.children).reduce((a, b) => a.concat(b), [])) || [];
const bodyValue = bodyChildren.map(bc => (bc.children.length && bc.children[0].value) || '').join(' ');

return [
Expand All @@ -37,3 +38,4 @@ function transformer(ast) {
}

module.exports = () => transformer;
module.exports.tableFlattening = transformer;

0 comments on commit 0c7d2a4

Please sign in to comment.