mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-17 19:20:39 +09:00
Extract mention-aware link node helper and add tests
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
eeb7e1a91a
commit
7c54b6e9e3
3 changed files with 119 additions and 42 deletions
67
ui/src/lib/mention-aware-link-node.ts
Normal file
67
ui/src/lib/mention-aware-link-node.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue