Filter pasted nodes

Dear community,

our TableCell node has a hardcoded list of node types it supports as children (text, paragraph, lists, code blocks and images).

Now I want to make sure that pasting to the cell doesn’t break the table, so I search for a way to filter the pasted nodes and transform all nodes that are not allowed as cell children into their text content.

I guess it can be done both in handlePaste() and in transformPasted(), but I’m unsure how to best mutate the slide.

I guess because the slice is expected to be immuable, it’s best to build a new slice, but there I fail. Can I do this somehow using slice.content.descendants()?

Just after posting here I found this old thread and came up with a solution that seems to work. Would be curious if that’s the correct approach.

// Must contain all allowed content node groups or types (for pasting into table)
const childNodeGroups = new Set(['list', 'image'])
const childNodeTypes = new Set(['paragraph', 'codeBlock'])

const supportedChildNode = (node) =>
	node.type.groups.some((group) => childNodeGroups.has(group))
	|| childNodeTypes.has(node.type.name)

// [...]

handlePaste: (view, _event, slice) => {
	if (!this.editor.isActive(this.type.name)) {
		return false
	}

	const { state } = view
	const { schema } = state

	const mapFragment = (fragment) => {
		Fragment.fromArray(
			fragment.content.map((node) => {
				if (!node.isText && !supportedChildNode(node)) {
					return node.textContent
						? schema.text(node.textContent, node.marks)
						: false
				} else if (node.content.childCount > 0) {
					return node.type.create(node.attrs, mapFragment(node.content))
				}
				return node
			}).filter((node) => node)
		)
	}

	slice.content = mapFragment(slice.content)
},

By the way, this is for adding multiline table support in Nextcloud Text, an editor built on top of ProseMirror and Tiptap. We cannot support arbitrary block nodes in table cells as everything needs to get serialized from and to markdown.

Seems like my solution is not working as expected. What I try to do is rebuild the slice in a way that unsupported nodes get replaced with a text node and supported nodes stay. Any hints?