Getting rid of marks in transformPasted

I’m writing a plugin which shall remove specific marks from the fragment passed to the transformPasted plugin hook. My attempt would be to write a recursive function which steps all the way down to the text nodes, modifies the marks array accordingly, then returns a new, modified version of that node. Something like this:

function step(nodeOrFragment) {
  if (nodeOrFragment instanceof Fragment) {
    const content = nodeOrFragment.content.map(item => step(item));
    return new Fragment(content, ???);
  } else if (nodeOrFragment instanceof Node) {
    if (nodeOrFragment.isText) {
      // ... make copy of text node and remove marks ...
      return modifiedTextNode;
    } else {
      const content = step(nodeOrFragment.content);
      return nodeOrFragment.copy(content);
    }
  }
}

Would this be a feasible solution, or is there a better one?

1 Like

That works. You could also use a custom clipboardParser that doesn’t parse those types of marks, but the transform function is probably just as easy and more composable with other clipboard magic.

Here’s the complete implementation:

function step(nodeOrFragment, markType) {
  if (nodeOrFragment instanceof Fragment) {
    const content = nodeOrFragment.content.map(item => step(item, markType));
    return new Fragment(content, nodeOrFragment.size);
  } else if (nodeOrFragment instanceof Node) {
    if (nodeOrFragment.isText) {
      // Here, additionally one could check if the mark exists at all and otherwise return the node immediately
      const newMarks = markType.removeFromSet(nodeOrFragment.marks);
      return nodeOrFragment.mark(newMarks);
    } else {
      const content = step(nodeOrFragment.content, markType);
      return nodeOrFragment.copy(content);
    }
  }
}
1 Like

Hi im new to prosemirror, im trying to figure out how to remove text nodes which have some specific marks.