a tool for shared writing and social publishing
1import { XmlElement, XmlText, XmlHook } from "yjs";
2
3export type Delta = {
4 insert: string;
5 attributes?: {
6 strong?: {};
7 code?: {};
8 em?: {};
9 underline?: {};
10 strikethrough?: {};
11 highlight?: { color: string };
12 link?: { href: string };
13 };
14};
15
16export function YJSFragmentToString(
17 node: XmlElement | XmlText | XmlHook,
18): string {
19 if (node.constructor === XmlElement) {
20 // Handle hard_break nodes specially
21 if (node.nodeName === "hard_break") {
22 return "\n";
23 }
24 // Handle inline mention nodes
25 if (node.nodeName === "didMention" || node.nodeName === "atMention") {
26 return node.getAttribute("text") || "";
27 }
28 return node
29 .toArray()
30 .map((f) => YJSFragmentToString(f))
31 .join("");
32 }
33 if (node.constructor === XmlText) {
34 return (node.toDelta() as Delta[])
35 .map((d) => {
36 return d.insert;
37 })
38 .join("");
39 }
40 return "";
41}