Package net.carcdr.yprosemirror
Class ProseMirrorConverter
java.lang.Object
net.carcdr.yprosemirror.ProseMirrorConverter
Converts ProseMirror documents to Y-CRDT structures.
This class provides utilities for converting ProseMirror nodes, fragments, and documents into their Y-CRDT XML equivalents (YXmlFragment, YXmlElement, YXmlText).
Mapping Strategy:
- ProseMirror Node → YXmlElement (node type becomes XML tag)
- ProseMirror Fragment → YXmlFragment (container for nodes)
- Text content → YXmlText (with marks as formatting attributes)
- Node attributes → XML element attributes
Usage Example:
// Convert a ProseMirror document to Y-CRDT
YDoc ydoc = ProseMirrorConverter.prosemirrorToYDoc(pmNode, schema);
// Or convert to an existing YXmlFragment
try (YDoc ydoc = new YDoc();
YXmlFragment fragment = ydoc.getXmlFragment("prosemirror")) {
ProseMirrorConverter.nodeToYXml(pmNode, fragment, schema);
}
- Since:
- 0.1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidfragmentToYXml(com.atlassian.prosemirror.model.Fragment pmFragment, YXmlFragment yFragment, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror Fragment to a YXmlFragment.static voidnodeToYXml(com.atlassian.prosemirror.model.Node node, YXmlFragment fragment, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror Node to a YXmlFragment.static YDocprosemirrorToYDoc(com.atlassian.prosemirror.model.Node doc, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror document to a new YDoc.
-
Method Details
-
nodeToYXml
public static void nodeToYXml(com.atlassian.prosemirror.model.Node node, YXmlFragment fragment, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror Node to a YXmlFragment.This method traverses the ProseMirror node tree and creates corresponding YXmlElement and YXmlText nodes in the target fragment. The conversion preserves:
- Node hierarchy and structure
- Node types (as XML tags)
- Node attributes (as XML attributes)
- Text content and formatting (as YXmlText with attributes)
- Parameters:
node- the ProseMirror node to convertfragment- the target YXmlFragment to populateschema- the ProseMirror schema for validation- Throws:
IllegalArgumentException- if node or fragment is nullIllegalStateException- if the fragment is closed
-
fragmentToYXml
public static void fragmentToYXml(com.atlassian.prosemirror.model.Fragment pmFragment, YXmlFragment yFragment, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror Fragment to a YXmlFragment.- Parameters:
pmFragment- the ProseMirror fragment to convertyFragment- the target YXmlFragment to populateschema- the ProseMirror schema for validation- Throws:
IllegalArgumentException- if any parameter is nullIllegalStateException- if the fragment is closed
-
prosemirrorToYDoc
public static YDoc prosemirrorToYDoc(com.atlassian.prosemirror.model.Node doc, com.atlassian.prosemirror.model.Schema schema) Converts a ProseMirror document to a new YDoc.This is a convenience method that creates a new YDoc and populates it with the converted ProseMirror document. The document is stored in a YXmlFragment named "prosemirror".
- Parameters:
doc- the ProseMirror document to convertschema- the ProseMirror schema- Returns:
- a new YDoc containing the converted document
- Throws:
IllegalArgumentException- if doc or schema is null
-