Define text serialization in NodeSpec

Is there an isolated way of defining how a node is serialized to text, preferably directly in the NodeSpec?

Let’s take the dino example:

If I copy the second line and pasted it here as plain text, I get this:

This paragraph , for example, is full of dinosaurs.

What I would like to have is the following result:

This paragraph stegosaurus, for example, triceratops is full tyrannosaurus of dinosaurs.

Achieving that seems to be possible only using the editor prop clipboardTextSerializer. That however adds centralized editor-specific logic far apart from the actual node specification.

I’d prefer to have the logic right inside the node specification, as the serialization is specific to this type of node, just like .toDOM()

I don’t think this will be part of the core, but you can add additional fields to node specs, and read them again from, for example, a clipboardTextSerializer function.

I understand.

What is the reason that node types define their own DOM representation but not their own text representation?

The core needs to be able to convert nodes from and to DOM representation. Text representations are highly context dependent (there’s just no universally accepted way to convert most node types to text) and the library can mostly get away with not worrying about it. (Very early versions did handle it, wiring in the markdown code in a way analogous to the DOM representation, but that was a pain to get right and incurred a bunch of unnecessarily heavy dependencies.)

1 Like