Skip to content

Commit

Permalink
Merge pull request #2 from yaycmyk/implement-table-column-alignment
Browse files Browse the repository at this point in the history
GFM Aligned Column functionality
  • Loading branch information
Evan Jacobs committed Dec 8, 2015
2 parents 0ca3334 + e4c1e5d commit 79958b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# markdown-to-jsx
## Changelog

### 1.1.0 (December 8, 2015)

- Implement GFM table per-column alignment (d2ab598eaa7b2effc21befc93024ae4eecd1b3d9)

### 1.0.1 (December 7, 2015)

Update package.json metadata.
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ converter('# Hello world[^2]!\n\n[^2]: A beautiful place.', {footnotes: true});
- mdast's handling of lists will sometimes add a child paragraph tag inside the
`<li>` where it shouldn't exist - [Bug Ticket](https://github.com/wooorm/mdast/issues/104)

- mdast does not currently have support for column-specific alignment in GFM tables -
[Bug Ticket](https://github.com/wooorm/mdast/issues/105)

- mdast incorrectly parses footnote definitions with only one word - [Bug Ticket](https://github.com/wooorm/mdast/issues/106)

MIT
4 changes: 2 additions & 2 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ describe('markdown-to-jsx', () => {
expect(row.children[0].tagName).toBe('TD');
});

/* disabled until https://github.com/wooorm/mdast/issues/105 is resolved */
xit('should handle a table with aligned columns', () => {
it('should handle a table with aligned columns', () => {
const element = render(converter('foo|bar\n-:|-\n1|2'));
const elementNode = ReactDOM.findDOMNode(element);
const table = elementNode.querySelector('table');
Expand All @@ -384,6 +383,7 @@ describe('markdown-to-jsx', () => {
expect(thead).not.toBe(null);
expect(thead.children.length).toBe(2);
expect(thead.children[0].tagName).toBe('TH');
expect(thead.children[0].style.textAlign).toBe('right');
expect(row).not.toBe(null);
expect(row.children.length).toBe(2);
expect(row.children[0].tagName).toBe('TD');
Expand Down
39 changes: 34 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,38 @@ function formExtraPropsForHTMLNodeType(props = {}, ast) {
start: ast.start,
};

case 'table':
case 'tableCell':
case 'th':
return {
...props,
style: {align: ast.align},
style: {textAlign: ast.align},
};
}

return props;
}

function seekCellsAndAlignThemIfNecessary(root, alignmentValues) {
const mapper = (child, index) => {
if (child.type === 'tableCell') {
return {
...child,
align: alignmentValues[index],
};
} else if (Array.isArray(child.children) && child.children.length) {
return child.children.map(mapper);
}

return child;
};

if (Array.isArray(root.children) && root.children.length) {
root.children = root.children.map(mapper);
}

return root;
}

function astToJSX(ast, index) { /* `this` is the dictionary of definitions */
if (textTypes.indexOf(ast.type) !== -1) {
return ast.value;
Expand Down Expand Up @@ -167,16 +189,23 @@ function astToJSX(ast, index) { /* `this` is the dictionary of definitions */

ast.children = ast.children.reduce((children, child) => {
if (child.type === 'tableHeader') {
children.unshift(child);
children.unshift(
seekCellsAndAlignThemIfNecessary(child, ast.align)
);
} else if (child.type === 'tableRow') {
tbody.children.push(child);
tbody.children.push(
seekCellsAndAlignThemIfNecessary(child, ast.align)
);
} else if (child.type === 'tableFooter') {
children.push(child);
children.push(
seekCellsAndAlignThemIfNecessary(child, ast.align)
);
}

return children;

}, [tbody]);

} /* React yells if things aren't in the proper structure, so need to
delve into the immediate children and wrap tablerow(s) in a tbody */

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "markdown-to-jsx",
"description": "Interprets markdown text and outputs a JSX equivalent.",
"license": "MIT",
"version": "1.0.1",
"version": "1.1.0",
"keywords": [
"markdown",
"react",
Expand Down

0 comments on commit 79958b2

Please sign in to comment.