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 // Handle footnote nodes - emit placeholder
29 if (node.nodeName === "footnote") {
30 return "*";
31 }
32 return node
33 .toArray()
34 .map((f) => YJSFragmentToString(f))
35 .join("");
36 }
37 if (node.constructor === XmlText) {
38 return (node.toDelta() as Delta[])
39 .map((d) => {
40 return d.insert;
41 })
42 .join("");
43 }
44 return "";
45}