Multiple transactions not creating multiple history steps?

I’m working on a handlePaste prop that will transform pasted links into a special kind of node. I want the action of “undo” to revert to the pasted link being in its regular plain text form, and only a a second “undo” would roll back the entire paste.

The way I tried to go about this is by dispatching two transactions- one that inserts the link as plain text, and another that removes the plain text and inserts it as a special node. It’s two transactions instead of one, because I want the user to be able to “undo” the second transaction without undoing the first.

However, I found that “undo” doesn’t revert back to the plain text, rather, it reverts the entire paste, i.e. both transactions.

I’m pasting the code below. (Obviously the “special node” action is just a placholder for now, but the question is just about the history/ transactions.) I added in “undoDepth” to observe that indeed the second transaction didn’t increment the undo depth. What am I doing wrong here?

export function handlePaste (view: EditorView, event: ClipboardEvent, slice: Slice) {
  if (slice.openStart === 0 && slice.openEnd === 0) {
    const selection = view.state.selection
    const startPos = selection.from < selection.to ? selection.from : selection.to

    console.log("%c Undo depth is:", "color:yellow", undoDepth(view.state)) // returns: 0

    // step one: delete selection and insert as plain text. 
    let tr = view.state.tr
    tr = tr.deleteSelection()
    tr = tr.insert(startPos, slice.content)
    view.dispatch(tr)

    console.log("%c Undo depth is:", "color:yellow", undoDepth(view.state)) // returns: 1

    // step two: delete the inserted fragment, and insert the transformed fragment. 
    tr = view.state.tr
    tr = tr.delete(startPos, startPos + slice.size)
    tr = tr.insertText("placeholder") // placeholder for creating a "special" link node
    view.dispatch(tr)

    // why doesn't the next line return 2?
    console.log("%c Undo depth is:", "color:yellow", undoDepth(view.state)) // returns: 1. 

    return true

  } else return false
}

OK, figured out, I just needed to call closeHistory in between the two steps.

// step two: delete the inserted fragment, and insert the transformed fragment. 
tr = view.state.tr
closeHistory(tr)
// ...