Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/app/editor/LinkExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const LinkExtension = Node.create({
name: "inlineLink",
group: "inline",
inline: true,
inclusive: false,
selectable: true,
draggable: true,
priority: 1000,
Expand Down Expand Up @@ -65,32 +66,36 @@ export const LinkExtension = Node.create({
return [
new InputRule({
find: text => {
const realText = this.editor.state.doc.textContent
const match = last(Array.from(text.matchAll(LINK_REGEX)))
if (match && text.length === match.index + match[0].length + 1) {
const realMatch = last(Array.from(realText.matchAll(LINK_REGEX)))
if (match && realMatch && text.length === match.index + match[0].length + 1) {
return {
index: match.index!,
text: match[0],
data: {
url: match[0],
url: match[0].trim(),
},
}
}

return null
},
handler: ({state, range, match}) => {
const {tr} = state
handler: ({range, match, chain}) => {
if (match[0]) {
try {
tr.insert(range.from - 1, this.type.create(match.data))
.delete(tr.mapping.map(range.from - 1), tr.mapping.map(range.to))
.insertText(last(Array.from(match.input!)))
} catch (e) {
// If the node was already linkified, the above code breaks for whatever reason
const lastChar = last(Array.from(match.input!))
const chainCommand = chain()
.deleteRange({from: range.from - 1, to: range.from - 1 + match[0].length})
.insertLink({url: match[0]})

if (lastChar === "\n") {
chainCommand.splitBlock()
} else {
chainCommand.insertContent(" ")
}
}

tr.scrollIntoView()
chainCommand.run()
}
},
}),
]
Expand Down