Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Nov 15, 2024
1 parent b36cdca commit c7a030e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-waves-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-markdown': patch
---

Support deserializing tables
123 changes: 39 additions & 84 deletions packages/markdown/src/lib/deserializer/utils/deserializeMd.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ describe('deserializeMdIndentList', () => {

expect(deserializeMd(editor, input)).toEqual(output);
});

it('should deserialize a table', () => {
const input = `
| Left columns | Right columns |
Expand All @@ -562,91 +563,45 @@ describe('deserializeMdIndentList', () => {
| left baz | right baz |
`;

const output = [
{
type: 'table',
children: [
{
type: 'tr',
children: [
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'Left columns' }],
}],
},
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'Right columns' }],
}],
},
],
},
{
type: 'tr',
children: [
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'left foo' }],
}],
},
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'right foo' }],
}],
},
],
},
{
type: 'tr',
children: [
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'left bar' }],
}],
},
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'right bar' }],
}],
},
],
},
{
type: 'tr',
children: [
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'left baz' }],
}],
},
{
type: 'td',
children: [{
type: 'p',
children: [{ text: 'right baz' }],
}],
},
],
},
],
}
]
const output = (
<fragment>
<htable>
<htr>
<hth>
<hp>Left columns</hp>
</hth>
<hth>
<hp>Right columns</hp>
</hth>
</htr>
<htr>
<htd>
<hp>left foo</hp>
</htd>
<htd>
<hp>right foo</hp>
</htd>
</htr>
<htr>
<htd>
<hp>left bar</hp>
</htd>
<htd>
<hp>right bar</hp>
</htd>
</htr>
<htr>
<htd>
<hp>left baz</hp>
</htd>
<htd>
<hp>right baz</hp>
</htd>
</htr>
</htable>
</fragment>
);

expect(deserializeMd(editor, input)).toEqual(output);
});

});
27 changes: 16 additions & 11 deletions packages/markdown/src/lib/remark-slate/remarkDefaultElementRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const remarkDefaultElementRules: RemarkElementRules = {
transform: (node, options) => {
const children = node.children?.length
? node.children.flatMap((paragraph) =>
remarkTransformElementChildren(paragraph, options)
)
remarkTransformElementChildren(paragraph, options)
)
: [{ text: '' }];

// Flatten nested blockquotes (e.g. >>>)
Expand Down Expand Up @@ -177,21 +177,26 @@ export const remarkDefaultElementRules: RemarkElementRules = {
table: {
transform: (node, options) => {
const rows =
node.children?.map((row) => {
node.children?.map((row, rowIndex) => {
return {
children:
row.children?.map((cell) => {
const cellType = rowIndex === 0 ? 'th' : 'td';

return {
type: options.editor.getType({ key: 'td' }),
children: remarkTransformElementChildren(cell, options).map(child => {
if (!child.type) {
return {
type: options.editor.getType({ key: 'p' }),
children: [child]
children: remarkTransformElementChildren(cell, options).map(
(child) => {
if (!child.type) {
return {
children: [child],
type: options.editor.getType({ key: 'p' }),
};
}

return child;
}
return child
}),
),
type: options.editor.getType({ key: cellType }),
};
}) || [],
type: options.editor.getType({ key: 'tr' }),
Expand Down

0 comments on commit c7a030e

Please sign in to comment.