From 603420e278d77dc434576bd64c5355810882938b Mon Sep 17 00:00:00 2001 From: depressedX <26671074+depressedX@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:18:49 +0800 Subject: [PATCH 1/2] fix: @udecode/plate-markdown fails when deserializing list with indented block element --- .../deserializer/utils/deserializeMd.spec.tsx | 59 +++++++++++++++++++ .../remark-slate/remarkDefaultElementRules.ts | 17 +++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/packages/markdown/src/lib/deserializer/utils/deserializeMd.spec.tsx b/packages/markdown/src/lib/deserializer/utils/deserializeMd.spec.tsx index d505ea962a..60cec0e005 100644 --- a/packages/markdown/src/lib/deserializer/utils/deserializeMd.spec.tsx +++ b/packages/markdown/src/lib/deserializer/utils/deserializeMd.spec.tsx @@ -420,4 +420,63 @@ describe('deserializeMdIndentList', () => { expect(deserializeMd(editor, input)).toEqual(output); }); + + it('should deserialize list with indented block element', () => { + const input = ` +- 1 +- 2 + - 2.1 + \`\`\` + 2.2 code + \`\`\` +`.trim(); + const output = [ + { + children: [ + { + text: '1', + }, + ], + indent: 1, + listStyleType: 'disc', + type: 'p', + }, + { + children: [ + { + text: '2', + }, + ], + indent: 1, + listStyleType: 'disc', + type: 'p', + }, + { + children: [ + { + text: '2.1', + }, + ], + indent: 2, + listStyleType: 'disc', + type: 'p', + }, + { + children: [ + { + children: [ + { + text: '2.2 code', + }, + ], + type: 'code_line', + }, + ], + indent: 2, + type: 'code_block', + }, + ]; + + expect(deserializeMd(editor, input)).toEqual(output); + }); }); diff --git a/packages/markdown/src/lib/remark-slate/remarkDefaultElementRules.ts b/packages/markdown/src/lib/remark-slate/remarkDefaultElementRules.ts index 1ee7c9b98c..34252f7d28 100644 --- a/packages/markdown/src/lib/remark-slate/remarkDefaultElementRules.ts +++ b/packages/markdown/src/lib/remark-slate/remarkDefaultElementRules.ts @@ -3,6 +3,7 @@ import type { TDescendant, TElement, TText } from '@udecode/plate-common'; import type { MdastNode, RemarkElementRules } from './types'; import { remarkTransformElementChildren } from './remarkTransformElementChildren'; +import { remarkTransformNode } from './remarkTransformNode'; // FIXME: underline, subscript superscript not yet supported by remark-slate export const remarkDefaultElementRules: RemarkElementRules = { @@ -82,7 +83,21 @@ export const remarkDefaultElementRules: RemarkElementRules = { }); subLists.forEach((subList) => { - parseListItems(subList, listItems, indent + 1); + if (subList.type === 'list') { + parseListItems(subList, listItems, indent + 1); + } else { + const result = remarkTransformNode(subList, options) as + | TElement + | TElement[]; + + if (Array.isArray(result)) { + listItems.push( + ...result.map((v) => ({ ...v, indent: indent + 1 })) + ); + } else { + listItems.push({ ...result, indent: indent + 1 }); + } + } }); }); From 40cd0d7b1e382fcddc4732ef3dc9b6377acd3f2a Mon Sep 17 00:00:00 2001 From: depressedX <26671074+depressedX@users.noreply.github.com> Date: Sun, 29 Sep 2024 16:26:23 +0800 Subject: [PATCH 2/2] changeset --- .changeset/few-pans-float.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/few-pans-float.md diff --git a/.changeset/few-pans-float.md b/.changeset/few-pans-float.md new file mode 100644 index 0000000000..d342a49475 --- /dev/null +++ b/.changeset/few-pans-float.md @@ -0,0 +1,5 @@ +--- +'@udecode/plate-markdown': patch +--- + +Fix @udecode/plate-markdown deserializing list with indented block element