Skip to content

Commit

Permalink
Revert "autolink improvement"
Browse files Browse the repository at this point in the history
This reverts commit 9a9e94b.
  • Loading branch information
Jakub Kolčář committed Sep 7, 2023
1 parent e861b24 commit 9e47766
Showing 1 changed file with 42 additions and 44 deletions.
86 changes: 42 additions & 44 deletions packages/extension-link/src/helpers/autolink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ import {
findChildrenInRange,
getChangedRanges,
getMarksBetween,
NodeWithPos,
} from '@tiptap/core'
import { MarkType } from '@tiptap/pm/model'
import { Plugin, PluginKey } from '@tiptap/pm/state'
import { find } from 'linkifyjs'

type AutolinkOptions = {
type: MarkType;
validate?: (url: string) => boolean;
}

type LinkType = {
type: string;
value: string;
isLink: boolean;
href: string;
start: number;
end: number;
type: MarkType
validate?: (url: string) => boolean
}

export function autolink(options: AutolinkOptions): Plugin {
Expand All @@ -45,57 +37,63 @@ export function autolink(options: AutolinkOptions): Plugin {
node => node.isTextblock,
)

// Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
const textBlock = nodesInChangedRanges[0]

if (!textBlock) {
return
let textBlock: NodeWithPos | undefined
let textBeforeWhitespace: string | undefined

if (nodesInChangedRanges.length > 1) {
// Grab the first node within the changed ranges (ex. the first of two paragraphs when hitting enter).
textBlock = nodesInChangedRanges[0]
textBeforeWhitespace = newState.doc.textBetween(
textBlock.pos,
textBlock.pos + textBlock.node.nodeSize,
undefined,
' ',
)
} else if (
nodesInChangedRanges.length
// We want to make sure to include the block seperator argument to treat hard breaks like spaces.
&& newState.doc.textBetween(newRange.from, newRange.to, ' ', ' ').endsWith(' ')
) {
textBlock = nodesInChangedRanges[0]
textBeforeWhitespace = newState.doc.textBetween(
textBlock.pos,
newRange.to,
undefined,
' ',
)
}

const textBeforeWhitespace = newState.doc.textBetween(
textBlock.pos,
newRange.to,
' ',
' ',
)

if (textBeforeWhitespace) {
if (textBlock && textBeforeWhitespace) {
const wordsBeforeWhitespace = textBeforeWhitespace.split(' ').filter(s => s !== '')

if (!/\s*\S+\s+$/gm.test(textBeforeWhitespace)) {
return
}

const lastWordBeforeSpaceMatch = textBeforeWhitespace.matchAll(/\s*(\S+)\s+$/gm)
const lastMatch = Array.from(lastWordBeforeSpaceMatch).at(-1)

if (lastMatch === undefined || lastMatch.length < 2) {
return
if (wordsBeforeWhitespace.length <= 0) {
return false
}

const lastWordBeforeSpace = lastMatch[1]
const lastWordBeforeSpace = wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1]
const lastWordAndBlockOffset = textBlock.pos + textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace)

if (!lastWordBeforeSpace) {
return false
}

find(lastWordBeforeSpace)
.filter((link: LinkType) => link.isLink)
.filter(link => link.isLink)
// Calculate link position.
.map((link: LinkType) => ({
.map(link => ({
...link,
start: lastWordAndBlockOffset + link.start + 1,
end: lastWordAndBlockOffset + link.end + 1,
from: lastWordAndBlockOffset + link.start + 1,
to: lastWordAndBlockOffset + link.end + 1,
}))
// ignore link inside code mark
.filter((link: LinkType) => {
.filter(link => {
if (!newState.schema.marks.code) {
return true
}

return !newState.doc.rangeHasMark(
link.start,
link.end,
link.from,
link.to,
newState.schema.marks.code,
)
})
Expand All @@ -108,13 +106,13 @@ export function autolink(options: AutolinkOptions): Plugin {
})
// Add link mark.
.forEach(link => {
if (getMarksBetween(link.start, link.end, newState.doc).some(item => item.mark.type === options.type)) {
if (getMarksBetween(link.from, link.to, newState.doc).some(item => item.mark.type === options.type)) {
return
}

tr.addMark(
link.start,
link.end,
link.from,
link.to,
options.type.create({
href: link.href,
}),
Expand Down

0 comments on commit 9e47766

Please sign in to comment.