Skip to content

Commit

Permalink
Fix link editor (for good) (#477)
Browse files Browse the repository at this point in the history
* fix double link matchin

* fix #473 and improved behaviour
  • Loading branch information
ticruz38 authored Nov 20, 2024
1 parent 8856ab5 commit fb8ffd3
Showing 1 changed file with 17 additions and 12 deletions.
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

0 comments on commit fb8ffd3

Please sign in to comment.