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

indentList option for MD deserializer #2856

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ export const createDeserializeMdPlugin =
options: {
elementRules: remarkDefaultElementRules,
textRules: remarkDefaultTextRules,
indentList: false,
},
});
1 change: 1 addition & 0 deletions packages/serializer-md/src/deserializer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import { RemarkElementRules, RemarkTextRules } from '../remark-slate/index';
export interface DeserializeMdPlugin<V extends Value = Value> {
elementRules?: RemarkElementRules<V>;
textRules?: RemarkTextRules<V>;
indentList?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,98 @@ describe('deserializeMd', () => {
expect(deserializeMd(editor, input)).toEqual(output);
});
});

describe('deserializeMdIndentList', () => {
const editor = createPlateEditor({
plugins: [
createDeserializeMdPlugin({ options: { indentList: true } }) as any,
],
});

it('should deserialize unordered lists', () => {
const input = '- List item 1\n- List item 2';

const output = [
{
type: 'p',
listStyleType: 'disc',
indent: 1,
children: [
{
text: 'List item 1',
},
],
},
{
type: 'p',
listStyleType: 'disc',
indent: 1,
children: [
{
text: 'List item 2',
},
],
},
];

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

it('should deserialize ordered lists', () => {
const input = '1. List item 1\n2. List item 2';

const output = [
{
type: 'p',
listStyleType: 'decimal',
indent: 1,
children: [
{
text: 'List item 1',
},
],
},
{
type: 'p',
listStyleType: 'decimal',
indent: 1,
children: [
{
text: 'List item 2',
},
],
},
];

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

it('should deserialize mixed nested lists', () => {
const input = '- List item 1\n 1. List item 1.1';

const output = [
{
type: 'p',
listStyleType: 'disc',
indent: 1,
children: [
{
text: 'List item 1',
},
],
},
{
type: 'p',
listStyleType: 'disc',
indent: 2,
children: [
{
text: 'List item 1.1',
},
],
},
];

expect(deserializeMd(editor, input)).toEqual(output);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ export const deserializeMd = <V extends Value>(
editor: PlateEditor<V>,
data: string
) => {
const { elementRules, textRules } = getPluginOptions<DeserializeMdPlugin, V>(
editor,
KEY_DESERIALIZE_MD
);
const { elementRules, textRules, indentList } = getPluginOptions<
DeserializeMdPlugin,
V
>(editor, KEY_DESERIALIZE_MD);

const tree: any = unified()
.use(markdown)
.use(remarkPlugin, {
editor,
elementRules,
textRules,
indentList,
} as unknown as RemarkPluginOptions<V>)
.processSync(data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { ELEMENT_IMAGE } from '@udecode/plate-media';
import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';

import { remarkTransformElementChildren } from './remarkTransformElementChildren';
import { RemarkElementRules } from './types';
import { MdastNode, RemarkElementRules } from './types';

// FIXME: underline, subscript superscript not yet supported by remark-slate
export const remarkDefaultElementRules: RemarkElementRules<Value> = {
Expand All @@ -52,13 +52,44 @@ export const remarkDefaultElementRules: RemarkElementRules<Value> = {
},
},
list: {
transform: (node, options) => ({
type: getPluginType(
options.editor,
node.ordered ? ELEMENT_OL : ELEMENT_UL
),
children: remarkTransformElementChildren(node, options),
}),
transform: (node, options) => {
if (options.indentList) {
const listStyleType = node.ordered ? 'decimal' : 'disc';

const parseListItems = (
_node: MdastNode,
listItems: TElement[] = [],
indent = 1
) => {
_node.children!.forEach((listItem) => {
const [paragraph, ...subLists] = listItem.children!;

listItems.push({
type: getPluginType(options.editor, ELEMENT_PARAGRAPH),
listStyleType,
indent,
children: remarkTransformElementChildren(paragraph, options),
});

subLists.forEach((subList) => {
parseListItems(subList, listItems, indent + 1);
});
});

return listItems;
};

return parseListItems(node);
} else {
return {
type: getPluginType(
options.editor,
node.ordered ? ELEMENT_OL : ELEMENT_UL
),
children: remarkTransformElementChildren(node, options),
};
}
},
},
listItem: {
transform: (node, options) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/serializer-md/src/remark-slate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ export type RemarkPluginOptions<V extends Value> = {
editor: PlateEditor<V>;
elementRules: RemarkElementRules<V>;
textRules: RemarkTextRules<V>;
indentList?: boolean;
};
4 changes: 2 additions & 2 deletions packages/utils/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export type ModifyDeep<A extends AnyObject, B extends DeepPartialAny<A>> = {
[K in keyof A]: B[K] extends never
? A[K]
: B[K] extends AnyObject
? ModifyDeep<A[K], B[K]>
: B[K];
? ModifyDeep<A[K], B[K]>
: B[K];
} & (A extends AnyObject ? Omit<B, keyof A> : A);

export type PartialPick<T, K extends keyof T> = {
Expand Down