Skip to content

Commit

Permalink
Merge pull request #3768 from frgarciames/feat/table-element
Browse files Browse the repository at this point in the history
Feat/table element
  • Loading branch information
zbeyens authored Nov 15, 2024
2 parents 828b1de + f8f3de8 commit b36cdca
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,100 @@ describe('deserializeMdIndentList', () => {

expect(deserializeMd(editor, input)).toEqual(output);
});
it('should deserialize a table', () => {
const input = `
| Left columns | Right columns |
| ------------- |:-------------:|
| left foo | right foo |
| left bar | right bar |
| 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' }],
}],
},
],
},
],
}
]

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

});
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 @@ -78,7 +78,7 @@ export const remarkDefaultElementRules: RemarkElementRules = {
listItems: TElement[] = [],
indent = 1
) => {
_node.children!.forEach((listItem) => {
_node.children?.forEach((listItem) => {
const [paragraph, ...subLists] = listItem.children!;

listItems.push({
Expand Down Expand Up @@ -114,12 +114,12 @@ export const remarkDefaultElementRules: RemarkElementRules = {
};

return parseListItems(node);
} else {
return {
children: remarkTransformElementChildren(node, options),
type: options.editor.getType({ key: node.ordered ? 'ol' : 'ul' }),
};
}

return {
children: remarkTransformElementChildren(node, options),
type: options.editor.getType({ key: node.ordered ? 'ol' : 'ul' }),
};
},
},
listItem: {
Expand Down Expand Up @@ -174,6 +174,36 @@ export const remarkDefaultElementRules: RemarkElementRules = {
return elements;
},
},
table: {
transform: (node, options) => {
const rows =
node.children?.map((row) => {
return {
children:
row.children?.map((cell) => {
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]
}
}
return child
}),
};
}) || [],
type: options.editor.getType({ key: 'tr' }),
};
}) || [];

return {
children: rows,
type: options.editor.getType({ key: 'table' }),
};
},
},
thematicBreak: {
transform: (node, options) => ({
children: [{ text: '' } as TText],
Expand Down
1 change: 1 addition & 0 deletions packages/markdown/src/lib/remark-slate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type MdastElementType =
| 'list'
| 'listItem'
| 'paragraph'
| 'table'
| 'thematicBreak';

export type MdastTextType =
Expand Down

0 comments on commit b36cdca

Please sign in to comment.