From d2ab598eaa7b2effc21befc93024ae4eecd1b3d9 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Tue, 8 Dec 2015 18:26:26 -0500 Subject: [PATCH 1/2] GFM Aligned Column functionality The align property on the table now was an array, which I didn't realize before. Not actually an mdast issue. --- README.md | 3 --- __tests__/index.js | 4 ++-- index.js | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6ed9f7e6..2c052e76 100644 --- a/README.md +++ b/README.md @@ -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 `
  • ` 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 diff --git a/__tests__/index.js b/__tests__/index.js index 1f89c5af..3540a0e2 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -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'); @@ -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'); diff --git a/index.js b/index.js index 2336a314..7be014ab 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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 */ From e4c1e5df07440106d625746ce31a842a885d4aa0 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Tue, 8 Dec 2015 18:28:41 -0500 Subject: [PATCH 2/2] 1.1.0 --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 636bf458..0aefdc31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package.json b/package.json index 00c30e7e..999d0a26 100644 --- a/package.json +++ b/package.json @@ -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",