Path: blob/master/src/packages/frontend/editors/slate/markdown-to-slate/normalize.ts
1697 views
import { createEditor, Descendant, Editor } from "slate";1import { withNormalize } from "../normalize";2import { withIsInline, withIsVoid } from "../plugins";34// Make an editor that we'll use for normalizing. It's important that it5// has exactly the plugins relevant to normalizing, but nothing else.6const editor = withNormalize(withIsInline(withIsVoid(createEditor())));78export default function normalize(children: Descendant[]) {9//console.log("about to normalize..."); //, JSON.stringify(children));10editor.children = children;11Editor.normalize(editor);12// console.log("after normalize:"); //, JSON.stringify(editor.children));13return editor.children;14}1516// Ensure that the array children starts and ends with a Text node.17// Mutates children in place.18export function ensureTextStartAndEnd(children: Descendant[]) {19if (children[children.length - 1]?.["text"] == null) {20children.push({ text: "" });21}22if (children[0]?.["text"] == null) {23children.unshift({ text: "" });24}25}262728