How to replace a block node with an inline node?

As the title above. I want to replace a figure tag with image tag. I have tried replace()replaceWith()setNodeMarkup()prosemirror-utils::replaceSelectedNode() methods etc.

function(tr, dispatch, node, nodeType, $pos, attrs){
    // `node` is figure Node, `nodeType` and `$pos` is the corresponding NodeType and ResolvedPos.

  
   let newNode = nodeType.create(attrs, null, null);
   let pos = $pos.pos;

    // attempt 1
   tr.replace(pos, pos + node.nodeSize, new Slice(newNode, 0, 0));

    // attempt 2
   tr.replaceWith(pos, pos + node.nodeSize, newNode);

    // attempt 3
   tr.setNodeMarkup($pos.pos, nodeType, attrs);

  //  // attempt 4       in prosemirror-utils
  replaceSelectedNode(newNode)(tr);
}

ProseMirror schemas don’t allow you to mix inline and block nodes in a given parent node—each node has either inline or block content (or no content). As such, directly replacing a block node with an inline, without wrapping the node in a block, is never going to be valid

Thanks for your kind reply. When ProseMirror parses html , it automatically wraps a paragraph outside the inline node. So, I thought it would do like this automatically when manipulating nodes.

Now I realize that, it won’t do it. I have taken a workaround to solve it ( first, delete the block node; then, insert the inline node).