-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdast-util-from-prosemirror.ts
192 lines (169 loc) · 5.28 KB
/
mdast-util-from-prosemirror.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { ok as assert } from "devlop";
import {
Nodes as MdastNodes,
RootContent as MdastRootContent,
Root as MdastRoot,
} from "mdast";
import { Node as PmNode, Mark as PmMark, Schema } from "prosemirror-model";
import { is } from "unist-util-is";
interface PmMarkedNode {
node: PmNode;
marks: readonly PmMark[];
}
interface State<PmNodes extends string, PmMarks extends string> {
one(pmNode: PmNode, parent?: PmNode): MdastNodes | MdastNodes[] | null;
all(pmNode: PmNode): MdastNodes[];
nodeHandlers: PmNodeHandlers<PmNodes>;
markHandlers: PmMarkHandlers<PmMarks>;
}
function createState<PmNodes extends string, PmMarks extends string>(
nodeHandlers: PmNodeHandlers<PmNodes>,
markHandlers: PmMarkHandlers<PmMarks>,
): State<PmNodes, PmMarks> {
const state: State<PmNodes, PmMarks> = {
one,
all,
nodeHandlers,
markHandlers,
};
function one(
pmNode: PmNode,
parent?: PmNode,
): MdastNodes | MdastNodes[] | null {
const schema = pmNode.type.schema;
const nodeName = pmNode.type.name as PmNodes;
const handler = state.nodeHandlers[nodeName];
if (handler) {
return handler(pmNode, parent, state);
}
if (pmNode.type === schema.topNodeType) {
const children = state.all(pmNode);
assert(
!children.some((child) => is(child, "root")),
"Expected non-root nodes",
);
return { type: "root", children: children as MdastRootContent[] };
}
if (pmNode.type === schema.nodes["text"]) {
// Text nodes always have a text property
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return { type: "text", value: pmNode.text! };
}
return null;
}
function processChildPartition(nodes: PmMarkedNode[], parent: PmNode) {
const firstChild = nodes[0];
const firstMark = firstChild?.marks[0];
if (!firstMark) return nodes.map((node) => state.one(node.node, parent));
const children = hydrateMarks(
nodes.map(({ node, marks }) => ({ node, marks: marks.slice(1) })),
parent,
);
const handler = state.markHandlers[firstMark.type.name as PmMarks];
if (!handler) return children;
return handler(firstMark, parent, children, state);
}
function hydrateMarks(
children: PmMarkedNode[],
parent: PmNode,
): MdastNodes[] {
const partitioned = children.reduce<PmMarkedNode[][]>((acc, child) => {
const lastPartition = acc[acc.length - 1];
if (!lastPartition) {
return [[child]];
}
const lastChild = lastPartition[lastPartition.length - 1];
if (!lastChild) {
return [...acc.slice(0, acc.length), [child]];
}
if (
(!child.marks.length && !lastChild.marks.length) ||
(child.marks.length &&
lastChild.marks.length &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
child.marks[0]?.eq(lastChild.marks[0]!))
) {
return [
...acc.slice(0, acc.length - 1),
[...lastPartition.slice(0, lastPartition.length), child],
];
}
return [...acc, [child]];
}, []);
return partitioned
.flatMap((nodes) => processChildPartition(nodes, parent))
.filter((node): node is MdastNodes | MdastNodes[] => !!node)
.flat();
}
function all(pmNode: PmNode): MdastNodes[] {
return hydrateMarks(
pmNode.children.map((child) => ({ node: child, marks: child.marks })),
pmNode,
);
}
return state;
}
export interface Options<PmNodes extends string, PmMarks extends string> {
schema: Schema<PmNodes, PmMarks>;
nodeHandlers: PmNodeHandlers<PmNodes>;
markHandlers: PmMarkHandlers<PmMarks>;
}
export function fromProseMirror<PmNodes extends string, PmMarks extends string>(
pmNode: PmNode,
options: Options<PmNodes, PmMarks>,
): MdastRoot {
const state = createState(options.nodeHandlers, options.markHandlers);
return state.one(pmNode) as MdastRoot;
}
export type PmNodeHandler = (
node: PmNode,
parent: PmNode | undefined,
state: State<string, string>,
) => MdastNodes | MdastNodes[] | null;
export type PmNodeHandlers<PmNodes extends string> = Partial<
Record<PmNodes, PmNodeHandler>
>;
export type PmMarkHandler = (
mark: PmMark,
parent: PmNode,
children: MdastNodes[],
state: State<string, string>,
) => MdastNodes | MdastNodes[] | null;
export type PmMarkHandlers<PmMarks extends string> = Partial<
Record<PmMarks, PmMarkHandler>
>;
export function fromPmNode<Type extends MdastNodes["type"]>(
type: Type,
getAttrs?: (
pmNode: PmNode,
) => Omit<Extract<MdastNodes, { type: Type }>, "type" | "children">,
): PmNodeHandler {
return (
node: PmNode,
_: PmNode | undefined,
state: State<string, string>,
) => {
const children = state.all(node);
const result = {
type,
...getAttrs?.(node),
children,
};
return result as Extract<MdastNodes, { type: Type }>;
};
}
export function fromPmMark<Type extends MdastNodes["type"]>(
type: Type,
getAttrs?: (
pmNode: PmMark,
) => Omit<Extract<MdastNodes, { type: Type }>, "type" | "children">,
): PmMarkHandler {
return (mark: PmMark, _: PmNode, mdastChildren: MdastNodes[]) => {
const result = {
type,
...getAttrs?.(mark),
children: mdastChildren,
};
return result as Extract<MdastNodes, { type: Type }>;
};
}