Skip to content
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
merged 12 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 24 additions & 53 deletions __tests__/compilers/tables.test.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,42 @@
import { mdast, mdx } from '../../index';
import { visit, EXIT } 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', () => {
it.only('saves to MDX 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]
| th 1 | th 2 |
| :----: | :----: |
| cell 1 | cell 2 |
`;

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]
`);
const tree = mdast(markdown);

console.log(JSON.stringify({ tree }, null, 2));
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 |
"
`);
});

it('converts to magic block syntax if there are breaks', () => {
Expand Down
19 changes: 0 additions & 19 deletions components/Table/index.jsx

This file was deleted.

51 changes: 51 additions & 0 deletions components/Table/index.tsx
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;
23 changes: 23 additions & 0 deletions docs/mdx-components.mdx
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} />
26 changes: 17 additions & 9 deletions docs/tables.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
title: "Tables"
title: 'Tables'
category: 5fdf7610134322007389a6ed
hidden: false
---

## Syntax

| Left | Center | Right |
Expand All @@ -20,7 +21,7 @@ hidden: false
This example also shows off custom theming!

| Left | Center | Right |
|:-----|:--------:|------:|
| :--- | :------: | ----: |
| L0 | **bold** | $1600 |
| L1 | `code` | $12 |
| L2 | _italic_ | $1 |
Expand All @@ -35,25 +36,32 @@ Tables have been simplified to mirror a more standard implementation. We've also
--table-head: #5b1c9f;
--table-head-text: white;
--table-stripe: #f0eaf7;
--table-edges: rgba(34, 5, 64, .5);
--table-edges: rgba(34, 5, 64, 0.5);
--table-row: white;
}
```

```scss CSS Selectors
/* Table
*/
.markdown-body .rdmd-table table {}
.markdown-body .rdmd-table table {
}

/* Rows
*/
.markdown-body .rdmd-table tr {}
.markdown-body .rdmd-table thead tr {} /* header row's background */
.markdown-body .rdmd-table tr:nth-child(2n) {} /* striped rows' background */
.markdown-body .rdmd-table tr {
}
.markdown-body .rdmd-table thead tr {
} /* header row's background */
.markdown-body .rdmd-table tr:nth-child(2n) {
} /* striped rows' background */

/* Cells
*/
.markdown-body .rdmd-table th {}
.markdown-body .rdmd-table td {}
.markdown-body .rdmd-table th {
}
.markdown-body .rdmd-table td {
}
```

<style>
Copy link
Collaborator

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:

image

but haaaay nice error messaging!

Copy link
Collaborator Author

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

Expand Down
26 changes: 26 additions & 0 deletions errors/mdx-syntax-error.ts
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}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

];

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;
}
}
16 changes: 12 additions & 4 deletions example/Doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Doc = () => {
fixture === 'edited' ? [fixture, searchParams.get('edit') || ''] : [docs[fixture].name, docs[fixture].doc];

const [Content, setContent] = useState<FunctionComponent>(null);
const [error, setError] = useState<string>(null);

useEffect(() => {
const render = async () => {
Expand All @@ -23,22 +24,29 @@ const Doc = () => {
safeMode,
};

const code = mdx.compile(doc, opts);
const content = await mdx.run(code);
try {
const code = mdx.compile(doc, opts);
const content = await mdx.run(code);

setContent(() => content);
setError(() => null);
setContent(() => content);
} catch (e) {
setError(() => e.message);
}
};

render();
}, [doc, lazyImages, safeMode]);

mdx.mdx(mdx.mdast(doc));

return (
<React.Fragment>
<div className="rdmd-demo--display">
<section id="hub-content">
{!ci && <h2 className="rdmd-demo--markdown-header">{name}</h2>}
<div id="content-container">
<RenderError>
<RenderError error={error}>
<div className="markdown-body">{Content && <Content />}</div>
</RenderError>
</div>
Expand Down
10 changes: 6 additions & 4 deletions example/RenderError.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { PropsWithChildren } from 'react';

interface Props {}
interface Props {
error?: string;
}

interface State {
hasError: boolean;
Expand All @@ -19,13 +21,13 @@ class RenderError extends React.Component<PropsWithChildren<Props>, State> {
}

render() {
const { children } = this.props;
const { children, error } = this.props;
const { hasError, message } = this.state;

return hasError ? (
return hasError || error ? (
<div className="error">
<pre>
<code>{message}</code>
<code>{message || error}</code>
</pre>
</div>
) : (
Expand Down
3 changes: 3 additions & 0 deletions example/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import images from '../docs/images.md';
// @ts-ignore
import lists from '../docs/lists.md';
// @ts-ignore
import mdxComponents from '../docs/mdx-components.mdx';
// @ts-ignore
import sanitizingTests from '../docs/sanitizing-tests.md';
// @ts-ignore
import tableOfContentsTests from '../docs/table-of-contents-tests.md';
Expand All @@ -43,6 +45,7 @@ const fixtures = Object.entries({
headings,
images,
lists,
mdxComponents,
sanitizingTests,
tableOfContentsTests,
tables,
Expand Down
Loading
Loading