mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
bf45e97641
* Update types * fix circular dep * type imports * lint type imports and --fix
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import type { Node, Schema } from "prosemirror-model";
|
|
import type { Primitive } from "utility-types";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import { isList } from "../queries/isList";
|
|
|
|
export function transformListToMentions(
|
|
listNode: Node,
|
|
schema: Schema,
|
|
attrs: Record<string, Primitive>
|
|
): Node {
|
|
const childNodes: Node[] = [];
|
|
|
|
listNode.forEach((node) => {
|
|
childNodes.push(transformListItemToMentions(node, schema, attrs));
|
|
});
|
|
|
|
return listNode.type.create(listNode.attrs, childNodes);
|
|
}
|
|
|
|
function transformListItemToMentions(
|
|
listItemNode: Node,
|
|
schema: Schema,
|
|
attrs: Record<string, Primitive>
|
|
) {
|
|
const childNodes: Node[] = [];
|
|
|
|
listItemNode.forEach((node) => {
|
|
if (node.type.name === "paragraph") {
|
|
const link = node.textContent;
|
|
const mentionType = attrs[link];
|
|
|
|
if (mentionType) {
|
|
childNodes.push(
|
|
node.type.create(
|
|
node.attrs,
|
|
schema.nodes.mention.create({
|
|
id: uuidv4(),
|
|
type: mentionType,
|
|
label: link,
|
|
href: link,
|
|
modelId: uuidv4(),
|
|
actorId: attrs.actorId,
|
|
})
|
|
)
|
|
);
|
|
} else {
|
|
childNodes.push(node);
|
|
}
|
|
} else if (isList(node, schema)) {
|
|
const subListNode = transformListToMentions(node, schema, attrs);
|
|
childNodes.push(subListNode);
|
|
}
|
|
});
|
|
|
|
return listItemNode.type.create(listItemNode.attrs, childNodes);
|
|
}
|