Path: blob/master/src/packages/frontend/editors/slate/markdown-to-slate/handle-children.ts
1698 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Descendant } from "slate";6import { register } from "./register";7import { parse } from "./parse";8import { State } from "./types";910function handleChildren({ token, state, cache }) {11if (!token.children || token.children.length == 0) return;1213// Parse all the children with own state, partly inherited14// from us (e.g., the text marks).15const child_state: State = {16marks: { ...state.marks },17nesting: 0,18lines: state.lines,19};20const children: Descendant[] = [];21for (const token2 of token.children) {22for (const node of parse(token2, child_state, cache)) {23children.push(node);24}25}26/*27SlateJS has some constraints on documents, as explained here:28https://docs.slatejs.org/concepts/10-normalizing29Number 4 is particular relevant here:30314. **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.32*/33if (children.length > 0 && children[0]["isInline"]) {34children.unshift({ text: "" });35}36if (children.length > 0 && children[children.length - 1]["isInline"]) {37children.push({ text: "" });38}3940return children;41}4243register(handleChildren);444546