So based on comments above, this is fixed code. Is this right?
function linkAround(doc, pos) {
let $pos = doc.resolve(pos);
let start = $pos.parent.childAfter($pos.parentOffset);
if (!start.node) {
return;
}
let link = start.node.marks.find((mark) => mark.type === this.$editorView.state.schema.marks.link);
if (!link) {
return;
}
let startIndex = $pos.index();
let startPos = $pos.start() + start.offset;
while (startIndex > 0 && link.isInSet($pos.parent.child(startIndex - 1).marks)) {
startIndex -= 1;
startPos -= $pos.parent.child(startIndex).nodeSize;
}
let endIndex = $pos.indexAfter();
let endPos = startPos + start.node.nodeSize;
while (endIndex < $pos.parent.childCount && link.isInSet($pos.parent.child(endIndex).marks)) {
endPos += $pos.parent.child(endIndex).nodeSize;
endIndex += 1;
}
return {from: startPos, to: endPos};
}