How to customize serialization of Node when calling `state.toJSON()`

I defined a NodeSpec:

test: {
      content: "paragraph+",
      isolating: true,
      parseDOM: [{tag: `div[data-name="test"]`}],
      toDOM() { return ["div", {"data-name": test, data-value: "..."}, 0] }
}

When I call state.toJSON(), it generates for this node something like this:

"type": "test",
"content": [
                    {
                        "type": "paragraph",
                        "content": [
                            {
                                "type": "text",
                                "text": ""
                            }
                        ]
                    }
]

I want to have some attributes, say data-value, in the output as well.

At first I guessed I might add a function property toJSON to the NodeSpec to make it work. Then I checked the reference. There is no such property in NodeSpec interface.

You can’t. The way nodes are serialized is fixed by the library. But it seems that if you keep any data to put in data-value, it would have to live in the node’s attributes anyway, or it won’t be represented as part of the node.

Do you mean it is not possible for a customized Node to behave just like heading or image which has attrs key in the output of state.toJSON()? May I extend the buildin Node to have another attribute, say lang, in attrs?

You can define attributes for your own nodes. There’s nothing special about the nodes in the schema modules.

OK, I see. Thank you.