# Split at paragraph boundaries leaves empty paragraphs before/after

**URL:** <https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358>\
**Category:** Uncategorized\
**Created:** [November 19, 2019, 2:12pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358 "2019-11-19T14:12:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javarome](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/javarome/32/1212_2.png) [@Javarome](https://discuss.prosemirror.net/u/Javarome)\
**Post date:** [November 19, 2019, 2:12pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358/1 "2019-11-19T14:12:13Z")

</div>

Hello, I’m trying to perform a split from a selection and this works well… …unless a boundary of the selection is the start or the end of the paragraph. In such a case the split occurs but leaves the original paragraph empty.

For example, splitting “Hello” with “ell” selected produces paragraphs “h”, “ell”, “o”, but splitting it with “Hell” selected produces “”, “Hell”, “o” and splitting it with “ello” selected produces “H”, “ello”, “”

How can I manage to avoid empty paragraphs remaining?

---

<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:** [November 19, 2019, 2:24pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358/2 "2019-11-19T14:24:09Z")

</div>

You didn’t mention how you are doing the splitting, but if you’re making direct calls to `Transform.split`, could you just not split when the position is at the end or start of its parent node?

---

<div class="post-metadata">

**Author:** ![Javarome](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/javarome/32/1212_2.png) [@Javarome](https://discuss.prosemirror.net/u/Javarome)\
**Post date:** [November 19, 2019, 3:00pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358/3 "2019-11-19T15:00:14Z")

</div>

Yes I’m using `Transform.split`. I don’t think I could avoid splitting in such edge cases because:

- when the position is at the start I still want to split at the end of the selection
- when the position is at the end I still want to split at the beginning of the selection.

To make things more clear, the structure to split is:

- simpleAnswer
  - paragraph
    - text(“Hello”)

and say I want to split some selection in paragraph, I’m trying to build:

- simpleAnswer
  - paragraph _only if text remaining at left side of selection is not empty_
    - text(textRemainingAtLeftSideOfSelection)

- simpleAnswer
  - paragraph
    - text(selection)

- simpleAnswer
  - paragraph _only if text remaining at right side of selection is not empty_
    - paragraph
      - text(textRemainingAtRightSideOfSelection)

To do so I’m doing:

```
const currentSimpleAnswer: ContentNodeWithPos = findParentNodeOfType(this.schema.nodes.simpleAnswer)(selection);
const splitDepth = currentSimpleAnswer.depth + 1; // Split at the above "simpleAnswer" level
tr.split(tr.mapping.map(selection.from), splitDepth);
tr.split(tr.mapping.map(selection.to), splitDepth);

```

(as you can see I’m using Atlassian’s `prosemirror-utils` to find parent node)

---

<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:** [November 19, 2019, 3:17pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358/4 "2019-11-19T15:17:38Z")

</div>

> [@Javarome](#):
>
> Yes I’m using `Transform.split` . I don’t think I could avoid splitting in such edge cases because:
> 
> - when the position is at the start I still want to split at the end of the selection
> - when the position is at the end I still want to split at the beginning of the selection.

I do not see how this is a problem. Your code can check for these cases and do the appropriate amount of splitting.

---

<div class="post-metadata">

**Author:** ![Javarome](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/javarome/32/1212_2.png) [@Javarome](https://discuss.prosemirror.net/u/Javarome)\
**Post date:** [November 19, 2019, 3:31pm UTC](https://discuss.prosemirror.net/t/split-at-paragraph-boundaries-leaves-empty-paragraphs-before-after/2358/5 "2019-11-19T15:31:55Z")

</div>

Well, you are perfectly right 🙂 Sorry I didn’t realize that.

Now my code is:

```
if (selection.$from.parentOffset > 0) {
  tr.split(tr.mapping.map(selection.from), splitDepth);
}
if (selection.$to.parentOffset < selection.$to.node().content.size) {
  tr.split(tr.mapping.map(selection.to), splitDepth);
}

```

And it is working perfectly as expected. Thank you very much!
