Flattening non-leaf nodes into text on copy/paste

I want to change how copying/pasting works for a non-leaf node. In particular, I want to render the node as flat text, and on paste parse it using a regular expression.

For example,

"doc": {
    "type": "doc",
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "type": "text",
            "text": "text "
          },
          {
            "type": "math",
            "content": [
              {
                "type": "text",
                "text": "y = mx + b"
              }
            ]
          },
          {
            "type": "text",
            "text": " moretext"
          }
        ]
      }
    ]
  }

should be serialized as text ${y = mx + b} moretext

I created the following toDOM rule for this:

  toDOM: (node: ProseMirror.Node) => {
    return "${" + node.textContent + "}"
  }

My plan is to then use a transformPasted plugin, like the one here to then serialize this back into math, matching on the ${ ... } regular expression.

Upon trying to do this, though, I get the following error:

I am wondering if there’s a reason for this error other than keeping a dev from shooting themselves in the foot, and if it’s safe to ignore in this case.

Yeah, that was there as a way to point out mistakes, but I guess there’s a good case to be made for what you’re doing. Changed in this patch.

1 Like