forked from udecode/plate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializeMd.ts
42 lines (34 loc) · 1.02 KB
/
serializeMd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { SlateEditor } from '@udecode/plate';
import merge from 'lodash/merge.js';
import type {
SerializeMdNodeOptions,
SerializeMdOptions,
} from './serializeMdNode';
import { serializeMdNodes } from './serializeMdNodes';
/** Serialize the editor value to Markdown. */
export const serializeMd = (
editor: SlateEditor,
options?: { value?: Parameters<typeof serializeMdNodes>['0'] } & Parameters<
typeof serializeMdNodes
>['1']
) => {
const plugins = editor.pluginList.filter(
(p) => p.node.isElement || p.node.isLeaf
);
const pluginNodes = plugins.reduce(
(acc, plugin) => {
(acc as any)[plugin.key] = {
isLeaf: plugin.node.isLeaf,
isVoid: plugin.node.isVoid,
type: plugin.node.type,
} as SerializeMdNodeOptions;
return acc;
},
{} as SerializeMdOptions['nodes']
);
const nodesToSerialize = options?.value ?? editor.children;
return serializeMdNodes(nodesToSerialize, {
...options,
nodes: merge(pluginNodes, options?.nodes),
});
};