Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Sep 30, 2024
2 parents a960433 + c929bd0 commit 4a2b322
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
55 changes: 55 additions & 0 deletions __tests__/compilers/compatability.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,59 @@ ${JSON.stringify(
"
`);
});

it('compiles tables with inline code with newlines', () => {
const md = `
[block:parameters]
${JSON.stringify(
{
data: {
'h-0': 'Field',
'h-1': 'Description',
'0-0': 'orderby',
'0-1': '`{\n "field": "ID",\n "type": "ASC"\n }`',
},
cols: 2,
rows: 1,
align: ['left', 'left'],
},
null,
2,
)}
[/block]
`;

const rmdx = mdx(rdmd.mdast(md));
expect(rmdx).toMatchInlineSnapshot(`
"<Table align={["left","left"]}>
<thead>
<tr>
<th style={{ textAlign: "left" }}>
Field
</th>
<th style={{ textAlign: "left" }}>
Description
</th>
</tr>
</thead>
<tbody>
<tr>
<td style={{ textAlign: "left" }}>
orderby
</td>
<td style={{ textAlign: "left" }}>
\`{
"field": "ID",
"type": "ASC"
}\`
</td>
</tr>
</tbody>
</Table>
"
`);
});
});
12 changes: 12 additions & 0 deletions __tests__/plugins/toc.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ describe('toc transformer', () => {
expect(screen.findByText('Common Heading')).toBeDefined();
expect(screen.findByText('Subheading')).toBeDefined();
});

it('parses out a toc and only uses plain text', async () => {
const md = `
# [Title](http://example.com)
`;
const { Toc } = await run(compile(md));

render(<Toc />);

expect(screen.findByText('Title')).toBeDefined();
expect(screen.queryByText('[', { exact: false })).toBeNull();
});
});
9 changes: 6 additions & 3 deletions processor/plugin/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { h } from 'hastscript';

import { CustomComponents, HastHeading, IndexableElements, TocList, TocListItem } from '../../types';
import { visit } from 'unist-util-visit';
import { mdx } from '../../lib';
import { mdx, plain } from '../../lib';

interface Options {
components?: Record<string, any>;
Expand Down Expand Up @@ -74,10 +74,13 @@ const tocToHast = (headings: HastHeading[] = []): TocList => {
stack.pop();
}

if (heading.properties)
if (heading.properties) {
const content = plain({ type: 'root', children: heading.children }) as string;

stack[stack.length - 1].children.push(
h('li', null, h('a', { href: `#${heading.properties.id}` }, heading.children)) as TocListItem,
h('li', null, h('a', { href: `#${heading.properties.id}` }, content)) as TocListItem,
);
}
});

return ast;
Expand Down
8 changes: 5 additions & 3 deletions processor/transform/tables-to-jsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node, Paragraph, Parents, Table, TableCell, Text } from 'mdast';
import { Literal, Node, Paragraph, Parents, Table, TableCell, Text } from 'mdast';
import { visit, EXIT } from 'unist-util-visit';
import { Transform } from 'mdast-util-from-markdown';

Expand All @@ -19,6 +19,8 @@ const alignToStyle = (align: 'left' | 'center' | 'right' | null) => {

const isTableCell = (node: Node) => ['tableHead', 'tableCell'].includes(node.type);

const isLiteral = (node: Node): node is Literal => 'value' in node;

const visitor = (table: Table, index: number, parent: Parents) => {
let hasFlowContent = false;

Expand All @@ -39,8 +41,8 @@ const visitor = (table: Table, index: number, parent: Parents) => {
return EXIT;
}

visit(cell, 'text', (text: Text) => {
if (text.value.match(/\n/)) {
visit(cell, isLiteral, (node: Literal) => {
if (node.value.match(/\n/)) {
hasFlowContent = true;
return EXIT;
}
Expand Down

0 comments on commit 4a2b322

Please sign in to comment.