-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathEnrichedLink.ts
More file actions
215 lines (191 loc) · 5.41 KB
/
Copy pathEnrichedLink.ts
File metadata and controls
215 lines (191 loc) · 5.41 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import Link, { type LinkOptions } from '@tiptap/extension-link';
import { mergeAttributes, type CommandProps } from '@tiptap/core';
import type { Editor } from '@tiptap/react';
import {
nativeLeafText,
nativePosToTiptapPos,
} from '../nativeMappers/positionMapping';
import { isLinkBlocked } from './formatRules';
import { findAutolinkRangesInWord } from '../pmPlugins/AutolinkPlugin/autolinkRegex';
function isFullLinkMatch(text: string, linkRegex: RegExp | undefined): boolean {
const ranges = findAutolinkRangesInWord(text, linkRegex);
return ranges.some((r) => r.start === 0 && r.endExclusive === text.length);
}
export const EnrichedLink = Link.extend<
LinkOptions & { getLinkRegex: () => RegExp | null | undefined }
>({
excludes: 'link code',
parseHTML() {
return [
{
tag: 'a[href]',
getAttrs: (node: string | HTMLElement) => {
if (typeof node === 'string') return null;
const href = node.getAttribute('href');
if (!href || /^javascript:/i.test(href)) return false;
return null;
},
},
];
},
addAttributes() {
return {
...this.parent?.(),
auto: {
default: false,
parseHTML: (el) => {
const href = el.getAttribute('href');
const textContent = el.textContent;
if (!href || href !== textContent) return false;
const linkRegex = this.options.getLinkRegex();
if (linkRegex === null) return false;
return isFullLinkMatch(href, linkRegex);
},
renderHTML: () => {
return {};
},
},
};
},
renderHTML({ HTMLAttributes }) {
return [
'a',
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
0,
];
},
addOptions() {
const parent = this.parent?.()!;
return {
...parent,
openOnClick: false,
autolink: false,
linkOnPaste: false,
HTMLAttributes: {
...parent.HTMLAttributes,
target: null,
rel: null,
},
getLinkRegex: () => {
throw new Error('EnrichedLink.configure({ getLinkRegex }) is required');
},
};
},
addKeyboardShortcuts() {
return {};
},
addCommands() {
const parent = this.parent?.();
return {
...parent,
setLink: (attributes) => (props: CommandProps) => {
if (isLinkBlocked(props.editor)) {
return false;
}
return parent?.setLink?.(attributes)(props) ?? false;
},
toggleLink: (attributes) => (props: CommandProps) => {
if (isLinkBlocked(props.editor)) {
return false;
}
return parent?.toggleLink?.(attributes)(props) ?? false;
},
unsetLink: () => (props: CommandProps) => {
if (isLinkBlocked(props.editor)) {
return false;
}
return parent?.unsetLink?.()(props) ?? false;
},
};
},
addPasteRules() {
return [];
},
});
export function removeLink(editor: Editor, start: number, end: number) {
const { doc, selection: selectionBefore } = editor.state;
const from = nativePosToTiptapPos(doc, start);
const to = nativePosToTiptapPos(doc, end);
editor
.chain()
.focus()
.setTextSelection({ from, to })
.unsetLink()
.command(({ tr }) => {
const mapped = selectionBefore.map(tr.doc, tr.mapping);
if (mapped) {
tr.setSelection(mapped);
}
return true;
})
.run();
}
export function setLink(
editor: Editor,
start: number,
end: number,
text: string,
url: string
) {
const { state } = editor;
const doc = state.doc;
const from = nativePosToTiptapPos(doc, start);
const to = nativePosToTiptapPos(doc, end);
if (isRangeLinkBlocked(editor, from, to)) {
return;
}
if (text.length === 0) {
if (from !== to) {
editor.chain().focus().deleteRange({ from, to }).run();
}
return;
}
if (url.length === 0) {
return;
}
const linkType = state.schema.marks.link;
if (!linkType) return;
const linkMark = linkType.create({ href: url });
editor
.chain()
.focus()
.command(({ tr, state: s }) => {
if (from === to) {
const marksAtRangeStart = doc.resolve(from).marks();
const marksWithLink = linkMark.addToSet(marksAtRangeStart);
tr.insert(from, s.schema.text(text, marksWithLink));
} else {
const currentText = nativeLeafText(doc, from, to);
if (text !== currentText) {
const marksAtRangeStart = doc.resolve(from).marks();
const marksWithLink = linkMark.addToSet(marksAtRangeStart);
tr.replaceWith(from, to, s.schema.text(text, marksWithLink));
} else {
tr.addMark(from, to, linkMark);
}
}
return true;
})
.run();
}
// We use this function instead of relying on editor.isActive('...') because it
// checks the current selection, not from and to passed by user
function isRangeLinkBlocked(editor: Editor, from: number, to: number): boolean {
const { doc, schema } = editor.state;
const hasInlineCode =
schema.marks.code && doc.rangeHasMark(from, to, schema.marks.code);
if (hasInlineCode) {
return true;
}
let hasBlockedNode = false;
const blockedNodes = ['codeBlock', 'image'];
doc.nodesBetween(from, to, (node) => {
if (hasBlockedNode) return false;
if (blockedNodes.includes(node.type.name)) {
hasBlockedNode = true;
return false;
}
return true;
});
return hasBlockedNode;
}