Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/markdown-to-slate/handle-children.ts
1698 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Descendant } from "slate";
7
import { register } from "./register";
8
import { parse } from "./parse";
9
import { State } from "./types";
10
11
function handleChildren({ token, state, cache }) {
12
if (!token.children || token.children.length == 0) return;
13
14
// Parse all the children with own state, partly inherited
15
// from us (e.g., the text marks).
16
const child_state: State = {
17
marks: { ...state.marks },
18
nesting: 0,
19
lines: state.lines,
20
};
21
const children: Descendant[] = [];
22
for (const token2 of token.children) {
23
for (const node of parse(token2, child_state, cache)) {
24
children.push(node);
25
}
26
}
27
/*
28
SlateJS has some constraints on documents, as explained here:
29
https://docs.slatejs.org/concepts/10-normalizing
30
Number 4 is particular relevant here:
31
32
4. **Inline nodes cannot be the first or last child of a parent block, nor can it be next to another inline node in the children array.** If this is the case, an empty text node will be added to correct this to be in compliance with the constraint.
33
*/
34
if (children.length > 0 && children[0]["isInline"]) {
35
children.unshift({ text: "" });
36
}
37
if (children.length > 0 && children[children.length - 1]["isInline"]) {
38
children.push({ text: "" });
39
}
40
41
return children;
42
}
43
44
register(handleChildren);
45
46