Utils
serialize
Bridge a rich editor doc to plain text (and structured mentions) and back — independent of any one component.
Preview
Standalone, independent of any one component
Installed to components/control-ui/chat-composer-editor/serialize.ts with the ChatComposer rich editor, so the shared helper stays in one place.
Source
Installed util source
import type { Node as ProseMirrorNode, Schema } from "prosemirror-model";// Each leaf serializes through its own schema `leafText`, so extensions stay self-describing and this stays mention-unaware.export function serializeDoc(doc: ProseMirrorNode): string { return doc.textBetween(0, doc.content.size, "\n", (leaf) => { const leafText = leaf.type.spec.leafText; return typeof leafText === "function" ? leafText(leaf) : ""; });}// external resets only — rebuilding doc from its own output would fight caretexport function docFromText(schema: Schema, text: string): ProseMirrorNode { const paragraph = schema.nodes.paragraph; const doc = schema.nodes.doc; if (!paragraph || !doc) { throw new Error("Chat composer schema requires doc and paragraph nodes."); } const blocks = text.split("\n").map((line) => paragraph.create(null, line === "" ? null : schema.text(line))); return doc.create(null, blocks.length === 0 ? paragraph.create() : blocks);}