-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: tables #874
Merged
+222
−241
Merged
feat: tables #874
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8b9d536
wip
kellyjosephprice 82897d8
feat: error handling
kellyjosephprice c13d997
tshit
kellyjosephprice 879c2b9
rebase oops
kellyjosephprice 1581f20
wip
kellyjosephprice fd5796b
wip
kellyjosephprice aa1f7ca
punt on that
kellyjosephprice e329b1e
copy
kellyjosephprice db9c1be
Merge branch 'beta' into feat/tables
kellyjosephprice 2c28b91
better docs + snaps
kellyjosephprice 7ab4426
fix doc
kellyjosephprice 23f600a
actually fix
kellyjosephprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+31.2 KB
...egression-tests-rdmd-syntax-renders-mdx-components-without-surprises-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,40 @@ | ||
import { mdast, mdx } from '../../index'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
describe.skip('table compiler', () => { | ||
it('converts to markdown syntax', () => { | ||
describe('table compiler', () => { | ||
it('writes to markdown syntax', () => { | ||
const markdown = ` | ||
[block:parameters] | ||
{ | ||
"data": { | ||
"h-0": "th 1", | ||
"h-1": "th 2", | ||
"0-0": "cell 1", | ||
"0-1": "cell 2" | ||
}, | ||
"cols": 2, | ||
"rows": 1, | ||
"align": [ | ||
"center", | ||
"center" | ||
] | ||
} | ||
[/block] | ||
| th 1 | th 2 | | ||
| :----: | :----: | | ||
| cell 1 | cell 2 | | ||
`; | ||
|
||
expect(mdx(mdast(markdown))).toBe( | ||
`| th 1 | th 2 | | ||
| :----: | :----: | | ||
| cell 1 | cell 2 | | ||
` | ||
`, | ||
); | ||
}); | ||
|
||
it('saves to magic block syntax if there are breaks', () => { | ||
const markdown = ` | ||
[block:parameters] | ||
{ | ||
"data": { | ||
"h-0": "th 1", | ||
"h-1": "th 2", | ||
"0-0": "cell 1\\nextra line", | ||
"0-1": "cell 2" | ||
}, | ||
"cols": 2, | ||
"rows": 1, | ||
"align": [ | ||
"center", | ||
"center" | ||
] | ||
} | ||
[/block] | ||
`; | ||
|
||
expect(mdx(mdast(markdown))).toBe(`[block:parameters] | ||
{ | ||
"data": { | ||
"h-0": "th 1", | ||
"h-1": "th 2", | ||
"0-0": "cell 1 \\nextra line", | ||
"0-1": "cell 2" | ||
}, | ||
"cols": 2, | ||
"rows": 1, | ||
"align": [ | ||
"center", | ||
"center" | ||
] | ||
} | ||
[/block] | ||
`); | ||
}); | ||
|
||
it('converts to magic block syntax if there are breaks', () => { | ||
it.skip('saves to MDX if there are breaks', () => { | ||
const markdown = ` | ||
| th 1 | th 2 | | ||
| :----: | :----: | | ||
| cell 1 | cell 2 | | ||
`; | ||
const nodes = mdast(markdown); | ||
const cell = nodes.children[0].children[1].children[0]; | ||
cell.children = [...cell.children, { type: 'break' }, { type: 'text', value: 'extra line' }]; | ||
|
||
expect(mdx(nodes)).toBe(`[block:parameters] | ||
{ | ||
"data": { | ||
"h-0": "th 1", | ||
"h-1": "th 2", | ||
"0-0": "cell 1 \\nextra line", | ||
"0-1": "cell 2" | ||
}, | ||
"cols": 2, | ||
"rows": 1, | ||
"align": [ | ||
"center", | ||
"center" | ||
] | ||
} | ||
[/block] | ||
`); | ||
const tree = mdast(markdown); | ||
|
||
visit(tree, 'tableCell', cell => { | ||
cell.children.push({ type: 'break' }, { type: 'text', value: 'inserted' }); | ||
}); | ||
|
||
expect(mdx(tree)).toMatchInlineSnapshot(` | ||
"| th 1 inserted | th 2 inserted | | ||
| :-------------: | :-------------: | | ||
| cell 1 inserted | cell 2 inserted | | ||
" | ||
`); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
|
||
interface Props extends JSX.IntrinsicAttributes { | ||
align?: ('left' | 'center' | 'right')[]; | ||
children: [React.ReactElement<HTMLTableCaptionElement | HTMLTableSectionElement | HTMLTableRowElement>]; | ||
rows?: any[][]; | ||
} | ||
|
||
interface TableContentProps { | ||
align?: Props['align']; | ||
rows: Props['rows']; | ||
} | ||
|
||
const TableContent = ({ rows, align = [] }: TableContentProps) => { | ||
const [head, ...body] = rows; | ||
|
||
return ( | ||
<> | ||
<thead> | ||
<tr> | ||
{head.map((cell, index) => ( | ||
<th style={{ textAlign: align[index] || 'center' }}>{cell}</th> | ||
))} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{body.map(row => ( | ||
<tr> | ||
{row.map((cell, index) => ( | ||
<td style={{ textAlign: align[index] || 'center' }}>{cell}</td> | ||
))} | ||
</tr> | ||
))} | ||
</tbody> | ||
</> | ||
); | ||
}; | ||
|
||
const Table = (props: Props) => { | ||
const { children, rows, align } = props; | ||
|
||
return ( | ||
<div className="rdmd-table"> | ||
<div className="rdmd-table-inner"> | ||
<table>{rows ? <TableContent align={align} rows={rows} /> : children}</table> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Table; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Tables | ||
|
||
If you have some tabular data, it may be easier to write it with mdx: | ||
|
||
```jsx MDX | ||
export const table = [ | ||
['Left', 'Center', 'Right'], | ||
['L0', '**bold**', '$1600'], | ||
['L1', '`code`', '$12'], | ||
['L2', '_italic_', '$1'], | ||
]; | ||
|
||
<Table align={['left', 'center', 'right']} rows={table} />; | ||
``` | ||
|
||
export const table = [ | ||
['Left', 'Center', 'Right'], | ||
['L0', '**bold**', '$1600'], | ||
['L1', '`code`', '$12'], | ||
['L2', '_italic_', '$1'], | ||
]; | ||
|
||
<Table align={['left', 'center', 'right']} rows={table} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { VFileMessage } from 'vfile-message'; | ||
|
||
export default class MdxSyntaxError extends SyntaxError { | ||
original: VFileMessage = null; | ||
|
||
constructor(error: VFileMessage, doc: string) { | ||
const { message, line, column, url } = error; | ||
|
||
const messages = [ | ||
`Uh oh! We ran into a syntax error at { line: ${line}, column: ${column} }, please see this url for more details: ${url}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
]; | ||
|
||
if (typeof line !== 'undefined') { | ||
messages.push(doc.split('\n')[line - 1]); | ||
|
||
if (typeof column !== 'undefined') { | ||
const prefix = new Array(column).map(() => '').join(' '); | ||
messages.push(`${prefix}↑ ${message}`); | ||
} | ||
} | ||
|
||
super(messages.join('\n')); | ||
|
||
this.original = error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't look like
acorn
is a big fan of this<style>
tag:but haaaay nice error messaging!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not so much the script, but css. I imagine that MDX looked at trying to detect and parse css, and just table flipped