-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathMarkdownPlugin.ts
84 lines (75 loc) · 2.05 KB
/
MarkdownPlugin.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
type OmitFirst,
type PluginConfig,
bindFirst,
createTSlatePlugin,
isUrl,
} from '@udecode/plate-common';
import { deserializeMd } from './deserializer/utils';
import {
type RemarkElementRules,
type RemarkTextRules,
remarkDefaultElementRules,
remarkDefaultTextRules,
} from './remark-slate';
import { serializeMd } from './serializer';
// export type MarkdownDeserializer = {
// elementRules?: Partial<Record<MdastElementType, RemarkElementRule>>;
// textRules?: Partial<Record<MdastTextType, RemarkTextRule>>;
// } & Deserializer;
export type MarkdownConfig = PluginConfig<
'markdown',
{
/** Override element rules. */
elementRules?: RemarkElementRules;
indentList?: boolean;
/**
* When the text contains \n, split the text into a separate paragraph.
*
* Line breaks between paragraphs will also be converted into separate
* paragraphs.
*
* @default false
*/
splitLineBreaks?: boolean;
/** Override text rules. */
textRules?: RemarkTextRules;
},
{
markdown: {
deserialize: OmitFirst<typeof deserializeMd>;
serialize: OmitFirst<typeof serializeMd>;
};
}
>;
export const MarkdownPlugin = createTSlatePlugin<MarkdownConfig>({
key: 'markdown',
options: {
elementRules: remarkDefaultElementRules,
indentList: false,
splitLineBreaks: false,
textRules: remarkDefaultTextRules,
},
})
.extendApi(({ editor }) => ({
deserialize: bindFirst(deserializeMd, editor),
serialize: bindFirst(serializeMd, editor),
}))
.extend(({ api }) => ({
parser: {
deserialize: ({ data }) => api.markdown.deserialize(data),
format: 'text/plain',
query: ({ data, dataTransfer }) => {
const htmlData = dataTransfer.getData('text/html');
if (htmlData) return false;
const { files } = dataTransfer;
if (
!files?.length && // if content is simply a URL pass through to not break LinkPlugin
isUrl(data)
) {
return false;
}
return true;
},
},
}));