# Automatic replacement of unnecessarily nested nodes

**URL:** https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633
**Category:** Uncategorized
**Created:** [August 21, 2024, 4:58pm UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633 "2024-08-21T16:58:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sulee04](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/sulee04/32/4199_2.png) [@sulee04](https://discuss.prosemirror.net/u/sulee04)
#### Post date: [August 21, 2024, 4:58pm UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633/1 "2024-08-21T16:58:43Z")

</div>

I am working on a custom node with two editable children(vertical split, or two columns), with the schema similar to the following.

```javascript
{
  doc: {content: "container"},
  paragraph: {group: "block", content: "inline*"},
  container: {content: "block+"},
  split: {group: "block", content: "container{2}"},
}

```

The desired behavior is, when pressing backspace inside the empty `container` child of a `split`, the `split` should be replaced by the plain series of `block`s. However the schema forces `split`s to always have two `container` children. I can just lift the schema restriction by letting `split` accept one or two children, and append a replacing transaction for every single child `split`, but I am wondering if there will be a better workaround for this.

Sorry if the question is not clear enough(English is not my first language), and thanks in advance.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [August 21, 2024, 5:14pm UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633/2 "2024-08-21T17:14:41Z")

</div>

It looks like you should be able to create a `ReplaceAround` step that deletes both the split’s opening/closing tokens and the empty container.

---

<div class="post-metadata">

### Author: ![sulee04](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/sulee04/32/4199_2.png) [@sulee04](https://discuss.prosemirror.net/u/sulee04)
#### Post date: [August 22, 2024, 10:25am UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633/3 "2024-08-22T10:25:30Z")

</div>

Thanks for replying! I started to love ProseMirror(although I am new to it) and I really appreciate your decision to open it to public.

One follow-up question: What is the best practice of appending the `ReplaceAround` step to a transaction? Where can it be handled?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [August 22, 2024, 12:28pm UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633/4 "2024-08-22T12:28:29Z")

</div>

If by ‘the best practice’ you mean how this is done, you’d call `tr.step(new ReplaceAroundStep(...))`, and make sure you get all the (rather finicky) parameter to the constructor right so that the slice and the gap produce a valid new document.

---

<div class="post-metadata">

### Author: ![sulee04](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/sulee04/32/4199_2.png) [@sulee04](https://discuss.prosemirror.net/u/sulee04)
#### Post date: [August 23, 2024, 8:26am UTC](https://discuss.prosemirror.net/t/automatic-replacement-of-unnecessarily-nested-nodes/6633/5 "2024-08-23T08:26:08Z")

</div>

Thanks for your time, Marijn!

In case anyone reads this later, I decided to handle it in `EditorView`’s `dispatchTransaction`, which is quite trivial but wasn’t easy to figure it out as a ProseMirror novice.
