Spaces:
Paused
Paused
File size: 1,695 Bytes
b152fd5 | 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 | import {
LinkNode,
type LinkAttributes,
type SerializedLinkNode,
} from "@lexical/link";
const CUSTOM_MENTION_URL_RE = /^(agent|project):\/\//;
export class MentionAwareLinkNode extends LinkNode {
static getType(): string {
return "mention-aware-link";
}
static clone(node: MentionAwareLinkNode): MentionAwareLinkNode {
return new MentionAwareLinkNode(
node.getURL(),
{
rel: node.getRel(),
target: node.getTarget(),
title: node.getTitle(),
},
node.getKey(),
);
}
static importJSON(serializedNode: SerializedLinkNode): MentionAwareLinkNode {
return new MentionAwareLinkNode(
serializedNode.url ?? "",
{
rel: serializedNode.rel ?? null,
target: serializedNode.target ?? null,
title: serializedNode.title ?? null,
},
);
}
constructor(url?: string, attributes?: LinkAttributes, key?: string) {
super(url, attributes, key);
}
sanitizeUrl(url: string): string {
if (CUSTOM_MENTION_URL_RE.test(url)) return url;
return super.sanitizeUrl(url);
}
}
type MentionAwareLinkSource = Pick<LinkNode, "getURL" | "getRel" | "getTarget" | "getTitle">;
export function getMentionAwareLinkNodeInit(node: MentionAwareLinkSource) {
return {
url: node.getURL(),
attributes: {
rel: node.getRel(),
target: node.getTarget(),
title: node.getTitle(),
},
};
}
export const mentionAwareLinkNodeReplacement = {
replace: LinkNode,
with: (node: LinkNode) => {
const { url, attributes } = getMentionAwareLinkNodeInit(node);
return new MentionAwareLinkNode(url, attributes);
},
withKlass: MentionAwareLinkNode,
} as const;
|