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/normalize.ts
1697 views
1
import { createEditor, Descendant, Editor } from "slate";
2
import { withNormalize } from "../normalize";
3
import { withIsInline, withIsVoid } from "../plugins";
4
5
// Make an editor that we'll use for normalizing. It's important that it
6
// has exactly the plugins relevant to normalizing, but nothing else.
7
const editor = withNormalize(withIsInline(withIsVoid(createEditor())));
8
9
export default function normalize(children: Descendant[]) {
10
//console.log("about to normalize..."); //, JSON.stringify(children));
11
editor.children = children;
12
Editor.normalize(editor);
13
// console.log("after normalize:"); //, JSON.stringify(editor.children));
14
return editor.children;
15
}
16
17
// Ensure that the array children starts and ends with a Text node.
18
// Mutates children in place.
19
export function ensureTextStartAndEnd(children: Descendant[]) {
20
if (children[children.length - 1]?.["text"] == null) {
21
children.push({ text: "" });
22
}
23
if (children[0]?.["text"] == null) {
24
children.unshift({ text: "" });
25
}
26
}
27
28