-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathremarkSplitLineBreaksCompiler.ts
50 lines (40 loc) · 1.37 KB
/
remarkSplitLineBreaksCompiler.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
import type { TDescendant, TText } from '@udecode/plate-common';
import type { MdastNode, RemarkPluginOptions } from './types';
import { remarkTransformNode } from './remarkTransformNode';
export const remarkSplitLineBreaksCompiler = (
node: MdastNode,
options: RemarkPluginOptions
): TDescendant[] => {
const results: TDescendant[] = [];
let startLine = node.position!.start.line;
const addEmptyParagraphs = (count: number) => {
if (count > 0) {
results.push(
...Array.from({ length: count }).map(() => {
return {
children: [{ text: '' } as TText],
type: options.editor.getType({ key: 'p' }),
};
})
);
}
};
node?.children?.forEach((child, index) => {
const isFirstChild = index === 0;
const isLastChild = index === node.children!.length - 1;
const emptyLinesBefore =
child.position!.start.line - (isFirstChild ? startLine : startLine + 1);
addEmptyParagraphs(emptyLinesBefore);
const transformValue = remarkTransformNode(child, options);
results.push(
...(Array.isArray(transformValue) ? transformValue : [transformValue])
);
if (isLastChild) {
const emptyLinesAfter =
node.position!.end.line - child.position!.end.line - 1;
addEmptyParagraphs(emptyLinesAfter);
}
startLine = child.position!.end.line;
});
return results;
};